Solved

PDFBox - convert image to PDF, PDF resolution

Posted on 2016-10-09
3
66 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
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 12

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe In a previous Experts Exchange article, How To Rename-Move a Batch of PDF Files Based on Contents of the Files (http://www.experts-exchan…
PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now