Optimize PDF file at page level using Aspose.PDF for .NET - output is larger in size than the input

Hi

We have size restrictions on a page level.
I tried to open the PDF file, loop over each page, optimize each page, add the optimized page to a new PDF document, and after I have looped over all the pages I saved the new PDF document, but the size of the document is now larger than the original PDF document.

Can you please assist me?

public void Method(string pdf)
{
Document optimizedDoc = new Document();
using (var originalDocument = new Document(pdf))
{
foreach (var page in originalDocument.Pages)
{
Document newPageDoc = new Document();
newPageDoc.Pages.Add(page);
newPageDoc.Save();

                newPageDoc.OptimizeResources(new OptimizationOptions
                {
                    RemoveUnusedObjects = true,
                    ImageCompressionOptions =
                    {
                        CompressImages = true,
                        ImageQuality = 50,
                        ResizeImages = true
                    },
                    RemoveUnusedStreams = true,
                    MaxResoultion = 150,
                    UnembedFonts = true,
                    RemovePrivateInfo = true,
                    AllowReusePageContent = true,
                    LinkDuplcateStreams = true
                });

                newPageDoc.Save();
                optimizedDoc.Pages.Add(newPageDoc.Pages.FirstOrDefault());

            }
            optimizedDoc.Save(@"C:\blah1.pdf");
        }
        
    }

file-example_PDF_1MB.pdf (1017.7 KB)
image.png (23.3 KB)

Just to clarify,
the original document is 1018KB,
after I optimized each page the document is now 1617KB

@Schalla

We were able to replicate the similar issue at our side while testing the scenario with 20.11 version of the API. We need to further investigate this case that why optimized pages form larger PDF file when combined. For the sake, we have logged an issue as PDFNET-49041 in our issue tracking system. We will further look into it and keep you posted with its rectification status. Please be patient and spare us some time.

We are sorry for the inconvenience.

Ha, so it was not me :slight_smile:

Thank you for the response.

FYI, I have another document it is 380 pages and a total size of 4MB and after I try this approach it is 140MB, I cannot share this doc with you it contains financial information.

@Schalla

You can share that document as well in a private message if you do not want to share it publicly. In order to send it privately, you can click over username and press Blue Message Button.

Sorry but I cannot share it, it is a clients information.

@Schalla

Alright. For the PDF which you have already shared, we were able to reproduce the issue and have logged it for correction. We will let you know once we have some feedback regarding its resolution.

Hi

Do you have any feedback?

@Schalla

The ticket has recently been logged in our issue tracking system and is pending for a review. It will be investigated/resolved on first come first serve basis. We will surely inform you within this forum thread as soon as we make some definite progress towards ticket resolution. Please give us some time.

We are sorry for the inconvenience.