Dpi or ppi set feature for aspose.pdf or aspose.image

is it possible to set the dpi for pdf file or set the dpi for the image in the pdf file? If it’s yes, then is there any way to locate the image in each page of the pdf file, so that I can fetch that page’s image to process dpi and do compress without quality loss, and finllay I can get the small size of the pdf file

Hi,


Thank you for contacting support. You can retrieve an image from the PDF document, set the resolution, and then store back to the PDF. To further enhance the image, you can also pass the retrieved image to Aspose.Image for .NET API, and store back to the PDF document. Please refer to the following thread where we have already answered:
https://forum.aspose.com/t/10213

Kindly also check this help topic: Optimize PDF File Size

Is there any way I can use to calculate each of pdf required size for the final output merged pdf.


For example, I want to output one final pdf with limited size 3M, but I have 3 files, such as 1 png, 1jpg, 1bmp, or 1 excel,1 word and 1 png. any kind of formats combinnation. For the 3 files, each of the files should be 1M? if we can calculated each of the file needed size for final output one merged file with 3M? I don’t know if you can got my mean, what I want to express is just that output 1 pdf with 3M size based on 3 little file, but don’t know what the size shoud be limited to for each of the little file.
Hi,

Thank you for the details. You can load 3 input files to stream objects and the length property of each stream object return the size in bytes. You will take the sum of sizes, and then apply a check in the code, whether to process these 3 files or not. In the same way, Aspose.Pdf for .NET API allows to save the final PDF in the stream object and you can apply check of 3MB size before saving in the disk storage. Please let us know in case of any confusion or questions.

Thanks imran.rafique,

For 3 small files, what’s the size for each of them can meet the final output PDF’s size less than 3M? Is there any approach can accomplish it?

Hi,

Thank you for the inquiry. As a test run, we have merged 3 files each with size 1MB and Aspose.Pdf API generates a final PDF with size 3MB, since it takes the sum of sizes. Based on your query, the sum of all input PDFs should be less than 3MB.

Thanks, for example, if I have 3 files, 1M,1.5M 1M, if I can generate final output pdf file with less 3M? any approach I can do to accomplish it? such as the following optimization method.

pdfDocument.OptimizeResources(new Document.OptimizationOptions()
10 {
11 LinkDuplcateStreams = true,
12 RemoveUnusedObjects = true,
13 RemoveUnusedStreams = true,
14 CompressImages = true,
15 ImageQuality = 10
16 });

if couldn’t optimize it to be less than 3M, that means I have to make sure the total size for all 3 little file has to be less than 3M.

Hi,

Thanks for writing back.

As per my understanding, you want to check the file size of the PDF before saving it to some physical location. If so, then you can achieve this requirement by saving document into memory stream and check the size of that stream. Please check following code snippet to check the file size before saving it physically.

Document doc = new Document();
// do your stuff
MemoryStream ms = new MemoryStream();
doc.Save(ms);
long filesize = ms.Length;

// Put a check on size in MBs
if ((filesize / (1024 * 1024)) < 3) // Check if less than 3MB
{
    // do your stuff
}

In case you have different requirements than my assumption, please share some more details so that we can test the scenario in our environment and respond you accordingly.

Best Regards,

thanks for the code snippet. sorry for not describe it clearly. let me explain it in brief.


For example:

3 image files, separately, the size is 1M,1M 1.5M, that means the total size of files is 3.5M before conver to pdf, and then I run code to do conversion, 3 image will compress and optimize and merged to the final pdf, the questions is:

the final output pdf size will exceed 3M? Since we prepare the 3.5M files before convert.

Hi,

Thank you for the inquiry. We have tested three files with the same size as you narrated and Aspose.Pdf for .NET API generates the final PDF with 2.7MB in size. We have optimized input PDFs by using the following code example: Optimize PDF File Size. However, the file size also depends on the elements of input PDFs like which type of graphics you have used.

You can test every aspect of the Aspose.Pdf API before buying it. You can get a 30 day temporary license for the testing purposes from the purchase portal. Its option is available in step 4. Please also refer to these helping links: Apply License to Aspose.Pdf for .NET API and Download Aspose.Pdf for .NET API