I try to create a PDF Document merging all my PDFs files and between each page of each file a fixed PDPage
// Create final PDF Document
PDDocument document = new PDDocument();
// List all pdf files input folder
String[] filenames;
File f = new File(context.repertoire_output_docone);
filenames = f.list();
// Get the PDF page
File cgv = new File(context.repertoire_advendio + "conf/CGV.pdf");
PDDocument doccgv = Loader.loadPDF(cgv);
PDPage cgvpage = doccgv.getPage(0);
// For each pdf file found
for (String filename : filenames)
{
// If name doens't match the template, skip
if (filename.startsWith("fact_") == false && filename.startsWith("agence_fact_") == false)
continue;
// Open current document
PDDocument doc = Loader.loadPDF(new File(context.repertoire_output_docone + filename));
// For each page of the current document
for (PDPage page : doc.getPages())
{
// Add page
document.addPage(page);
// Add fixed page
document.addPage(cgvpage);
}
}
// Save final document
document.save(context.repertoire_output_docone + "output/docone_" + context.id_legal_entity + "_" + context.input_invoice_date.replace("-", "") + ".pdf");
// Close final document
document.close();
This code produce a PDF with only full blank pages. It goes in the loops, I have the good number of pages in the final document (so the pages are added) but it's just blank pages.
Try using importPage
instead of addPage
.
importPage
's Javadoc says:
This will import and copy the contents from another location [...] If you are adding a page to this document from another document and want to copy the contents to this document's scratch file then use this method otherwise just use the
addPage(org.apache.pdfbox.pdmodel.PDPage)
method.