首页 > 解决方案 > iText7 Image Quality Optimizer not working as document

问题描述

I am using iText7's PDF Optimizer product. It is very simple to use, but it is not reducing the image quality and size of my PDF at all. I'm using exactly same code as given in their product's live LIVE DEMO, but it is not working as documented. I'm using it's C# version. I tried toggling image_scalar_level and compression_level as given in code, but it did not make any impact. If I upload same PDF file for testing their demo, it works fine. What am I doing wrong? Any help/guidelines would be appreciated.

EDIT: (adding code that I'm using, as request)

        LicenseKey.LoadLicenseFile("license-key-path.xml");

        PdfOptimizer optimizer = new PdfOptimizer();

        /* Here we instantiate a FileReportBuilder which we can use as a log of the efficacy of
            our optimization.  Security level configuration possible.                          */

        FileReportPublisher publisher = new FileReportPublisher(new FileInfo("report.txt"));
        FileReportBuilder builder = new FileReportBuilder(SeverityLevel.INFO, publisher);
        optimizer.SetReportBuilder(builder);

        optimizer.AddOptimizationHandler(new FontDuplicationOptimizer());
        optimizer.AddOptimizationHandler(new FontSubsettingOptimizer());

        /* Scales down and compresses Image objects. In this case, we scale and compress a
             Tiff image by 50% */
        ImageQualityOptimizer tiff_optimizer = new ImageQualityOptimizer();
        tiff_optimizer.SetTiffProcessor(new BitmapCompressor(.05f, .05f));
        optimizer.AddOptimizationHandler(new ImageQualityOptimizer());
        optimizer.AddOptimizationHandler(new CompressionOptimizer());

        ColorSpaceConverter RGB_to_CMYK_Converter = new ColorSpaceConverter();
        CsConverterProperties csConversionProperties = new CsConverterProperties(ColorConversionMode.NORMAL);
        RGB_to_CMYK_Converter.SetCsConverter(new RgbToCmykCsConverter(csConversionProperties));
        optimizer.AddOptimizationHandler(RGB_to_CMYK_Converter);

        // Document is optimized according to defined handlers and written out to file.
        optimizer.Optimize(
                new FileInfo(@"D:\sample-input\ImageQuality_Optimization_Input.pdf"),
                new FileInfo(@"D:\sample-input\ImageQuality_Optimization_Input_OPT.pdf"));

Following is the content of report.txt after code executes:

[INFO] PdfOptimizer/FontDuplicationOptimizer: No font duplication found
[INFO] PdfOptimizer/FontSubsettingOptimizer: Glyphs in document were found successfully.
[INFO] PdfOptimizer/ColorSpaceConverter: Color space of the content stream with reference 5 0 R was converted.
[INFO] PdfOptimizer/ColorSpaceConverter: Color space of the content stream resources was converted.

The pdf file that I'm using is the from their Image Quality Compressor Demo at their KB article HERE. Link to the KB article is HERE

标签: c#itextpdf-generationitext7image-compression

解决方案


  1. In your example, you don't add tiff_optimizer as an optimization handler. And in report.txt there is no logging from ImageQualityOptimizer. That means, that there was no image quality optimization. But as I understand, if you add tiff_optimizer you will see the following line in report.txt:

    [ERROR] PdfOptimizer/ImageQualityOptimizer: Unable to optimize image with reference 7 0 R of type TIFF
    
  2. PdfOptimizer doesn't throw any exception, if during the optimization any exception is thrown, current handler from handlers chain is finished and writes log that it is unable to do the specified action.

  3. To understand source of problem I debugged the method PdfOptimizer#Optimize and found out that the exception is thrown in ImageQualityOptimizer#optimizePdf

    System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
    

    and ImageQualityOptimizer catches it and writees the log.

So to Summarize, the solutions is:

  1. Add tiff_optimizer as optimization handler:

    optimizer.AddOptimizationHandler(tiff_optimizer);
    
  2. Add System.Drawing.Common as dependency


推荐阅读