PDF Compression

6 posts, 0 answers
  1. Chris Suttman
    Chris Suttman avatar
    3 posts
    Member since:
    Jan 2006

    Posted 15 Jan Link to this post

    Hello we have been getting large PDF files in the 80 MBs range and need to compress these.  Some clients are scanning in too high resolution and in color.  I have tried several Telerik code examples for importing and exporting, but have had no successful attempts at reducing the file size. Most made the same PDF size, but some examples resulted in larger PDF sizes.  There was a grayscale example in the a thread, but that increased the file size by about 50% regardless of the PDF.  Is there an example on importing, running a compression scheme, and then exporting that results in smaller PDFs? 

    Thank you.

     

  2. Peshito
    Admin
    Peshito avatar
    562 posts

    Posted 18 Jan Link to this post

    Hello Chris,

    When you import and then export a PDF file with RadPdfProcessing the exported file is always compressed the best way RadPdfProcessing can. That said, in case the original file has uncompressed content, it will be compressed on export with the RadPdfProcessing. There is a property - ImageQuality that can change the default quality compression however it does so for newly created images and not for the ones imported from the original PDF file.

    There is a feature request in our public portal - Introduce API allowing to optimize and reduce the size of existing PDF files. You could upvote it and subscribe for it in order to get notified when its implementation status changes.

    Regards,
    Peshito
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Chris Suttman
    Chris Suttman avatar
    3 posts
    Member since:
    Jan 2006

    Posted 18 Jan in reply to Peshito Link to this post

    Yes, I was trying the ImageQuality on the lowest setting and it would output about same size but larger.  For instance this 147,295 KB pdf outputs to 148,398 KB. Why would this output to a larger pdf when the ImageQuality is set to low?

     

     public static readonly string RootDirectory = AppDomain.CurrentDomain.BaseDirectory;
            public static readonly string InputFileName = RootDirectory + "InputFiles\\721906.pdf";


            static void Main()
            {
                TestOutput();
            }

            private static void TestOutput()
            {
                if (File.Exists(ResultFileName))
                {
                    File.Delete(ResultFileName);
                }
                PdfFormatProvider provider = new PdfFormatProvider();
                PdfExportSettings settings = new PdfExportSettings();
                settings.ImageQuality = ImageQuality.Low;

                using (Stream stream = File.OpenRead(InputFileName))
                {
                    RadFixedDocument document = provider.Import(stream);
                    provider.ExportSettings = settings;
                   
                    Stream outStream = File.Create("output.pdf");
                    provider.Export(document, outStream);
                }

            }

     

     

  4. Peshito
    Admin
    Peshito avatar
    562 posts

    Posted 23 Jan Link to this post

    Hi Chris, 

    The ImageQuality changes the quality compression for newly created images and not for the ones imported from the original PDF file. This is why the size of the document is not reduced. In case new images are being added to the document their quality will be affected by the ImageQuality setting.

    As for why the size increased, this is caused by the fact that when a document is imported and then exported, the PdfProcessing embeds fonts in it. According to PDF Reference specification all fonts used in the document should be embedded except from the 14 Standard PDF fonts.

    Hope this clarifies why has the document size increased a bit.

    Should you have any other questions, please let me know.

    Regards,
    Peshito
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. Chris Suttman
    Chris Suttman avatar
    3 posts
    Member since:
    Jan 2006

    Posted 23 Jan in reply to Peshito Link to this post

    So is there a way I can tell the export to treat the existing images as new ones?  Or how would I extract the images then add them to the ImageQuality will actually work?  
  6. Boby
    Admin
    Boby avatar
    697 posts

    Posted 28 Jan Link to this post

    Hi Chris,

    You are right, it's possible to iterate the content elements, detect the original images, remove them, and insert new images with modified image source, or with ImageQuality set to lower values, for example:
    foreach (Image originalImage in page.Content.Where(contentElement => contentElement is Image).ToList())
    {
        var bitmapSource = originalImage.ImageSource.GetBitmapSource();
     
        // Resize the bitmap source here with a WPF API, or just use
        // var newImage = page.Content.AddImage(new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(bitmapSource, Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ImageQuality.Low));
        // instead of the below line:
        var newImage = page.Content.AddImage(new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(bitmapSource));
        newImage.Position = originalImage.Position;
        newImage.Width = originalImage.Width;
        newImage.Height = originalImage.Height;
        newImage.Clipping = originalImage.Clipping;
     
        page.Content.Remove(originalImage);
    }


    Regards,
    Boby
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top