I have some code that generates a PDF file programmatically and I need to append to it the existing file (to the end of generated one). Can somebody give an example or link?
Thank you
UPD#1: Actually I am looking for some piece of code of merging existing file and byte's array (of programmatically generated file)
Itext is the good solution.
To append , you need to read the already present data and write it to the next file.
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("D:/Text.pdf"));
String text1 = parser.getTextFromPage(3);
And before you write your code that generates a PDF file programmatically , you need to add text1.
Thanks for all repliers, I have found the solution:
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
writer.setPageEvent(new FooterGenerator());
document.open();
document.setMargins(MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM);
document.add(generateHeader());
document.add(generateContent());
appendTermsAndConditions(writer, document, context.getRealPath("/files/terms-and-conditions.pdf");
document.close();
protected void appendTermsAndConditions(PdfWriter writer, Document document, String fileName) throws IOException {
File f = new File(fileName);
if (f.exists()) {
PdfReader reader = new PdfReader(fileName);
PdfContentByte cb = writer.getDirectContent();
int pagesCount = reader.getNumberOfPages();
PdfImportedPage page;
for (int i = 0; i < pagesCount; i++) {
document.newPage();
page = writer.getImportedPage(reader, document.getPageNumber() + 1);
cb.addTemplate(page, 0, 0);
}
}
}