July 10, 20187 yr Adapted file from http://leodicroce.com/combine-pdfs-without-using-fm-append/ Changes from the demo file: -Latest Scriptmaster version instead of 4.32 -Loaded 2 additional Jars (bcprov-jdk15on-160 and bcpkix-jdk15on-160) The merge PDF function RegisterGroovy( "MergePDF( files ; output )" ; "import java.io.FileOutputStream;¶ import java.util.ArrayList;¶ import java.util.List;¶ ¶ import com.itextpdf.text.Document;¶ import com.itextpdf.text.pdf.PdfReader;¶ import com.itextpdf.text.pdf.PdfCopy;¶ import com.itextpdf.text.pdf.PdfImportedPage;¶ ¶ try {¶ String[] inFiles = files.split(\"\n\");¶ int f = 0;¶ int pageOffset = 0;¶ String outFile = output;¶ Document document = null;¶ PdfCopy writer = null;¶ ¶ while (f < inFiles.length) {¶ // Create a reader for the next document¶ PdfReader reader = new PdfReader(inFiles[f]);¶ reader.consolidateNamedDestinations();¶ ¶ // Retrieve the total number of pages¶ int n = reader.getNumberOfPages();¶ ¶ pageOffset += n;¶ ¶ // Create the master document¶ if (f == 0) {¶ // step 1: Create the document-object¶ document = new Document(reader.getPageSizeWithRotation(1));¶ // step 2: Create the writer that listens to the document¶ writer = new PdfCopy(document, new FileOutputStream(outFile));¶ // step 3: Open the document¶ document.open();¶ }¶ // step 4: Add content¶ PdfImportedPage page;¶ for (int i = 0; i < n; ) {¶ ++i;¶ page = writer.getImportedPage(reader, i);¶ writer.addPage(page);¶ }¶ ¶ f++;¶ }¶ ¶ document.close();¶ return outFile;¶ } catch(e) {¶ return 'ERROR'¶ }" ) The function generates the pdf correctly but locks all the files except for the output file so I can't delete them. I added writer.flush();¶ writer.close();¶ It did not change anything. Tried to add reader.close() but it generates an error. Any idea ? Edited July 10, 20187 yr by sfpx
July 10, 20187 yr which version of iText??? There's abetter method using com.itextpdf.text.pdf.PdfConcatenate you need to close the reader file at the end of the while loop...
July 10, 20187 yr Author Just now, john renfrew said: you need to close the reader file at the end of the while loop... yeah I just figured that out. Thanks for the info about the concatenate function
July 10, 20187 yr There is also a way to do this without the export step, but right from the container field, but SM is a little broken on this method on high sierra at the moment. When thats fixed I'll get back on how.
Create an account or sign in to comment