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.

Merging PDF (locked files)

Featured Replies

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 by sfpx

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

  • 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

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

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.