Solved

PDFBox - convert image to PDF, PDF resolution

Posted on 2016-10-09
3
437 Views
Last Modified: 2016-11-03
I am using PDFBox v2 to convert jpg images to PDF. JPG image is already on the filesystem, so I just pick it up and convert it to PDF. Below is my code

public void convertImgToPDF(String imagePath, String fileName, String destDir) throws IOException {
        PDDocument document = new PDDocument();
        InputStream in = new FileInputStream(imagePath);
        BufferedImage bimg = ImageIO.read(in);
        float width = bimg.getWidth();
        float height = bimg.getHeight();
        PDPage page = new PDPage(new PDRectangle(width, height));
        document.addPage(page);
        PDImageXObject img = PDImageXObject.createFromFile(imagePath, document);
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(img, 0, 0);
        contentStream.close();
        in.close();
        document.save(destDir + "/" + fileName + ".pdf");
        document.close();
    }

Open in new window


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 other PDF, it opens just fine.

2. The default size in acrobat reader is shown to a smaller value (like 15% or 24%, screenshot attached) for converted PDFs. Even though it covers 100% of the screen (15% sould 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?
PDFSize.PNG
0
Comment
Question by:ank5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Assisted Solution

by:Justin May
Justin May earned 250 total points
ID: 41836861
Your PDF file size will be affected by the original image/file you are using to create the PDF. Any pixel/raster image generated by the process of converting from a pixel based image file to a PDF will still be pixels. These files are generally larger than text or vector images.

If the conversion process in your code adds resolution or changes resolution from the original files you will see image degradation.

PDF files viewed in Acrobat are generally render well at all scales up to 100% then the files will be upscaled and images on screen will be degrading the higher the magnification. However you are describing image degradation at much less than 100% image scaling. This could be because your code is upscaling the images when converting to PDF.

Images viewed in Acrobat (as PDF files) can be slow to open especially if the pixel (width, height) count is very high.

Try checking the conversion process the code is using to convert your files as I suspect that is the issue.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 250 total points
ID: 41837674
DIsclaimer: I haven't used PDFBox (at least not recently) so I don't know 100% but I still think I can help you try some things anyway...

So, regarding the fact that it seems that the page is being shown at the small zoom level, is because I think that the PDF has pages that are much larger than normal. I think that this may be due to line 7. I'm not sure what the "units" are that you pass to the PDPage constructor but I am guessing that it ISN'T pixels (which is the units that your width/height variables would be in).

Can you try initialising your page like this...

PDPage page = new PDPage(PDRectangle.A4);

Open in new window


I'm not sure if you will need to use the width/height elsewhere to get the conversion correct, but at least this should be a start in the right direction.
0
 
LVL 14

Expert Comment

by:Edwin Hoffer
ID: 41837822
Dear user
Usually I prefer to use Image to PDF converter when I have to convert multiple Image into PDF file format. I think it can prove useful for you too. But I would like to suggest you to try the free version first. When you are totally satisfied with the tool then only it's beneficial to purchase it.

Thanks & Regards
Edwin Hoffer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

*Adobe Acrobat 9 was used for this article. Particular steps may vary depending on software versions. 1. Create a framework of your form in Word, leaving space where you’d ultimately like the Adobe fields to appear.  (Note: I use the blank lines …
In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question