Baars, Peter wrote:
Hello,

First of all I want to complement you on the great library you guys created. I really appreciate it!

I would like to report a bug when trying to merge a large number of PDF documents. I created a simple testcase and added the code to this mail.

I don't understand your code and I can't reproduce the "bug" you
mention. You are referring to a bug that was fixed in iTextSharp,
not in iText. Although I don't like the way you concatenate the
files (using PdfWriter instead of PdfCopy or PdfSmartCopy), I threw
away all the ballast from your code and made it simpler. Please
adapt the example (without making it more complex) so that the
problem can be reproduced.
best regards,
Bruno
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

/**
 * PDF utility class
 * Used for a number of generic functions for handling or creating PDF files.
 */
public class PdfUtil {
        
        /**
         * Merge multiple PDF's into one
         * @param inputstreams collection of pdf files as a inputstream
         * @return a merged PDF file as a byte[]
         * @throws IOException problem reading input files.
         * @throws DocumentException problem merging files.
         */
        public void mergePdfFiles(String[] files) 
                throws IOException, DocumentException  
        {
                if (files == null || files.length == 0) return;
                Document doc = new Document();
                PdfWriter writer = PdfWriter.getInstance(doc, new 
FileOutputStream("test.pdf"));
                doc.open();
                PdfContentByte cb = writer.getDirectContent();
                for (int i = 0; i < files.length; i++) {
                        System.err.println(files[i] + " " + i);
                        PdfReader reader = new PdfReader(files[i]);
                        for (int x=1; x<= reader.getNumberOfPages(); x++) {
                                doc.newPage();
                                PdfImportedPage page = 
writer.getImportedPage(reader, x);

                                //determine if the page needs to be rotated
                                int rotation =  reader.getPageRotation(x);
                                if (rotation == 90 || rotation == 270)
                                {
                                        cb.addTemplate(page, 0,-1f, 1f, 0, 0, 
reader.getPageSizeWithRotation(x).getHeight());
                                } else {
                                        cb.addTemplate(page, 1f, 0, 0, 1f, 0, 
0);
                                }
                                
                        }
                }
                doc.close();
        }
        
        public static void main(String[] args) throws Exception{
                String[] files = new String[100];
                for (int i = 0; i < 50; i++) {
                        files[i * 2] = "test1.pdf";
                        files[i * 2 + 1] = "test2.pdf";
                }
                PdfUtil util = new PdfUtil();
                util.mergePdfFiles(files);
                System.out.println("finished");
        }
        
        
}

Attachment: test1.pdf
Description: Adobe PDF document

Attachment: test2.pdf
Description: Adobe PDF document

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Do you like iText?
Buy the iText book: http://www.1t3xt.com/docs/book.php
Or leave a tip: https://tipit.to/itexttipjar

Reply via email to