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?