Adam and Thomas,

Thanks a lot for your pointers.

After searching through the mailing list archives, i came to realize that
there are no dedicated class to handle PNG/GIF images with alpha layer like
PDJPeg and PDCcitt classes. A suggested work around was to load PNG image
into a buffered image using ImageIO. If the loaded image has alpha channel,
PDJpeg attempts to do some processing, take a look at the PDJpeg
constructor;
    
*PDJpeg<http://www.pdfbox.org/javadoc/org/pdfbox/pdmodel/graphics/xobject/PDJpeg.html#PDJpeg(org.pdfbox.pdmodel.PDDocument,
java.awt.image.BufferedImage)>*(PDDocument doc, BufferedImage bi)

Going ahead with this approach, if the load image, say a sample user
signature file, is in any other color than black, the default handling of
alpha transparency in PDJpeg renders the image correctly and the image is
embedded as expected. I have an issue as mostly these input images will be
inked in black, nothing of the embedded image is visible on the pdf
document.

After some brain storming I came up with a routine to alter the image using
Java2D;
  public static BufferedImage alterImage(BufferedImage im) {
   ...
   // Using LookupOp filter from Java2D api to replace RGB pixels of
Color.BLACK
   // with RGB values (1,1,1).
 }

The altered image is then processed by PDJpeg and input image is displayed
correctly in the PDF document. I could not add a white background to the
image as the PDF document is not black on white background document.

Can you suggest a better approach? I am ready to spend more time to research
on this to do it the right way.

Adam, the message thread you suggested looks interesting, I will dive deeper
into it.

Regards,
Vinayak

On Thu, Sep 30, 2010 at 11:30 PM, Thomas Chojecki <[email protected]>wrote:

> Am 30.09.2010 19:35, schrieb Vinayak Mishra:
>
>> Hi all,
>>
> Hi
>
>
>  Forgive my naiveness but I am new to PdfBox, and trying my best to
>> understand the library. I am stuck at a point and need someone to show a
>> work around, pls see the mail below for details.
>>
>
> No problem, i think no one have a helping solution, so no one answer you.
>
>
>  I mailed this to the list a few days back, and receiving no response feels
>> weird. I am not sure if my mails are getting through.
>>
>
> The mailing list works great. If you subscribed the list correctly, you
> should also got your mail from the mailing list, as well as the other
> subscriber.
>
>  -----Original Message-----
>> From: Vinayak
>> Sent:  28/09/2010 12:42:24
>> Subject:  Re: Embed PNG/GIF image into existing PDF Document
>>
>>  I am loading PNG image using ImageIO and loading into PDJpeg using
>>> PDJpeg(PDDocument, BufferedImage).
>>>
>>
> You are trying to load a PNG with the PDJpeg which is only for JPGs.
>
> Some times ago i used iText for adding images like PNG and JPG in PDF
> documents and find out, that ...
> - JPGs are stored directly byte-by-byte in the document
> - PNGs are stored manipulated, i mean iText do something with the images
> befor storing it in the document.
>
> So i think it would be tricky to implement a similar class for PNGs.
> BUT you can convert the images with java on the fly in JPG like i did it
> and then store the image with the PDJpeg. If the image is transparent, you
> should add a white background color befor storing it.
>
>
>  Thank you very much.
>>> --
>>> Vinayak
>>>
>>
> regards
>
> Thomas
>

Reply via email to