PdfPageLabels { 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(); } else { prefix = ""; } 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; } PdfPageLabels(); void addPageLabel(int page, int numberStyle, String text, int firstPage); void addPageLabel(int page, int numberStyle, String text, int firstPage, boolean includeFirstPage); void addPageLabel(int page, int numberStyle, String text); void addPageLabel(int page, int numberStyle); void addPageLabel(PdfPageLabelFormat format); void removePageLabel(int page); PdfDictionary getDictionary(PdfWriter writer); static String[] getPageLabels(PdfReader reader); static PdfPageLabelFormat[] getPageLabelFormats(PdfReader reader); static final int DECIMAL_ARABIC_NUMERALS; static final int UPPERCASE_ROMAN_NUMERALS; static final int LOWERCASE_ROMAN_NUMERALS; static final int UPPERCASE_LETTERS; static final int LOWERCASE_LETTERS; static final int EMPTY; }
@Test public void testGetPageLabels() throws Exception { File testFile = TestResourceUtils.getResourceAsTempFile(this, "test-prefix-reset.pdf"); String[] expectedPageLabels = new String[] {"i", "ii", "iii", "iv", "v", "1", "2", "3", "4", "5", "G1", "G2", "G3", "G4", "G5", "6", "7", "8", "9", "10"}; PdfReader reader = new PdfReader(testFile.getAbsolutePath()); String[] pageLabels = PdfPageLabels.getPageLabels(reader); Assert.assertNotNull(pageLabels); Assert.assertEquals(expectedPageLabels.length, pageLabels.length); for (int page = 0; page < pageLabels.length; page++) { Assert.assertEquals(expectedPageLabels[page], pageLabels[page]); } reader.close(); }