Setting DPI/PPI of images in PDF

Hello Aspose Team,

I'm converting from many data types to PDF. I have the task to implement that images get a certain DPI/PPI because some sources have a high picture size for their display size.

I have already asked in the Apose.Total Product Family for a methods to handle the DPI in Office Documents before they get PDF. Since I have about 20 datatypes that get converted to PDF it would be a lot of work to do it for each one of them.. so I tought it might be easier to handle them once in PDF format.

So my question is if there is a convenient way to downscale the DPI of images in PDF files so say 300 DPI? If not.. is the last option to extract the image, convert it and add it back to the PDF?

I have posted this thread in Aspose.Pdf and Pdf.Kit Product Family since both are about PDF and one might have a solution.

Thank you in advance!

Kind regards
Peter

Hi Peter,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for the details.

I am afraid; your requested feature of changing the DPI/PPI of the images in the PDF files is not supported at the moment. I have created a new feature request in our issue tracking system as PDFNEWNET-34352. Our development team will further look into this feature. Also, as mentioned by you, to achieve this you need to extract the images with low resolution and then replace the existing images with the low resolution images. You may check the following documentation link for the details in this regard.

Extract Images from the PDF File

Replace Image in an Existing PDF File

Sorry for the inconvenience,

Hi Peter,

Sorry for the confusion.

As your requirement is regarding Aspose.Pdf for Java, I have added the new request in our issue tracking system as PDFNEWJAVA-33203. Our development team will look into it and see if it can be supported in near future.

Also, you can see the following links regarding Aspose.Pdf.Kit for Java to extract and replace images.

Extract Image from PDF Document

Manipulate text and images in an existing PDF File

Sorry for the inconvenience,

Hi Peter,


Thanks for using our products.

Aspose.Pdf.Kit for Java is a component which provides the capability to manipulate existing PDF documents. But I am afraid currently it does not support the feature to change/downgrade the DPI/PPI of images present in existing PDF documents. In order to accomplish your requirement, you may consider first extracting the Images from PDF file using extractImage(…) method of PdfExtractor class, downgrade their DPI and then replace the existing Images in PDF file with newly generated files. In order to replace the images in PDF document, please try using replaceImage(…) method of PdfContentEditor class.

[Java]

PdfContentEditor editor = new
PdfContentEditor();<o:p></o:p>

editor.bindPdf(".\\example1.pdf");

editor.replaceImage(1, 2, ".\\untitled1.jpg");

editor.save(".\\example1_replace_out.pdf");


Please visit the following link for further information on

In case of any further query, please feel free to contact. We apologize for your inconvenience.

Hello Nausherwan and Nayyer,

thank you for your replies and creating an issue for this feature.

I will implement a replacing mechanism provided by the methods from the code snippets for now, thank you!

Kind regards
Peter

I tried the code snippets. Unfortunately it throws a ClassCastException after executing editor.replaceImage():

java.lang.ClassCastException: com.aspose.pdf.kit.nr incompatible with com.aspose.pdf.kit.ml

at com.aspose.pdf.kit.PdfContentEditor.replaceImage(Unknown Source)

regards

Hi Peter,


The problem seems to be related to the source PDF file which you are using. Can you please share the document so that we can test the scenario at our end. We are sorry for your inconvenience.

Hi

The source file is generated by the Aspose PDF Kit.

I am trying to iterate over all images on all pages in the PDF:

// pdfPath is a path to a pdf file

int numberOfPages = new PdfFileInfo(pdfPath).getNumberofPages();

PdfContentEditor editor = new PdfContentEditor();

editor.bindPdf(pdfPath);

for(int i = 1; i <= numberOfPages; i++) {

//Instantiate PdfExtractor object

PdfExtractor extractor = new PdfExtractor();

//Bind the input PDF document to extractor

extractor.bindPdf(pdfPath);

extractor.setStartPage(i);

extractor.setEndPage(i);

//Extract images from the input PDF document

extractor.extractImage();

int imageCount = 1;

while (extractor.hasNextImage()) {

String fileName = new File(pdfPath).getAbsolutePath() + "_" + i + "_" + imageCount + ".jpg";

extractor.getNextImage(fileName);

imageCount++;

//

// scale down image, write to file destinationFile

//

editor.replaceImage(i, imageCount, destinationFile.getAbsolutePath());

}

extractor.close();

}

editor.save(pdfPath);

editor.close();

Unfortunately the call to editor.replaceImage() throws an Exception.

regards

Hi Peter,


Thanks for the additional information.

I have tested the scenario where I have used the following simple code and I am able to reproduce the same exception. For the sake of correction, I have logged it in our issue tracking system as PDFKITJAVA-33241. We
will investigate this issue in details and will keep you updated on the status
of a correction. We
apologize for your inconvenience.

[Java]

String pdfPath = “d:/pdftest/OWASP_Testing_Guide_v3_Updated.pdf”;<o:p></o:p>

// pdfPath is a path to a pdf file

int numberOfPages = new PdfFileInfo(pdfPath).getNumberofPages();

PdfContentEditor editor = new PdfContentEditor();

editor.bindPdf(pdfPath);

for (int i = 1; i <= numberOfPages; i++)

{

int imageCount = 1;

editor.replaceImage(i, imageCount, "d:/pdftest/bird_67.png");

}

editor.save("d:/pdftest/Images_Replaced.pdf");

editor.close();


PS, I have observed that if I replace numberOfPages with 3 or any value less than 3, exception is not occurring.

The issues you have found earlier (filed as PDFNEWJAVA-33203) have been fixed in Aspose.Pdf for Java 9.5.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.