Beispiel #1
0
  /**
   * Retrieves the page labels from a PDF as an array of String objects.
   *
   * @param reader a PdfReader object that has the page labels you want to retrieve
   * @return a String array or <code>null</code> if no page labels are present
   */
  public static String[] getPageLabels(PdfReader reader) {

    int n = reader.getNumberOfPages();

    PdfDictionary dict = reader.getCatalog();
    PdfDictionary labels =
        (PdfDictionary) PdfReader.getPdfObjectRelease(dict.get(PdfName.PAGELABELS));
    if (labels == null) return null;

    String[] labelstrings = new String[n];

    HashMap<Integer, PdfObject> numberTree = PdfNumberTree.readTree(labels);

    int pagecount = 1;
    Integer current;
    String prefix = "";
    char type = 'D';
    for (int i = 0; i < n; i++) {
      current = Integer.valueOf(i);
      if (numberTree.containsKey(current)) {
        PdfDictionary d = (PdfDictionary) PdfReader.getPdfObjectRelease(numberTree.get(current));
        if (d.contains(PdfName.ST)) {
          pagecount = ((PdfNumber) d.get(PdfName.ST)).intValue();
        } else {
          pagecount = 1;
        }
        if (d.contains(PdfName.P)) {
          prefix = ((PdfString) d.get(PdfName.P)).toUnicodeString();
        }
        if (d.contains(PdfName.S)) {
          type = ((PdfName) d.get(PdfName.S)).toString().charAt(1);
        } else {
          type = 'e';
        }
      }
      switch (type) {
        default:
          labelstrings[i] = prefix + pagecount;
          break;
        case 'R':
          labelstrings[i] = prefix + RomanNumberFactory.getUpperCaseString(pagecount);
          break;
        case 'r':
          labelstrings[i] = prefix + RomanNumberFactory.getLowerCaseString(pagecount);
          break;
        case 'A':
          labelstrings[i] = prefix + RomanAlphabetFactory.getUpperCaseString(pagecount);
          break;
        case 'a':
          labelstrings[i] = prefix + RomanAlphabetFactory.getLowerCaseString(pagecount);
          break;
        case 'e':
          labelstrings[i] = prefix;
          break;
      }
      pagecount++;
    }
    return labelstrings;
  }
  private Chunk formatNumber(StyleListProperties listProperties, int value) {
    Chunk symbol = new Chunk("", getFont());

    StyleTextProperties textProperties = listProperties.getTextProperties();
    if (textProperties != null) {
      Font font = textProperties.getFont();
      if (font != null) {
        symbol.setFont(font);
      }
    }

    StyleNumFormat numFormat = listProperties.getNumFormat();
    if (numFormat != null) {
      StringBuilder sbuf = new StringBuilder();

      // num-prefix
      String numPrefix = listProperties.getNumPrefix();
      if (numPrefix != null) {
        sbuf.append(numPrefix);
      }

      // number
      if (numFormat.isAlphabetical()) {
        sbuf.append(RomanAlphabetFactory.getString(value, numFormat.isLowercase()));
      } else if (numFormat.isRoman()) {
        sbuf.append(RomanNumberFactory.getString(value, numFormat.isLowercase()));
      } else {
        sbuf.append(value);
      }

      // num-suffix
      String numSuffix = listProperties.getNumSuffix();
      if (numSuffix != null) {
        sbuf.append(numSuffix);
      }

      symbol.append(sbuf.toString());
    }
    return symbol;
  }