On 16/09/2011 17:53, mingqiang yu wrote: > Hi, > > Here is the problem I need to solve: > > I have a list of byte arrays which represents list of files. I need to > combine them into one pdf. They can be pdf, images, or information I > need to put into the new pdf.
Aha, go to http://affiliate.manning.com/idevaffiliate.php?id=223_212 There's a table "Resources" with a column "Downloads". Download chapter 6 (it's available for free), and you'll learn all about manipulating existing PDFs. > I cannot use PdfWriter because some pdfs contain not just content but > annotations That's also explained in chapter 6. > So, I have to use PdfCopy. Yes. > It works fine for any byte array which > represents pdf file. But for image files, I don't know how to put them > in. Here is what I came out but I kept getting > "PDF header signature not found." error: Er... image files are JPG, PNG, GIF,... Does it surprise you that these files don't have a PDF header signature? > Document itextDoc = new Document(PageSize.LETTER, 0, 0, 0, 0); > PdfCopy pdfCopy = new PdfCopy(itextDoc, fos); > FileOutputStream fos = new FileOutputStream(fileName); ??? You are using the variable fos before you declare it. This code doesn't compile, does it? > // problem happens here because nothing is in pdfCopy yet. > PdfReader reader = new PdfReader(pdfCopy.getDirectContent().toPdf(pdfCopy)); ??? What are you trying to achieve? The above line is wrong for many different reasons! > itextDoc.open(); > > > Image image =Image.getInstance(r.getData()); // get byte array data of > an image (say, png) > itextDoc.setMargins(10, 10, 10, 10); > image.scaleToFit(ITEXT_PDF_WIDTH - 20, ITEXT_PDF_HEIGHT - 20); Why are you creating a PdfStamper instance? > ByteArrayOutputStream baos = new ByteArrayOutputStream(); > PdfStamper stamper = new PdfStamper(reader, baos); > PdfContentByte content = stamper.getOverContent(reader.getNumberOfPages()); > content.addImage(image); > stamper.close(); > > reader = new PdfReader(baos.toByteArray()); > pdfCopy.addPage(pdfCopy.getImportedPage(reader, 1)); > pdfCopy.freeReader(reader); > baos.close(); > > Has anyone done this process before? Of course: plenty of people have achieved what you want to do. There are two solutions: Solution A: Use PdfCopy, and: [1] when you encounter an image, create a single-page PDF using PdfWriter in memory, and use that PDF. [2] when you encounter a PDF (or when you've just created a single-page PDF), create a PdfReader instance, and add PdfImportedPage objects to PdfCopy. Solution B: combine the different kinds of files AS-IS into a portable collection. Portable collections are explained in chapter 16 of the book. That chapter isn't available for free. ------------------------------------------------------------------------------ BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA http://p.sf.net/sfu/rim-devcon-copy2 _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
