Insert Image into PDF

  • Matt Taylor

    Matt Taylor - 2010-01-11

    Looking at the example code I can see that I can use CSCreator.inlineImage(…) but this requires a PDImage object.

    My question is how do I go from a PNG/Jpeg file, etc.. to a PDImage.  Going from java.awt.Image -> PDImage would also be acceptable.

    Thanks in advance for any help!


  • Elfi Heck

    Elfi Heck - 2010-01-12

    To get a PDImage you can use ImageConverterAwt2Pdf which takes a BufferedImage in its constructor. I would however only recommend using inlineImage() if the image in question is REALLY small. Usually doXObject() is used to put an image on a page. Be aware that the internal size of an image is 1x1 so you have to do a scaling transformation first.

  • Matt Taylor

    Matt Taylor - 2010-01-12

    Thanks for the tip eheck!

    Unfortunately I can't seem to find the ImageConverter2Pdf class, either in the source-code or the java-doc, or google-search. 

    I was able to find  ImageConverterAwt2Swt and  ImageConverterSwt2Awt in the isCWT package but that doesn't get me where I need to go. :)

    Note I downloaded the latest v5.2.20091102 zip file.

    Looking in the example code it looks like I'll be able to do the following (Once I can get my hands on a PDImage):

    CSCreator creator = CSCreator.createNew(page);
    creator.doXObject(null, pdImage);

    I'm making a guess that the width/height are in pixels, the javadoc doesn't say.

    Thanks again for your help!

  • Matt Taylor

    Matt Taylor - 2010-01-13

    Hurray!  I found imageConverterAwt2Pdf hiding in the jpodRendorer package. 

    Unfortunately with a page-sized 480k jpeg image it appears the memory usage is way off the charts (to the point that I can't get it to complete even with 1gig).  Any suggestions? Here is the code I'm using:

    ImageConverterAwt2Pdf imageConverterAwt2Pdf = new ImageConverterAwt2Pdf(bufferedImage);
    PDImage pdImage = imageConverterAwt2Pdf.getPDImage();

    creator.doXObject(null, pdImage);

  • Elfi Heck

    Elfi Heck - 2010-01-15

    Does the DrawImage example (also in the jPodRenderer project) work?
    How large is your image in pixels? Is it 600x800 as in the code? If yes: that should work (just verified this with a 1600x1200 image), and you don't have to set the size of the PDImage yourself. The converter does this already. If no: you cannot change the size of an image by setting different values. You'd have to either change the size of the input image or only change the scaling value in the content stream (see the transform() call in DrawImage.draw() method).
    Alternatively when your source is a JPEG file you could create the PDImage with

    pdImage = (PDImage) PDImage.META.createNew();

    set the necessary values then add the raw JPEG bytes with


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks