Skip to content

Commit d856bfc

Browse files
Change defaults for PdfMerger; add flags to PdfSplitter to disable tags and outlines copying
Now by default tags and outlines in PdfMerger are copied even if destination document is not tagged and has no outlines.
1 parent 90cfdca commit d856bfc

File tree

11 files changed

+65
-24
lines changed

11 files changed

+65
-24
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,6 @@ public PdfCatalog setAdditionalAction(PdfName key, PdfAction action) {
149149
return this;
150150
}
151151

152-
/**
153-
* This flag determines if Outline tree of the document has been built via calling getOutlines method. If this flag is false all outline operations will be ignored
154-
*
155-
* @return state of outline mode.
156-
*/
157-
public boolean isOutlineMode() {
158-
return outlineMode;
159-
}
160-
161152
/**
162153
* This method sets a page mode of the document.
163154
* </p>
@@ -387,6 +378,15 @@ boolean hasOutlines() {
387378
return getPdfObject().containsKey(PdfName.Outlines);
388379
}
389380

381+
/**
382+
* This flag determines if Outline tree of the document has been built via calling getOutlines method. If this flag is false all outline operations will be ignored
383+
*
384+
* @return state of outline mode.
385+
*/
386+
boolean isOutlineMode() {
387+
return outlineMode;
388+
}
389+
390390
/**
391391
* This method removes all outlines associated with a given page
392392
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocume
10231023
toDocument.addPage(newPage);
10241024
}
10251025
pageInsertIndex++;
1026-
if (toDocument.getCatalog().isOutlineMode()) {
1026+
if (toDocument.hasOutlines()) {
10271027
List<PdfOutline> pageOutlines = page.getOutlines(false);
10281028
if (pageOutlines != null)
10291029
outlinesToCopy.addAll(pageOutlines);

kernel/src/main/java/com/itextpdf/kernel/utils/PdfMerger.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,38 @@ public class PdfMerger {
5454

5555
private PdfDocument pdfDocument;
5656
private boolean closeSrcDocuments;
57+
private boolean mergeTags;
58+
private boolean mergeOutlines;
5759

5860
/**
59-
* This class is used to merge a number of existing documents into one;
60-
* @param pdfDocument - the document into which source documents will be merged.
61+
* This class is used to merge a number of existing documents into one. By default, if source document
62+
* contains tags and outlines, they will be also copied to the destination document.
63+
* @param pdfDocument the document into which source documents will be merged.
6164
*/
6265
public PdfMerger(PdfDocument pdfDocument) {
66+
this(pdfDocument, true, true);
67+
}
68+
69+
/**
70+
* This class is used to merge a number of existing documents into one.
71+
* @param pdfDocument the document into which source documents will be merged.
72+
* @param mergeTags if true, then tags from the source document are copied even if destination document is not set as
73+
* tagged. Note, that if false, tag structure is still could be copied if the destination document
74+
* is explicitly marked as tagged with {@link PdfDocument#setTagged()}.
75+
* @param mergeOutlines if true, then outlines from the source document are copied even if in destination document
76+
* outlines are not initialized. Note, that if false, outlines are still could be copied if the
77+
* destination document outlines were explicitly initialized with {@link PdfDocument#initializeOutlines()}.
78+
*/
79+
public PdfMerger(PdfDocument pdfDocument, boolean mergeTags, boolean mergeOutlines) {
6380
this.pdfDocument = pdfDocument;
81+
this.mergeTags = mergeTags;
82+
this.mergeOutlines = mergeOutlines;
6483
}
6584

6685
/**
67-
* If set to <i>true</i> then passed to the <i>{@code PdfMerger#merge}</i> method source documents will be closed immediately after merging
68-
* specified pages into current document. If <i>false</i> - PdfDocuments are left open. Default value - <i>false</i>.
86+
* If set to <i>true</i> then passed to the <i>{@code PdfMerger#merge}</i> method source documents will be closed
87+
* immediately after merging specified pages into current document. If <i>false</i> - PdfDocuments are left open.
88+
* Default value - <i>false</i>.
6989
* @param closeSourceDocuments should be true to close pdf documents in merge method.
7090
* @return this {@code PdfMerger} instance.
7191
*/
@@ -102,6 +122,13 @@ public PdfMerger merge(PdfDocument from, int fromPage, int toPage) {
102122
* @return this {@code PdfMerger} instance.
103123
*/
104124
public PdfMerger merge(PdfDocument from, List<Integer> pages) {
125+
if (mergeTags && from.isTagged()) {
126+
pdfDocument.setTagged();
127+
}
128+
if (mergeOutlines && from.hasOutlines()) {
129+
pdfDocument.initializeOutlines();
130+
}
131+
105132
from.copyPagesTo(pages, pdfDocument);
106133
if (closeSrcDocuments) {
107134
from.close();

kernel/src/main/java/com/itextpdf/kernel/utils/PdfSplitter.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ This file is part of the iText (R) project.
5757
import java.util.Collections;
5858
import java.util.List;
5959
import java.util.Map;
60-
import java.util.TreeSet;
61-
import java.util.regex.Matcher;
62-
import java.util.regex.Pattern;
6360

6461
public class PdfSplitter {
6562

6663
private PdfDocument pdfDocument;
64+
private boolean preserveTagged;
65+
private boolean preserveOutlines;
6766

6867
/**
6968
* Creates a new instance of PdfSplitter class.
@@ -75,6 +74,26 @@ public PdfSplitter(PdfDocument pdfDocument) {
7574
throw new PdfException(PdfException.CannotSplitDocumentThatIsBeingWritten);
7675
}
7776
this.pdfDocument = pdfDocument;
77+
this.preserveTagged = true;
78+
this.preserveOutlines = true;
79+
}
80+
81+
/**
82+
* If original document is tagged, then by default all resultant document will also be tagged.
83+
* This could be changed with this flag - if set to false, resultant documents will be not tagged, even if
84+
* original document is tagged.
85+
*/
86+
public void setPreserveTagged(boolean preserveTagged) {
87+
this.preserveTagged = preserveTagged;
88+
}
89+
90+
/**
91+
* If original document has outlines, then by default all resultant document will also have outlines.
92+
* This could be changed with this flag - if set to false, resultant documents won't contain outlines, even if
93+
* original document had them.
94+
*/
95+
public void setPreserveOutlines(boolean preserveOutlines) {
96+
this.preserveOutlines = preserveOutlines;
7897
}
7998

8099
/**
@@ -234,9 +253,9 @@ protected PdfWriter getNextPdfWriter(PageRange documentPageRange) {
234253

235254
private PdfDocument createPdfDocument(PageRange currentPageRange) {
236255
PdfDocument newDocument = new PdfDocument(getNextPdfWriter(currentPageRange));
237-
if (pdfDocument.isTagged())
256+
if (pdfDocument.isTagged() && preserveTagged)
238257
newDocument.setTagged();
239-
if (pdfDocument.getCatalog().isOutlineMode())
258+
if (pdfDocument.hasOutlines() && preserveOutlines)
240259
newDocument.initializeOutlines();
241260
return newDocument;
242261
}

kernel/src/test/java/com/itextpdf/kernel/utils/PdfSplitterTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static void beforeClass() {
3939
public void splitDocumentTest01() throws IOException, InterruptedException {
4040
String inputFileName = sourceFolder + "iphone_user_guide.pdf";
4141
PdfDocument inputPdfDoc = new PdfDocument(new PdfReader(inputFileName));
42-
inputPdfDoc.initializeOutlines();
4342

4443
List<Integer> pageNumbers = Arrays.asList(30, 100);
4544

@@ -72,7 +71,6 @@ protected PdfWriter getNextPdfWriter(PageRange documentPageRange) {
7271
public void splitDocumentTest02() throws IOException, InterruptedException {
7372
String inputFileName = sourceFolder + "iphone_user_guide.pdf";
7473
PdfDocument inputPdfDoc = new PdfDocument(new PdfReader(inputFileName));
75-
inputPdfDoc.initializeOutlines();
7674

7775
new PdfSplitter(inputPdfDoc) {
7876
int partNumber = 1;
@@ -113,7 +111,6 @@ public void documentReady(PdfDocument pdfDocument, PageRange pageRange) {
113111
public void splitDocumentTest03() throws IOException, InterruptedException {
114112
String inputFileName = sourceFolder + "iphone_user_guide.pdf";
115113
PdfDocument inputPdfDoc = new PdfDocument(new PdfReader(inputFileName));
116-
inputPdfDoc.initializeOutlines();
117114

118115
PageRange pageRange1 = new PageRange().addPageSequence(4, 15).addSinglePage(18).addPageSequence(1, 2);
119116
PageRange pageRange2 = new PageRange().addSinglePage(99).addSinglePage(98).addPageSequence(70, 99);
@@ -149,7 +146,6 @@ public void splitDocumentByOutlineTest() throws IOException, InterruptedExceptio
149146

150147
String inputFileName = sourceFolder + "iphone_user_guide.pdf";
151148
PdfDocument inputPdfDoc = new PdfDocument(new PdfReader(inputFileName));
152-
inputPdfDoc.initializeOutlines();
153149
PdfSplitter splitter = new PdfSplitter(inputPdfDoc);
154150
List listTitles = new ArrayList();
155151
listTitles.add("Syncing iPod Content from Your iTunes Library");
@@ -165,7 +161,6 @@ public void splitDocumentByOutlineTest() throws IOException, InterruptedExceptio
165161
public void splitDocumentBySize() throws IOException, InterruptedException {
166162
String inputFileName = sourceFolder + "splitBySize.pdf";
167163
PdfDocument inputPdfDoc = new PdfDocument(new PdfReader(inputFileName));
168-
inputPdfDoc.initializeOutlines();
169164
PdfSplitter splitter = new PdfSplitter(inputPdfDoc) {
170165

171166
int partNumber = 1;

0 commit comments

Comments
 (0)