Never mind. My bad. I fixed the bugs. Many thanks!

            Document document = new Document();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, baos);
            document.open();
            Image image = Image.getInstance("C:\\Sunset.jpg");
            document.newPage();
            document.add(image);
            document.close();
            writer.close();

            PdfReader reader2 = new PdfReader(baos.toByteArray());
            Document document2 = new
Document(reader2.getPageSizeWithRotation(1));
            PdfCopy copy = new PdfCopy(document2, new
FileOutputStream("C:\\test1.pdf"));
            document2.open();
            PdfImportedPage page = copy.getImportedPage(reader2, 1);
            copy.addPage(page);
            document2.close();
            copy.close();
            baos.close();


On Fri, Sep 16, 2011 at 1:45 PM, mingqiang yu <[email protected]> wrote:
> Thanks for the help. I will read the chapter you mentioned and will
> get the book to read the chapter about collection as well. I'm quite
> new to itext and I'm sure it will help me understand more about itext.
>
> In the mean time, I'm trying to see if I can follow the 1st solution
> you mentioned with simplified codes (assume all files are in file
> system).
>
> // when I see an image, create a temporary outstream:
> Document document = new Document();
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> PdfWriter writer = PdfWriter.getInstance(document, baos);
> document.open();
> Image image = Image.getInstance("C:\\Sunset.jpg");
> document.newPage();
> document.add(image);
> document.close();
> writer.close();
>
> So at this point, the baos should hold the image.
>
> Document document2 = new Document();
> PdfCopy copy = new PdfCopy(document2, new FileOutputStream("C:\\test1.pdf"));
> PdfReader reader2 = new PdfReader(baos.toByteArray());
> PdfImportedPage page = copy.getImportedPage(reader2, 1);
> copy.addPage(page);
> document2.close();
> copy.close();
> baos.close();
>
> But I kept getting java.lang.NullPointerException at copy.addPage(page);
>
> at com.itextpdf.text.pdf.PdfWriter.getPageReference(PdfWriter.java:992)
>        at com.itextpdf.text.pdf.PdfWriter.getCurrentPage(PdfWriter.java:1010)
>        at com.itextpdf.text.pdf.PdfCopy.addPage(PdfCopy.java:356)
>        at MergePDF.main(MergePDF.java:100)
>
> I checked the page variable, it's not null. Is there something I did wrong?
>
> Thanks a lot!
>
> 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.
>
> On Fri, Sep 16, 2011 at 12:54 PM, 1T3XT BVBA <[email protected]> wrote:
>> 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&reg; 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
>>
>

------------------------------------------------------------------------------
BlackBerry&reg; 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

Reply via email to