Jestino Sam
Jestino Sam

Reputation: 602

iText - How to Clone a page of the document?

I am using iText 5 API to clone the page of a PDF. My requirement is as follows: I have a document named "Test" which has 2 different pages. I need to clone the each pages i.e. if the test document is having two pages, i need to clone the first page & then the second page which in total makes 4 pages in the same Test document. I need this setup using iText API. I have tried out some code but it's giving me some exception & also doesn't satisfy my requirement.

Code:

public void clonePageOfPdf() throws IOException, DocumentException{
        Document doc = null;
        PdfReader reader = null;
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;

        //Assign
        inputStream = new FileInputStream(new File(sourcefile));
        outputStream = new FileOutputStream(new File(destfile));

        doc = new Document();
        PdfCopy copy = new PdfSmartCopy(doc, outputStream);
        doc.open();
        reader = new PdfReader(inputStream);

        for(int page = 0; page < reader.getNumberOfPages(); page++){
            PdfImportedPage importedPage = copy.getImportedPage(reader, page);

            //Duplicate
            for(int i = 0; i < 1; i++){
                copy.addPage(importedPage);
            }
        }
        copy.freeReader(reader);

        reader.close();
        doc.close();
        outputStream.close();
        inputStream.close();
    }
}

Please help me out with this.

Updated Code

public void clonePageOfPdf() throws IOException, DocumentException{
        //Document doc = null;
        PdfReader reader = null;
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;

        //Assign
        inputStream = new FileInputStream(new File(sourceFile));
        outputStream = new FileOutputStream(new File(destFile));

        //doc = new Document();

        reader = new PdfReader(inputStream);
        reader.selectPages("1,1");
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        stamper.close();

        reader.close();
        //doc.close();
        outputStream.close();
        inputStream.close();
    }

Can anyone help me with the updated code?

Upvotes: 0

Views: 4695

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

For some reason, the selectPages() approach I suggested no longer works with recent versions of iText 5 (it worked with the older ones), so I looked at your original code, and I adapted it like this:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfReader reader = new PdfReader(src);
    PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
    document.open();
    for(int page = 1; page <= reader.getNumberOfPages(); page++) {
        PdfImportedPage importedPage = copy.getImportedPage(reader, page);
        for(int i = 0; i < 2; i++) {
            copy.addPage(importedPage);
        }
    }
    document.close();
    reader.close();
}

These are the changes I made:

for(int page = 1; page <= reader.getNumberOfPages(); page++)

Page numbers start at page 1, not at page 0, and that has an impact on your for-loop.

for(int i = 0; i < 2; i++)

If you want to add 2 pages instead of one, you need i < 2 instead of i < 1.

Upvotes: 1

Related Questions