PDF Compression

Posted by: isemkiv on 17 May 2022, 4:32 pm EST

  • Posted 17 May 2022, 4:32 pm EST

    Hi

    My company considering to buying GrapeCity Documents for PDF library.
    One of the feature we need is the compression of the pdf files.

    I made quick test. І wrote code :

    var pdfDocument = new GcPdfDocument();
    pdfDocument.Load(inputStream);

    pdfDocument.PdfVersion = "1.6";
    pdfDocument.ImageOptions.CompressColors = true;
    pdfDocument.ImageOptions.JpegQuality = 1;
    pdfDocument.ImageOptions.Format = ImageOptions.ImageFormat.Auto;
    pdfDocument.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
    pdfDocument.FontEmbedMode = FontEmbedMode.NotEmbed;

    pdfDocument.Save(outputStream, SaveMode.Linearized);


    .. and try compress different files. The size of the file after "compression" is the same or even greater.

    Can You help me with this? There are possibility to edit the embedded images? I'm thinking about the resolution's change or/and convert image to greyscale.

    Can I copy only images and texts from one pdf to another without attachments, notes and so on?
  • Replied 18 May 2022, 7:01 pm EST

    Hello Igor,

    You can see the following link to see how to get and set the images in the pdf.
    https://www.grapecity.com/documents-api-pdf/docs/online/Images.html?highlight=images%2C

    For text, use the GetText method to get the text and set it via DrawString method. Please see the following links for the help:
    https://www.grapecity.com/documents-api-pdf/docs/online/GrapeCity.Documents.Pdf~GrapeCity.Documents.Pdf.GcPdfDocument~GetText.html?highlight=gettext%2C
    https://www.grapecity.com/documents-api-pdf/docs/online/quickstart.html

    If you still face any issues then please share the pdf that you want to process so that we can assist you accordingly.

    Also for the compression issue also share your pdf file so that we can replicate the same issue at our end and help you further.


    Regards,
    Prabhat Sharma.
  • Replied 19 May 2022, 8:46 pm EST

    How can I replace image with the compressed image? I cannot see how to remove the old image from page or document.

    If you still face any issues then please share the pdf that you want to process so that we can assist you accordingly.

    Also for the compression issue also share your pdf file so that we can replicate the same issue at our end and help you further.

    General it is problem for each pdf file. Anyway I attached one file and lets focus on this file.

    Size of this file is 1.94MB. I can compress this file to 229KB (on pdf24 page) or to 219KB (on acrobat adobe page)

    How to compress this file using GrapeCity? Can You provide the example of the code?
  • Replied 19 May 2022, 8:46 pm EST

    Wtorek.zip
  • Replied 22 May 2022, 10:36 pm EST

    hello,

    We are looking into this issue and will let you know the updates soon.

    Regards,
    Prabhat Sharma.
  • Replied 23 May 2022, 3:12 pm EST

    Hello,

    We too can observe the issue with the size of the saved compressed file and discussing this with the developers. We will let you know the updates on this soon.

    Also, the method that we have suggested to you in our last response is for getting the images from one pdf using the GetImages method and putting it as per your requirements using the DrawImage method.
    But as per your last response, it seems that your requirement is to save the text and images only from the pdf and ignore attachments, notes, and other things in the pdf.
    If there is any other understanding then please let us know.
    [Internal Tracking id: DOC-4305]

    Regards,
    Prabhat Sharma.
  • Replied 2 June 2022, 12:06 am EST

    Hi,

    I would like to ask about updates of this issue. Do You have already any ideas how to compress the files?
  • Replied 4 June 2022, 8:31 pm EST

    Hello,

    As per the developers, the original PDF is already compressed, so compressing it again cannot yield any useful results. They have attached a sample that loads a PDF that was created without compression (LargeDocument2-no-compression.pdf) and saves it using optimal compression. The file size decreased from 2Mb to 1.3Mb in this case.

    To copy just the content without annotations or form fields, a loop like the following can be used:

           var docAnnot = new GcPdfDocument();
    using var fsAnnot = File.OpenRead("file-attachments-1.pdf");
    docAnnot.Load(fsAnnot);
    var docRes = new GcPdfDocument();
    foreach (var p in docAnnot.Pages)
    {
    var pp = docRes.Pages.Add();
    pp.Size = p.Size;
    // Copy pages ignoring annotations and form fields:
    pp.Graphics.DrawPdfPage(p, p.Bounds, false, false);
    }
    docRes.Save("no-attachments.pdf");


    This code is also included in the sample.
    Hope this helps.

    Regards,
    Prabhat Sharma.

    PdfCompressionDemo-fixed.zip
  • Replied 27 June 2022, 12:16 am EST

    It isn't the thing what I need.

    I need to compress the "compressed" file. I plan to read all images from pdf file, convert them to greyscale and decrease the resolution. After that I plan to replace the old images with the new ones.

    I can see the example of code on yours page how to read or add images to the pdf file but I cannot see how to replace or delete image from file. Can You help me with it?
  • Replied 27 June 2022, 7:27 pm EST

    Hello,

    Apologies for the inconvenience caused to you.
    We are discussing your requirement further with the developer to see what best we can do in this scenario to resolve the issue at your end and will get back to you as soon as we get the updates from the devs.

    Regards,
    Prabhat Sharma.
  • Replied 27 June 2022, 7:27 pm EST

    Hello,

    Apologies for the inconvenience caused to you.
    We are discussing your requirement further with the developer to see what best we can do in this scenario to resolve the issue at your end and will get back to you as soon as we get the updates from the devs.

    Regards,
    Prabhat Sharma.
  • Replied 3 July 2022, 4:11 pm EST

    Any thoughts? Is it possible to replace or delete image from pdf file?
  • Replied 5 July 2022, 9:33 pm EST

    Hello,

    We have replied to this on your support ticket: CAS-32343-X3F3C2.
    Please continue the thread there only to avoid confusion.

    Regards,
    Prabhat Sharma.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels