?
Solved

PDFBox - convert image to PDF, PDF resolution

Posted on 2016-10-09
3
Medium Priority
?
741 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

770 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