1
votes

I have seen code to extract the images based on image DPI using PDFBox like below

    PDDocument  document = PDDocument.load(path);
    PDFImageWriter writer = new PDFImageWriter();
    boolean success = writer.writeImage(document, "jpg", "", 1, 1,
                 "C:\\Image Quality\\150", BufferedImage.TYPE_INT_RGB, 150);
    return document;

In above code I can specify the image resolution(150) while extracting the image from pdf. With higher resolution I get larger image in return.

Now I want reverse of it means to specify the resolution/dpi of image while writing image to PDF, but following code is not providing such options to specify DPI ? Can anyone guide me where I am missing

PDPageContentStream contentStream = null;
contentStream = new PDPageContentStream(document, userPage);
contentStream.drawImage(img, 60, 60);       
contentStream.close();

Please guide me where I can pass the parameter of resolution/DPI (as image is larger than pdf page size) while writing image to PDF ?

Thanks,

1
You have been told in answer to your previous question that dpi hardly has meaning in the context of PDF. that been said You might achieve your objective using the overload drawXObject(PDXObject xobject, float x, float y, float width, float height).mkl
Yes I had understood that point, but I saw some online sites that also require online2pdf.com/convert-images-to-pdf resolution when converting image to PDF, so its creating confusion for me. Now what I am assuming that resizing image to make fit into PDF wont change quality, is it ? I think resizing and then using drawImage is equal to drawXObject method, please correct me if this is not the difference between these two.Usman
I think resizing and then using drawImage is equal to drawXObject method, please correct me if this is not the difference between these two. - resizing (i.e. downsampling) the image in its original form and then using drawImage embeds the downsampled image. drawXObject on the other hand embeds the original image and scales it. Thus, at high resolution print-out the former only supplies the downsampled, less resolved image while the latter allows the higher resolved image to be output.mkl
Many Thanks you solved my problem. Now I am getting high quality PDF previously I was resizing and it loses quality but now its working perfectly with drawXObject.Usman
Ok, I made my comments an actual answer.mkl

1 Answers

2
votes

You have been told in answer to your previous question that dpi hardly has meaning in the context of PDF.

That been said, tough, you can achieve your objective using the method PDPageContentStream.drawXObject(PDXObject xobject, float x, float y, float width, float height)

Resizing (i.e. downsampling) the image in its original form and then using drawImage embeds the downsampled image.

Using drawXObject on the other hand embeds the original image and scales it. Thus, at high resolution print-out the former only supplies the downsampled, less resolved image while the latter allows the higher resolved image to be output.