@@ -178,16 +178,24 @@ protected final void action(Action ruleAction, NodeRef actionedUponNodeRef, Cont
178178
179179 // split the PDF and put the pages in a list
180180 Splitter splitter = new Splitter ();
181- // Need to adjust the input value to get the split at the right page
182- splitter .setSplitAtPage (insertAt - 1 );
183181
184182 // Split the pages
185183 List <PDDocument > pdfs = splitter .split (pdf );
186184
187185 // Build the output PDF
188186 PDFMergerUtility merger = new PDFMergerUtility ();
189- merger .appendDocument ((PDDocument )pdfs .get (0 ), insertContentPDF );
190- merger .appendDocument ((PDDocument )pdfs .get (0 ), (PDDocument )pdfs .get (1 ));
187+
188+ PDDocument newDocument = new PDDocument ();
189+
190+ for (int i = 0 ; i < pdfs .size (); i ++) {
191+
192+ if (i == insertAt -1 ) {
193+ merger .appendDocument (newDocument , insertContentPDF );
194+ }
195+
196+ merger .appendDocument (newDocument , (PDDocument )pdfs .get (i ));
197+ }
198+
191199 merger .setDestinationFileName (options .get (PARAM_DESTINATION_NAME ).toString ());
192200 merger .mergeDocuments ();
193201
@@ -200,13 +208,14 @@ protected final void action(Action ruleAction, NodeRef actionedUponNodeRef, Cont
200208 String fileName = options .get (PARAM_DESTINATION_NAME ).toString ();
201209 Boolean inplace = Boolean .valueOf (String .valueOf (options .get (PARAM_INPLACE )));
202210
203- PDDocument completePDF = ( PDDocument ) pdfs . get ( 0 ) ;
211+ PDDocument completePDF = newDocument ;
204212
205213 completePDF .save (tempDir + "" + File .separatorChar + fileName + FILE_EXTENSION );
206214
207215 try
208216 {
209217 completePDF .close ();
218+ newDocument .close ();
210219 }
211220 catch (IOException e )
212221 {
0 commit comments