Example usage for org.apache.pdfbox.multipdf PDFCloneUtility cloneForNewDocument

List of usage examples for org.apache.pdfbox.multipdf PDFCloneUtility cloneForNewDocument

Introduction

In this page you can find the example usage for org.apache.pdfbox.multipdf PDFCloneUtility cloneForNewDocument.

Prototype

COSBase cloneForNewDocument(Object base) throws IOException 

Source Link

Document

Deep-clones the given object for inclusion into a different PDF document identified by the destination parameter.

Usage

From source file:pdfsplicer.SplicerModel.java

License:Open Source License

/**
 * Create the new PDF, and save it.//from  w  ww.ja va 2  s  .c  o m
 * 
 * @param saveFile the file to save it as
 * @throws IOException if it cannot save the file
 */
public void makeFinalizedPDF(File saveFile) throws IOException {

    PDDocument doc = null;
    PDDocument newdoc = new PDDocument();

    for (int i = 0; i < pageEntryPDFList.size(); ++i) {
        doc = pdfList.get(pageEntryPDFList.get(i));

        if (doc.isEncrypted()) {
            System.out.println("Error: Encrypted PDF");
            System.exit(1);
        }

        List<Integer> pRange = pageRangeList.get(i);
        PDFCloneUtility pdfCloner = new PDFCloneUtility(newdoc);
        for (int pNum : pRange) {
            PDPage page = doc.getPage(pNum - 1);
            COSDictionary clonedDict = (COSDictionary) pdfCloner.cloneForNewDocument(page);
            newdoc.addPage(new PDPage(clonedDict));
        }
    }

    newdoc.save(saveFile);
    if (newdoc != null) {
        newdoc.close();
    }
}