You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Ad...@swmc.com on 2010/10/01 00:59:21 UTC

Re: Embed PNG/GIF image into existing PDF Document

Replacing the the transparent pixels with white will work as long as the 
background is white, but it doesn't work out too well if the background is 
any other color.  It also doesn't deal with things like partial 
transparency (e.g. 50% alpha).  So, the PDJPeg "works" but in a limited 
fashion.  It sounds like the best solution would be to see if you can get 
the PDPixelMap working.  I'm not familiar with it and I don't really have 
enough time to look into it and see what's wrong, but I'd imagine it'd be 
easy to fix since it (presumably) worked at some point in time.

---- 
Thanks,
Adam





From:
Vinayak <vn...@gmail.com>
To:
users@pdfbox.apache.org
Date:
09/30/2010 14:59
Subject:
Re: Embed PNG/GIF image into existing PDF Document



PDPixelMap constructor that could take a buffered image and PDDocument 
seems
to be broken, and hence discontinued. If someone could provide fix for the
broken code, it would be an ideal solution for handling PNG files.

JPG format doesn't support alpha layers, but PDJPeg constructor I 
mentioned
in my earlier mail, takes/processes the input buffered image with alpha
transparency. Please correct me if I am wrong, but the approach seems to
work for me with PNG images.

Vinayak

On Fri, Oct 1, 2010 at 2:52 AM, <Ad...@swmc.com> wrote:

> As far as I know the JPG format doesn't do alpha layers.  You'll have to
> go with a PNG or GIF for that.
>
> org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap mentions "png" in
> the constructor.  And there's a fileSuffix in the constructor for
> org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage where the
> documentation says "The file suffix, jpg/png."  The FlateFilter class 
also
> has a bunch of comments which reference PNGs, but I think that class 
might
> be used exclusively for decoding images... so that wouldn't help you
> insert them if that's the case.
>
> Good luck
>
> ----
> Thanks,
> Adam
>
>
>
>
>
> From:
> Vinayak <vn...@gmail.com>
> To:
> users@pdfbox.apache.org
> Date:
> 09/30/2010 12:31
> Subject:
> Re: Embed PNG/GIF image into existing PDF Document
>
>
>
> 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
> <in...@rayman2200.de>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
> >
>
>
>
> ?  Click here to submit conditions
>
> This email and any content within or attached hereto from  Sun West
> Mortgage Company, Inc.  is confidential and/or legally privileged. The
> information is intended only for the use of the individual or entity 
named
> on this email. If you are not the intended recipient, you are hereby
> notified that any disclosure, copying, distribution or the taking of any
> action in reliance on the contents of this email information is strictly
> prohibited, and that the documents should be returned to this office
> immediately by email. Receipt by anyone other than the intended 
recipient is
> not a waiver of any privilege. Please do not include your social 
security
> number, account number, or any other personal or financial information 
in
> the content of the email. Should you have any questions, please call 
(800)
> 453 7884.
>



?  Click here to submit conditions  

This email and any content within or attached hereto from  Sun West Mortgage Company, Inc.  is confidential and/or legally privileged. The information is intended only for the use of the individual or entity named on this email. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this email information is strictly prohibited, and that the documents should be returned to this office immediately by email. Receipt by anyone other than the intended recipient is not a waiver of any privilege. Please do not include your social security number, account number, or any other personal or financial information in the content of the email. Should you have any questions, please call  (800) 453 7884.