empira / PDFsharp-1.5

A .NET library for processing PDF
MIT License
1.28k stars 589 forks source link

Compressed PDF not working properly #172

Closed MohammedMubeen closed 1 year ago

MohammedMubeen commented 1 year ago

Hi @TH-Soft , @ThomasHoevel

Please help me. The below code not working properly.

        using (var stream = new MemoryStream(Pdfdata))
        using (var source = PdfReader.Open(stream, PdfDocumentOpenMode.Import))
        using (var document = new PdfSharp.Pdf.PdfDocument())
        {
            var options = document.Options;
            options.FlateEncodeMode = PdfFlateEncodeMode.BestCompression;
            options.UseFlateDecoderForJpegImages = PdfUseFlateDecoderForJpegImages.Automatic;
            options.CompressContentStreams = true;
            options.NoCompression = false;

20MB.pdf

            foreach (var page in source.Pages)
            {
                document.AddPage(page);
            }
            document.Save(filepath);
        }
ThomasHoevel commented 1 year ago

I cannot see any issue. But I only see a code snippet. What do you expect vs. what you get?

The options work for images you add to the document. They have no effect on images already stored in other PDF files.

MohammedMubeen commented 1 year ago

Hi @ThomasHoevel ,

Thanks for the response.

My expectation, I want to reduce the file size below 10 MB.

Its any option to optimize the PDF?

Reason : I going to use the pdf though API, That API endpoint allow up to 10 MB Only.

ThomasHoevel commented 1 year ago

PDF files are usually compressed anyway. "BestCompression" may gain a few percent when creating new PDFs. You probably cannot easily go from 20 MB to 10 MB using lossless compression only.

Where does the PDF come from? Any chance to optimize the size at the source?

MohammedMubeen commented 1 year ago

Actually, I generated PDF using Dynamic PDF. https://www.dynamicpdf.com/PDF-Suite-.NET.aspx

I have merged External PDF. That time PDF file size increased.

NOTE: Without external pdf - File size 3 to 5 Mb Only.

I already requested to the dynamic pdf support team. They Said like " There is no option to optimize external pdf".

Could you please suggest me any alternative options?