Ocean West Posted August 30, 2010 Posted August 30, 2010 I am trying to get iText working and from some reason my code is generating an error. // The following code is groovy code to be used with the ScriptMaster plugin // The code requires the iText jar library and will merge any number of PDF files // into one PDF file // @param files = return separated list of absolute paths to PDF files // @param output = absolute path for new pdf file (including file name) 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
Ocean West Posted August 30, 2010 Author Posted August 30, 2010 after trial & error and reloading the latest version of iText 5.0.4 it seems to be working with this code. // The following code is groovy code to be used with the ScriptMaster plugin // The code requires the iText jar library and will merge any number of PDF files // into one PDF file // @param files = return separated list of absolute paths to PDF files // @param output = absolute path for new pdf file (including file name) 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
john renfrew Posted September 2, 2010 Posted September 2, 2010 Same thing but a bit simpler and more Groovy // PDFadd( filelist ; fm_FileOut ) // JR 05_03_10 // v2 // concatenate PDf files import com.itextpdf.text.Document import com.itextpdf.text.pdf.PdfCopy import com.itextpdf.text.pdf.PdfReader files = filelist.split("n") // step 1 document = new Document() // step 2 copy = new PdfCopy(document, new FileOutputStream( fm_FileOut )) // step 3 document.open() // step 4 PdfReader reader int n // loop over the documents you want to concatenate files.each { item -> reader = new PdfReader("${item}") n = reader.getNumberOfPages() for (int page = 0; page < n; ) { copy.addPage(copy.getImportedPage(reader, ++page)) } } // step 5 document.close() return true 1
Newbies Mike Wallen Posted December 22, 2010 Newbies Posted December 22, 2010 Hi, I am looking to incorporate this with a run-time I have built. I can get this to work with one file, but am not sure how to ensure that it works with multiple files. How do I list the files (with path) so that the process merges all files into one? This step appears to be where the process works with the multiple files. files = filelist.split("n") When I provide a list separated by a carriage return or ";" the function errors: java.io.IOException: /Users/mike/Desktop/VBBEUPLOAD/PDF/Test_AP1.pdf /Users/mike/Desktop/VBBEUPLOAD/PDF/Test_AP2.pdf /Users/mike/Desktop/VBBEUPLOAD/PDF/Test_AP3.pdf not found as file or resource. How do I provide a list of files so the function can process them without an error? Looking forward to a response. Mike 1
john renfrew Posted December 22, 2010 Posted December 22, 2010 That will be my mistake OR the formatting gets stripped should be filelist.split(" backslash n " )
Newbies Mike Wallen Posted December 22, 2010 Newbies Posted December 22, 2010 Thanks for the prompt response. That did the trick. Thanks. Mike
Newbies becky123 Posted February 19, 2013 Newbies Posted February 19, 2013 Hello, I am trying to use the PDFadd function. Until last night i managed to do so, but now I keep getting an error :"java.io.FileNotFoundException: file:C:UsersBeckyDesktopLogiciel SoloreaFichiers base SoloreaContrat Global Solorea du client laksalksalkslk tututu.pdf (The filename, directory name, or volume label syntax is incorrect)" I am using two calculated paths as input variables. I checked the paths and corrected all the mistakes I think. I really don t know what to do anymore. Maybe smn could help me?
clemhoff Posted February 20, 2013 Posted February 20, 2013 Hello, SM/Groovy is expecting a System path, not a path "à la FileMaker". Just get rid of the "file:/" prefix. Note also that in your path you can use slashes instead of backslashes. Windows knows how to interpret them since Win98.
john renfrew Posted February 12, 2014 Posted February 12, 2014 Have made the function a little more Groovy and updated best practice of closing reader // PDFadd( filelist ; fm_FileOut ) // JR 14_02_12 // v2.1 // concatenate PDF files import com.itextpdf.text.Document import com.itextpdf.text.pdf.PdfCopy import com.itextpdf.text.pdf.PdfReader files = filelist.split('n') // step 1 document = new Document() // step 2 copy = new PdfCopy(document, new FileOutputStream( fm_FileOut )) // step 3 document.open() // step 4 PdfReader reader int n // loop over the documents you want to concatenate files.each { item -> reader = new PdfReader("${item}") n = reader.getNumberOfPages() for (page in 0.<n ) { copy.addPage(copy.getImportedPage(reader, page)) } reader.close() } // step 5 document.close() return true
Tim Anderson Posted May 28, 2014 Posted May 28, 2014 Hi John, Hope you are well and busy. Think you have missed a '.' and a '++' as below // PDFadd( filelist ; fm_FileOut ) // JR 14_02_12 // v2.1 // concatenate PDF files import com.itextpdf.text.Document import com.itextpdf.text.pdf.PdfCopy import com.itextpdf.text.pdf.PdfReader files = filelist.split('n') // step 1 document = new Document() // step 2 copy = new PdfCopy(document, new FileOutputStream( fm_FileOut )) // step 3 document.open() // step 4 PdfReader reader int n // loop over the documents you want to concatenate files.each { item -> reader = new PdfReader("${item}") n = reader.getNumberOfPages() for (page in 0..<n ) { copy.addPage(copy.getImportedPage(reader, ++page)) } reader.close() } // step 5 document.close() return true Cheers Tim
john renfrew Posted May 28, 2014 Posted May 28, 2014 Tim... right about the '.' but oddly the '++' makes no difference - as there is no page '0' it should in fact be for ( page in 1..n ) with copy.addPage(copy.getImportedPage(reader, page)) john
Recommended Posts
This topic is 3843 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now