0

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?

Jestino Sam
  • 526
  • 2
  • 12
  • 31
  • At first sight, you might think: *Hey, this Bruno-guy closed my question as a duplicate of a totally different question!* But give it a try. Throw away your `PdfCopy` code, and replace it with `PdfStamper` code. Use a very specific selection, such as `"1, 1, 2, 2"`. You will discover that the `selectPages()` method can also be used to repeat pages. – Bruno Lowagie Feb 05 '18 at 06:50
  • @BrunoLowagie Hi thanks for the selectPages() method. However, I am unable to repeat the pages even if I repeat the selectPages() method. Can you demonstrate with a small snippet? – Jestino Sam Feb 05 '18 at 07:11
  • It should be as simple as this: `PdfReader reader = new PdfReader(src); reader.selectPages("1, 1, 2, 2"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); stamper.close();` – Bruno Lowagie Feb 05 '18 at 07:37
  • @BrunoLowagie `reader = new PdfReader(inputStream); reader.selectPages("1,1,2,2"); PdfStamper stamper = new PdfStamper(reader, outputStream); stamper.close();` This is the code I have used. But still i am not able to see the duplicate page in my output PDF. Any idea as to why? – Jestino Sam Feb 05 '18 at 07:54
  • No, that works for me. Which version of iText are you using? Can you share the PDF document? – Bruno Lowagie Feb 05 '18 at 08:07
  • @BrunoLowagie I am using iText version 5. P.S. My source PDF is currently having only one page & I am trying to clone the single page to make it 2 pages. – Jestino Sam Feb 05 '18 at 08:54
  • Version 5 could be anything from 5.0.0 to 5.5.12. Obviously, if you only have 1 page, you have to adapt the code like this: `PdfReader reader = new PdfReader(inputStream); reader.selectPages("1,1"); PdfStamper stamper = new PdfStamper(reader, outputStream); stamper.close();` (but that goes without saying; that's just common sense). – Bruno Lowagie Feb 05 '18 at 09:13
  • @BrunoLowagie I am using version 5.5.12 of iText. I have used the same snippet that you have posted here. But still not able to clone the page in the document. – Jestino Sam Feb 05 '18 at 09:26
  • @BrunoLowagie I have my updated code there which I am currently using to clone the pages; however using the same code I am unable to clone the pages. If you don't mind, would just point out the mistake in the code? – Jestino Sam Feb 05 '18 at 09:36
  • I'll try to reproduce the problem. As for the first version of your own code, this is wrong: `for(int i = 0; i < 1; i++)` because it adds the page only once. If you want to add it twice, you need `for(int i = 0; i < 2; i++)`. – Bruno Lowagie Feb 05 '18 at 09:53
  • You're right. My example used an old version of iText. I installed the most recent version, and it doesn't behave as I expect. There's only one page in the resulting PDF. That's odd, something must have changed, but I don't know from which version on. I'll undo my duplicate vote. – Bruno Lowagie Feb 05 '18 at 10:04
  • There is even a 5.5.13 since last week. – Amedee Van Gasse Feb 05 '18 at 14:50

1 Answers1

1

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165