Solved

PDFBox - convert image to PDF, PDF resolution

Posted on 2016-10-09
3
542 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 36

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Have you ever come up with a need of emailing only few pages of PDF file to one of yourfriend or colleague, instead of whole Adobe file? If yes, then surely you have face problems in doing that! Read this section as I have suggested multiple solutio…
Inserting page numbers in Portable Document Files not only enhances manageability but also makes them look professional. With numbered pages, the file appears more organized and it becomes easier to search for a particular page. The size and the vol…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

691 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