Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

iText Scripts...

Featured Replies

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 

  • Author

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 

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

  • 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

That will be my mistake OR the formatting gets stripped

should be filelist.split(" backslash n " )

  • Newbies

Thanks for the prompt response.

That did the trick. Thanks.

Mike

  • 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?

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

  • 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
  • 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

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

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.