@@ -44,7 +44,6 @@ This file is part of the iText (R) project.
4444 */
4545package com .itextpdf .kernel .utils ;
4646
47- import com .itextpdf .kernel .PdfException ;
4847import com .itextpdf .kernel .pdf .PdfDocument ;
4948
5049import java .util .ArrayList ;
@@ -54,7 +53,7 @@ This file is part of the iText (R) project.
5453public class PdfMerger {
5554
5655 private PdfDocument pdfDocument ;
57- private List < AddedPages > pagesToCopy = new ArrayList <>() ;
56+ private boolean closeSrcDocuments ;
5857
5958 /**
6059 * This class is used to merge a number of existing documents into one;
@@ -65,69 +64,57 @@ public PdfMerger(PdfDocument pdfDocument){
6564 }
6665
6766 /**
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.
6982 * @param from - document, from which pages will be copied.
7083 * @param fromPage - start page in the range of pages to be copied.
7184 * @param toPage - end page in the range to be copied.
72- * @throws PdfException
85+ * @return this {@code PdfMerger} instance.
7386 */
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 );
7589 for (int pageNum = fromPage ; pageNum <= toPage ; pageNum ++){
76- enqueuePageToCopy ( from , pageNum );
90+ pages . add ( pageNum );
7791 }
92+ return merge (from , pages );
7893 }
7994
8095 /**
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.
82100 * @param from - document, from which pages will be copied.
83101 * @param pages - List of numbers of pages which will be copied.
84- * @throws PdfException
102+ * @return this {@code PdfMerger} instance.
85103 */
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 ();
89108 }
109+ return this ;
90110 }
91111
92112 /**
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.
95116 */
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 ();
132119 }
133120}
0 commit comments