Am 12.10.2016 um 07:45 schrieb Anshul Kayastha:
Thank you Doug.

You have used JPEGFactory. Curious to know what class would be used if I
also had PNG images along with JPG images. Couldn't find anything called a



On Tue, Oct 11, 2016 at 1:47 AM, Doug Smith <> wrote:

Hi Anshul,

I made a few changes to make this work. First, I used a modified technique
to scale the image to the right dimensions. I also used the PDFBox
JPEGFactory instead of ImageIO to read the image. Lastly, I wrapped
everything in try-with-resources blocks to make sure what you open is
closed even if there is an exception.

public void convertImgToPDF(String imagePath, String fileName, String
         throws IOException {
     try (PDDocument document = new PDDocument();
          InputStream in = new FileInputStream(imagePath)) {
         PDImageXObject img   = JPEGFactory.createFromStream(document, in);
         Dimension scaledSize = getScaledDimension(img.getWidth(),
         PDPage         page  = new PDPage(new
PDRectangle(scaledSize.width, scaledSize.height));
         try (PDPageContentStream contentStream = new
PDPageContentStream(document, page)) {
             contentStream.drawImage(img, 0, 0, scaledSize.width,
         } + "/" + fileName + ".pdf");

// 8.5" * 72 points per inch, PDF Units
private static final int MIN_BOUNDARY = 612;

  * Using this method to scale images to PDF units, maintaining orientation
  * landscape or portrait.
  * Inspired by a stack overflow post
private Dimension getScaledDimension(final int originalWidth, final
int originalHeight) {
     int newWidth;
     int newHeight;

     if (originalWidth < originalHeight) {
         newWidth = MIN_BOUNDARY;
         // scale height to maintain aspect ratio
         newHeight = (newWidth * originalHeight) / originalWidth;
     else {
         newHeight = MIN_BOUNDARY;
         // scale width to maintain aspect ratio
         newWidth = (newHeight * originalWidth) / originalHeight;

     return new Dimension(newWidth, newHeight);

I hope that helps,


On Mon, Oct 10, 2016 at 12:49 PM, Anshul Kayastha <>

I am using PDFBox v2 to convert jpg images to PDF. JPG image is already
the filesystem, so I just pick it up and convert it to PDF. Below is my
public void convertImgToPDF(String imagePath, String fileName, String
destDir) throws IOException {
         PDDocument document = new PDDocument();
         InputStream in = new FileInputStream(imagePath);
         BufferedImage bimg =;
         float width = bimg.getWidth();
         float height = bimg.getHeight();
         PDPage page = new PDPage(new PDRectangle(width, height));
         PDImageXObject img = PDImageXObject.createFromFile(imagePath,
         PDPageContentStream contentStream = new
PDPageContentStream(document, page);
         contentStream.drawImage(img, 0, 0);
         in.close(); + "/" + fileName + ".pdf");

This code does the conversion just fine. However, I have observed the
following in converted PDFs

    1. When I open a converted PDF, it opens very slowly (in Acrobat
    reader). It seems as it PDF is opening pixel by pixel. If I open any
    PDF, it opens fine.
    2. The default size in acrobat reader is shown as a smaller value
    (like 15% or 26%, screenshot attached) for converted PDFs. Even
though it
    covers 100% of the screen (15% should have implied that I see a much
    smaller image, but that is not the case). When I change the size to
100%, I
    see a highly blurred image which is much larger than the actual image
and I
    have to scroll left/right and top/down to see the complete image.

Both these observations make me feel that for some reason the PDF that is
getting generated is a much higher resolution than it should have been.
Would that be a fair statement? If so, how can I correct this?

Also, attached is a sample image that I am trying to convert.

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

Reply via email to