[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PDFBox - convert image to PDF, PDF resolution

Posted on 2016-10-09
3
Medium Priority
?
1,013 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 1000 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 1000 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 15

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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Suggested Courses

656 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