Jump to content

iText Scripts...


This topic is 3643 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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 

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

  • 3 months later...
  • Newbies

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

  • Like 1
Link to comment
Share on other sites

  • 2 years later...
  • Newbies

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?

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

  • 11 months later...

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
Link to comment
Share on other sites

  • 3 months later...

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This topic is 3643 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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.