Skip to content

Commit 3312d13

Browse files
committed
Add structParents directly before adding entry to parentTree so StructParents write only for pages when it's necessary; update cmps
DEVSIX-1253
1 parent baccac8 commit 3312d13

File tree

16 files changed

+10
-26
lines changed

16 files changed

+10
-26
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfPage.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class PdfPage extends PdfObjectWrapper<PdfDictionary> {
7373
private static final long serialVersionUID = -952395541908379500L;
7474
private PdfResources resources = null;
7575
private int mcid = -1;
76-
private int structParents = -1;
7776
PdfPages parentPages;
7877
private List<PdfName> excludedKeys = new ArrayList<>(Arrays.asList(
7978
PdfName.Parent,
@@ -106,8 +105,6 @@ protected PdfPage(PdfDocument pdfDocument, PageSize pageSize) {
106105
getPdfObject().put(PdfName.MediaBox, new PdfArray(pageSize));
107106
getPdfObject().put(PdfName.TrimBox, new PdfArray(pageSize));
108107
if (pdfDocument.isTagged()) {
109-
structParents = (int) pdfDocument.getNextStructParentIndex();
110-
getPdfObject().put(PdfName.StructParents, new PdfNumber(structParents));
111108
setTabOrder(PdfName.S);
112109
}
113110
}
@@ -392,8 +389,8 @@ public PdfPage copyTo(PdfDocument toDocument, IPdfPageExtraCopier copier) {
392389
}
393390
}
394391
if (toDocument.isTagged()) {
395-
page.structParents = (int) toDocument.getNextStructParentIndex();
396-
page.getPdfObject().put(PdfName.StructParents, new PdfNumber(page.structParents));
392+
int structParents = (int) toDocument.getNextStructParentIndex();
393+
page.getPdfObject().put(PdfName.StructParents, new PdfNumber(structParents));
397394
}
398395

399396
if (copier != null) {
@@ -720,17 +717,10 @@ public int getNextMcid() {
720717
* Gets {@link Integer} key of the page’s entry in the structural parent tree.
721718
*
722719
* @return {@link Integer} key of the page’s entry in the structural parent tree.
720+
* If page has no entry in the structural parent tree, returned value is -1.
723721
*/
724722
public Integer getStructParentIndex() {
725-
if (structParents == -1) {
726-
PdfNumber n = getPdfObject().getAsNumber(PdfName.StructParents);
727-
if (n != null) {
728-
structParents = n.intValue();
729-
} else {
730-
structParents = (int) getDocument().getNextStructParentIndex();
731-
}
732-
}
733-
return structParents;
723+
return getPdfObject().getAsNumber(PdfName.StructParents) != null ? getPdfObject().getAsNumber(PdfName.StructParents).intValue() : -1;
734724
}
735725

736726
/**

kernel/src/main/java/com/itextpdf/kernel/pdf/tagging/ParentTreeHandler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public PdfObjRef findObjRefByStructParentIndex(PdfDictionary pageDict, int struc
122122
}
123123

124124
public int getNextMcidForPage(PdfPage page) {
125-
TreeMap<Integer, PdfMcr> pageMcrs = pageToPageMcrs.get(page.getPdfObject().getIndirectReference());
125+
TreeMap<Integer, PdfMcr> pageMcrs = (TreeMap) getPageMarkedContentReferences(page);
126126
if (pageMcrs == null || pageMcrs.size() == 0) {
127127
return 0;
128128
} else {
@@ -146,7 +146,7 @@ public void createParentTreeEntryForPage(PdfPage page) {
146146
return;
147147
}
148148
pageToPageMcrs.remove(page.getPdfObject().getIndirectReference());
149-
updateStructParentTreeEntries(page.getStructParentIndex(), mcrs);
149+
updateStructParentTreeEntries(page, mcrs);
150150
structTreeRoot.setModified();
151151
}
152152

@@ -269,7 +269,7 @@ private void registerAllMcrs() {
269269
}
270270
}
271271

272-
private void updateStructParentTreeEntries(Integer pageStructParentIndex, Map<Integer, PdfMcr> mcrs) {
272+
private void updateStructParentTreeEntries(PdfPage page, Map<Integer, PdfMcr> mcrs) {
273273
// element indexes in parentsOfPageMcrs shall be the same as mcid of one of their kids.
274274
// See "Finding Structure Elements from Content Items" in pdf spec.
275275
PdfArray parentsOfPageMcrs = new PdfArray();
@@ -289,10 +289,11 @@ private void updateStructParentTreeEntries(Integer pageStructParentIndex, Map<In
289289
}
290290
}
291291

292-
293292
if (parentsOfPageMcrs.size() > 0) {
294293
parentsOfPageMcrs.makeIndirect(structTreeRoot.getDocument());
295-
parentTree.addEntry(pageStructParentIndex, parentsOfPageMcrs);
294+
int structParents = (int) page.getStructParentIndex() != -1 ? (int) page.getStructParentIndex() : (int) page.getDocument().getNextStructParentIndex();
295+
page.getPdfObject().put(PdfName.StructParents, new PdfNumber(structParents));
296+
parentTree.addEntry((Integer) structParents, parentsOfPageMcrs);
296297
structTreeRoot.getDocument().checkIsoConformance(parentsOfPageMcrs, IsoKey.TAG_STRUCTURE_ELEMENT);
297298
parentsOfPageMcrs.flush();
298299
}

kernel/src/test/java/com/itextpdf/kernel/pdf/ParentTreeTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ This file is part of the iText (R) project.
5959
import com.itextpdf.test.annotations.type.IntegrationTest;
6060

6161
import org.junit.BeforeClass;
62-
import org.junit.Ignore;
6362
import org.junit.Test;
6463
import org.junit.experimental.categories.Category;
6564

@@ -76,7 +75,6 @@ public static void beforeClass() {
7675
}
7776

7877
@Test
79-
@Ignore("DEVSIX-1253")
8078
public void test01() throws IOException, InterruptedException {
8179
String outFile = destinationFolder + "parentTreeTest01.pdf";
8280
String cmpFile = sourceFolder + "cmp_parentTreeTest01.pdf";
@@ -109,7 +107,6 @@ public void test01() throws IOException, InterruptedException {
109107

110108

111109
@Test
112-
@Ignore("DEVSIX-1253")
113110
public void test02() throws IOException, InterruptedException {
114111
String outFile = destinationFolder + "parentTreeTest02.pdf";
115112
String cmpFile = sourceFolder + "cmp_parentTreeTest02.pdf";
@@ -142,7 +139,6 @@ public void test02() throws IOException, InterruptedException {
142139
}
143140

144141
@Test
145-
@Ignore("DEVSIX-1253")
146142
public void test03() throws IOException, InterruptedException {
147143
String outFile = destinationFolder + "parentTreeTest03.pdf";
148144
String cmpFile = sourceFolder + "cmp_parentTreeTest03.pdf";
@@ -176,7 +172,6 @@ public void test03() throws IOException, InterruptedException {
176172
}
177173

178174
@Test
179-
@Ignore("DEVSIX-1253")
180175
public void test04() throws IOException, InterruptedException {
181176
String outFile = destinationFolder + "parentTreeTest04.pdf";
182177
String cmpFile = sourceFolder + "cmp_parentTreeTest04.pdf";
@@ -208,7 +203,6 @@ public void test04() throws IOException, InterruptedException {
208203
}
209204

210205
@Test
211-
@Ignore("DEVSIX-1253")
212206
public void test05() throws IOException, InterruptedException {
213207
String outFile = destinationFolder + "parentTreeTest05.pdf";
214208
String cmpFile = sourceFolder + "cmp_parentTreeTest05.pdf";
@@ -257,7 +251,6 @@ public void test05() throws IOException, InterruptedException {
257251
}
258252

259253
@Test
260-
@Ignore("DEVSIX-1253")
261254
public void test06() throws IOException, InterruptedException {
262255
String outFile = destinationFolder + "parentTreeTest06.pdf";
263256
String cmpFile = sourceFolder + "cmp_parentTreeTest06.pdf";

0 commit comments

Comments
 (0)