@@ -44,7 +44,6 @@ This file is part of the iText (R) project.
44
44
*/
45
45
package com .itextpdf .kernel .utils ;
46
46
47
- import com .itextpdf .kernel .PdfException ;
48
47
import com .itextpdf .kernel .pdf .PdfDocument ;
49
48
50
49
import java .util .ArrayList ;
@@ -54,7 +53,7 @@ This file is part of the iText (R) project.
54
53
public class PdfMerger {
55
54
56
55
private PdfDocument pdfDocument ;
57
- private List < AddedPages > pagesToCopy = new ArrayList <>() ;
56
+ private boolean closeSrcDocuments ;
58
57
59
58
/**
60
59
* This class is used to merge a number of existing documents into one;
@@ -65,69 +64,57 @@ public PdfMerger(PdfDocument pdfDocument){
65
64
}
66
65
67
66
/**
68
- * This method adds pages from the source document to the List of pages which will be merged.
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>.
69
+ * @param closeSourceDocuments should be true to close pdf documents in merge method.
70
+ * @return this {@code PdfMerger} instance.
71
+ */
72
+ public PdfMerger setCloseSourceDocuments (boolean closeSourceDocuments ) {
73
+ this .closeSrcDocuments = closeSourceDocuments ;
74
+ return this ;
75
+ }
76
+
77
+ /**
78
+ * This method merges pages from the source document to the current one.
79
+ * <br/><br/>
80
+ * If <i>closeSourceDocuments</i> flag is set to <i>true</i> (see {@link #setCloseSourceDocuments(boolean)}),
81
+ * passed {@code PdfDocument} will be closed after pages are merged.
69
82
* @param from - document, from which pages will be copied.
70
83
* @param fromPage - start page in the range of pages to be copied.
71
84
* @param toPage - end page in the range to be copied.
72
- * @throws PdfException
85
+ * @return this {@code PdfMerger} instance.
73
86
*/
74
- public void addPages (PdfDocument from , int fromPage , int toPage ) {
87
+ public PdfMerger merge (PdfDocument from , int fromPage , int toPage ) {
88
+ List <Integer > pages = new ArrayList <>(toPage - fromPage );
75
89
for (int pageNum = fromPage ; pageNum <= toPage ; pageNum ++){
76
- enqueuePageToCopy ( from , pageNum );
90
+ pages . add ( pageNum );
77
91
}
92
+ return merge (from , pages );
78
93
}
79
94
80
95
/**
81
- * This method adds pages from the source document to the List of pages which will be merged.
96
+ * This method merges pages from the source document to the current one.
97
+ * <br/><br/>
98
+ * If <i>closeSourceDocuments</i> flag is set to <i>true</i> (see {@link #setCloseSourceDocuments(boolean)}),
99
+ * passed {@code PdfDocument} will be closed after pages are merged.
82
100
* @param from - document, from which pages will be copied.
83
101
* @param pages - List of numbers of pages which will be copied.
84
- * @throws PdfException
102
+ * @return this {@code PdfMerger} instance.
85
103
*/
86
- public void addPages (PdfDocument from , List <Integer > pages ) {
87
- for (Integer pageNum : pages ){
88
- enqueuePageToCopy (from , pageNum );
104
+ public PdfMerger merge (PdfDocument from , List <Integer > pages ) {
105
+ from .copyPagesTo (pages , pdfDocument );
106
+ if (closeSrcDocuments ) {
107
+ from .close ();
89
108
}
109
+ return this ;
90
110
}
91
111
92
112
/**
93
- * This method gets all pages from the List of pages to be copied and merges them into one document.
94
- * @throws PdfException
113
+ * Closes the current document. It is a complete equivalent of calling {@code PdfDocument#close} on the PdfDocument
114
+ * passed to the constructor of this PdfMerger instance. This means that it is enough to call <i>close</i> either on
115
+ * passed PdfDocument or on this PdfMerger instance, but there is no need to call them both.
95
116
*/
96
- public void merge () {
97
- for (AddedPages addedPages : pagesToCopy ) {
98
- addedPages .from .copyPagesTo (addedPages .pagesToCopy , pdfDocument );
99
- }
100
- }
101
-
102
- /**
103
- * This method adds to the List of pages to be copied with given page.
104
- * Pages are stored along with their documents.
105
- * If last added page belongs to the same document as the new one, new page is added to the previous {@code AddedPages} instance.
106
- * @param from - document, from which pages will be copied.
107
- * @param pageNum - number of page to be copied.
108
- * @throws PdfException
109
- */
110
- private void enqueuePageToCopy (PdfDocument from , int pageNum ) {
111
- if (!pagesToCopy .isEmpty ()) {
112
- AddedPages lastAddedPagesEntry = pagesToCopy .get (pagesToCopy .size () - 1 );
113
- if (lastAddedPagesEntry .from == from ) {
114
- lastAddedPagesEntry .pagesToCopy .add (pageNum );
115
- } else {
116
- pagesToCopy .add (new AddedPages (from , pageNum ));
117
- }
118
- } else {
119
- pagesToCopy .add (new AddedPages (from , pageNum ));
120
- }
121
- }
122
-
123
- static class AddedPages {
124
- public AddedPages (PdfDocument from , int pageNum ) {
125
- this .from = from ;
126
- this .pagesToCopy = new ArrayList <>();
127
- this .pagesToCopy .add (pageNum );
128
- }
129
-
130
- PdfDocument from ;
131
- List <Integer > pagesToCopy ;
117
+ public void close () {
118
+ pdfDocument .close ();
132
119
}
133
120
}
0 commit comments