Jump to content

Merging PDF (locked files)


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

Recommended Posts

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

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This topic is 2088 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.