Jump to content

iText with ScriptMaster


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

Recommended Posts

Hi,

 

here is a PDF Spliter

// iText_SplitPDF ( pathToSrc ; pathToDest )
// 2012-07-05 by clem
//
// NOTE: To be properly numbered, the output file name must contain the format string %d
// ex: pathToDest = C:/Path/To/mySplittedPortableDoc_page%d.pdf 
//	   result => C:/Path/To/mySplittedPortableDoc_page1.pdf etc…

import com.itextpdf.text.Document
import com.itextpdf.text.DocumentException
import com.itextpdf.text.pdf.PdfCopy
import com.itextpdf.text.pdf.PdfReader

try{
    def reader = new PdfReader(pathToSrc)
    def document
    def copy
    int n = reader.numberOfPages

    n.times{
        document = new Document()
        copy = new PdfCopy(document, new FileOutputStream(String.format( pathToDest, it + 1)))
        document.open()
        copy.addPage copy.getImportedPage(reader, it + 1)
        document.close()
    }

     reader.close()
     return true

}  catch (IOException ioe){
    return "ERROR: $ioe.message"

} catch (DocumentException de){
    return "ERROR: $de.message"
}

and you'll find a PDF Merger here :  fmforums.com/forum/topic/73597-itext-scripts/

Link to comment
Share on other sites

Clem

 

you now need to explicitly close the reader 

so

document.close()

reader.close()

 

also this uses the simpler but less useful class

from their docs.>>

PdfSmartCopy has the same functionality as PdfCopy, but when resources (such as fonts, images,...) are encountered, a reference to these resources is saved in a cache, so that they can be reused. This requires more memory, but reduces the file size of the resulting PDF document.

Link to comment
Share on other sites

hi John,

 

... my bad ... I just forgot to copy the "finally" clause.

 

 

also this uses the simpler but less useful class

from their docs.>>

PdfSmartCopy has the same functionality as PdfCopy, but when resources (such as fonts, images,...) are encountered, a reference to these resources is saved in a cache, so that they can be reused. This requires more memory, but reduces the file size of the resulting PDF document.

... Sir, yes Sir !! …I totally agree with you as long as we're dealing with concatenating PDFs, but in current case we're splitting them and all resources are needed.

Link to comment
Share on other sites

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