Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function that can use iText to concatenate / merge pdfs together - causing some issues

Tags:

java

pdf

itext

I'm using the following code to merge PDFs together using iText:

public static void concatenatePdfs(List<File> listOfPdfFiles, File outputFile) throws DocumentException, IOException {
        Document document = new Document();
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        for (File inFile : listOfPdfFiles) {
            PdfReader reader = new PdfReader(inFile.getAbsolutePath());
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                document.newPage();
                PdfImportedPage page = writer.getImportedPage(reader, i);
                cb.addTemplate(page, 0, 0);
            }
        }
        outputStream.flush();
        document.close();
        outputStream.close();
    }

This usually works great! But once and a while, it's rotating some of the pages by 90 degrees? Anyone ever have this happen?

I am looking into the PDFs themselves to see what is special about the ones that are being flipped.

like image 506
Nicholas DiPiazza Avatar asked Apr 14 '14 14:04

Nicholas DiPiazza


People also ask

What is iText used for?

iText is a library for creating and manipulating PDF files in Java and.NET. iText was written by Bruno Lowagie.

How do you concatenate PDF files?

Open Acrobat to combine files: Open the Tools tab and select "Combine files." Add files: Click "Add Files" and select the files you want to include in your PDF. You can merge PDFs or a mix of PDF documents and other files.

Is PDF merge and split safe?

It allows you to merge and split instantly, real-time to the PDF file. You don't need to upload PDFs to any server. It's very safe, and guarantees privacy.

What is Pdfmerge?

Allows to merge PDF files with a simple drag and drop interface. This WebApp provides a simple way to merge PDF files. You can either select the files you want to merge from you computer or drop them on the app using drag and drop. After that use drag and drop to bring the files in the desired order.


2 Answers

There are errors once in a while because you are using the wrong method to concatenate documents. Please read chapter 6 of my book and you'll notice that using PdfWriter to concatenate (or merge) PDF documents is wrong:

  • You completely ignore the page size of the pages in the original document (you assume they are all of size A4),
  • You ignore page boundaries such as the crop box (if present),
  • You ignore the rotation value stored in the page dictionary,
  • You throw away all interactivity that is present in the original document, and so on.

Concatenating PDFs is done using PdfCopy, see for instance the FillFlattenMerge2 example:

Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
PdfReader reader;
String line = br.readLine();
// loop over readers
    // add the PDF to PdfCopy
    reader = new PdfReader(baos.toByteArray());
    copy.addDocument(reader);
    reader.close();
// end loop
document.close();

There are other examples in the book.

like image 110
Bruno Lowagie Avatar answered Oct 04 '22 03:10

Bruno Lowagie


In case anyone is looking for it, using Bruno Lowagie's correct answer above, here is the version of the function that does not seem to have the page flipping issue i described above:

public static void concatenatePdfs(List<File> listOfPdfFiles, File outputFile) throws DocumentException, IOException {
        Document document = new Document();
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        PdfCopy copy = new PdfSmartCopy(document, outputStream);
        document.open();
        for (File inFile : listOfPdfFiles) {
            PdfReader reader = new PdfReader(inFile.getAbsolutePath());
            copy.addDocument(reader);
            reader.close();
        }
        document.close();
}
like image 32
Nicholas DiPiazza Avatar answered Oct 04 '22 02:10

Nicholas DiPiazza