0

I am trying to concatente my selfmade text in itext with other already completed pdf. The problem is that when i concatenate two pdfs the second one starts from new page, he is not merging with the other pdf.

public static void main(String[] args) throws DocumentException, IOException {
    String[] files = new String[]{"FirstPdf.pdf","SecondPdf.pdf"};
    Document document = new Document();
    PdfCopy copy = new PdfCopy(document, new FileOutputStream("PdfConcatenation.pdf"));

    document.open();
    for (String file : files){
        PdfReader reader = new PdfReader(file);
        for (int i = 1; i <= reader.getNumberOfPages(); i++){
            // optionally write an if statement to include the page
            copy.addPage(copy.getImportedPage(reader, i));
        }
        copy.freeReader(reader);
        reader.close();
    }
    document.close();

}

i am expecting "SecondPdf" text to be straight after the "FirstPdf" text... enter image description here

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
  • This code is working for me. Could you clarify what is happening? Is PDFConcatenation.pdf not appearing? Is PDFConcatenation writing but putting FirstPdf twice? – ProgrammersBlock Dec 10 '17 at 13:13
  • The OP thinks that PDF is a Word processing format. It's not. A PDF is a PDL. Every page is self contained, and all content on a page is added at absolute positions. The OP expects the content to reflow, so that two half pages result in one full page. The OP needs a lot of education, more than can usually be provided in an answer on Stack Overflow. – Bruno Lowagie Dec 10 '17 at 15:52

0 Answers0