Skip to content

Commit

Permalink
change PdfPageLabels.getPageLabels() to scope prefixes to the range
Browse files Browse the repository at this point in the history
they are defined for

While reading a PDF generated by Adobe InDesign CS6 using the Adobe
PDF Library 10.0.1 which included several page label sections I
noticed iText was keeping the prefix applied to subsequent sections
in the values return by PdfPageLabels.getPageLabels(), even when the
key was omitted in the following page label dictionary entries.
According to the PDF Reference 1.7, Table 8.10 says that /P is an
optional label prefix for page labels in this range. PDF Readers I
have installed scope the prfix only to the range it is specified.
This change empties out the prefix field to give behaviour I think
aligns with the reference and my experience of other PDF readers.

I am not aware of any potential regression issues introduced by this
change, existing page label dictionaries created by iText set /P to
an empty string so this will only affect reading PDFs with partially
prefixed page label ranges as far as I know.
  • Loading branch information
Palmr committed Oct 26, 2015
1 parent 1803584 commit 856b648
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/itextpdf/text/pdf/PdfPageLabels.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ public static String[] getPageLabels(PdfReader reader) {
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);
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/itextpdf/text/pdf/PdfPageLabelsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.itextpdf.text.pdf;

import com.itextpdf.testutils.TestResourceUtils;
import junit.framework.Assert;
import org.junit.Test;

import java.io.File;

public class PdfPageLabelsTest {
@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();
}
}
Binary file not shown.

0 comments on commit 856b648

Please sign in to comment.