Usman
Usman

Reputation: 125

Write higher resolution (DPI) Images to PDF

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,

Upvotes: 1

Views: 4304

Answers (1)

mkl
mkl

Reputation: 95918

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.

Upvotes: 3

Related Questions