This is a migrated thread and some comments may be shown as answers.

PDF Compression

5 Answers 264 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Chris Suttman
Top achievements
Rank 1
Chris Suttman asked on 15 Jan 2019, 03:39 PM

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.

 

5 Answers, 1 is accepted

Sort by
0
Peshito
Telerik team
answered on 18 Jan 2019, 08:43 AM
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.
0
Chris Suttman
Top achievements
Rank 1
answered on 18 Jan 2019, 03:19 PM

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);
            }

        }

 

 

0
Peshito
Telerik team
answered on 23 Jan 2019, 11:29 AM
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.
0
Chris Suttman
Top achievements
Rank 1
answered on 23 Jan 2019, 05:47 PM
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?  
0
Boby
Telerik team
answered on 28 Jan 2019, 04:11 PM
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.
Tags
PdfProcessing
Asked by
Chris Suttman
Top achievements
Rank 1
Answers by
Peshito
Telerik team
Chris Suttman
Top achievements
Rank 1
Boby
Telerik team
Share this question
or