iText7 Image Quality Optimizer 不能作为文档工作
iText7 Image Quality Optimizer not working as document
我正在使用 iText7
的 PDF Optimizer
产品。它使用起来非常简单,但它根本不会降低我的 PDF 的图像质量和大小。我使用的代码与他们产品的实时 LIVE DEMO 中给出的代码完全相同,但它没有按照记录工作。我正在使用它的 C#
版本。
我尝试按照代码中给出的方式切换 image_scalar_level 和 compression_level,但没有产生任何影响。如果我上传相同的 PDF 文件来测试他们的演示,它工作正常。
我究竟做错了什么?任何 help/guidelines 将不胜感激。
编辑: (根据要求添加我正在使用的代码)
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"));
代码执行后report.txt
的内容如下:
[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.
在您的示例中,您没有添加 tiff_optimizer
作为优化处理程序。在 report.txt
中没有来自 ImageQualityOptimizer
的日志记录。这意味着,没有图像质量优化。但据我了解,如果您添加 tiff_optimizer
您将在 report.txt
中看到以下行:
[ERROR] PdfOptimizer/ImageQualityOptimizer: Unable to optimize image with reference 7 0 R of type TIFF
PdfOptimizer
不抛出任何异常,如果在优化期间抛出任何异常,处理程序链中的当前处理程序已完成并写入无法执行指定操作的日志.
为了了解问题的根源,我调试了方法 PdfOptimizer#Optimize
并发现在 ImageQualityOptimizer#optimizePdf
中抛出了异常
System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
和 ImageQualityOptimizer
捕获它并写入日志。
所以总结一下,解决方案是:
添加 tiff_optimizer
作为优化处理程序:
optimizer.AddOptimizationHandler(tiff_optimizer);
添加 System.Drawing.Common
作为依赖项
我正在使用 iText7
的 PDF Optimizer
产品。它使用起来非常简单,但它根本不会降低我的 PDF 的图像质量和大小。我使用的代码与他们产品的实时 LIVE DEMO 中给出的代码完全相同,但它没有按照记录工作。我正在使用它的 C#
版本。
我尝试按照代码中给出的方式切换 image_scalar_level 和 compression_level,但没有产生任何影响。如果我上传相同的 PDF 文件来测试他们的演示,它工作正常。
我究竟做错了什么?任何 help/guidelines 将不胜感激。
编辑: (根据要求添加我正在使用的代码)
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"));
代码执行后report.txt
的内容如下:
[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.
在您的示例中,您没有添加
tiff_optimizer
作为优化处理程序。在report.txt
中没有来自ImageQualityOptimizer
的日志记录。这意味着,没有图像质量优化。但据我了解,如果您添加tiff_optimizer
您将在report.txt
中看到以下行:[ERROR] PdfOptimizer/ImageQualityOptimizer: Unable to optimize image with reference 7 0 R of type TIFF
PdfOptimizer
不抛出任何异常,如果在优化期间抛出任何异常,处理程序链中的当前处理程序已完成并写入无法执行指定操作的日志.为了了解问题的根源,我调试了方法
中抛出了异常PdfOptimizer#Optimize
并发现在ImageQualityOptimizer#optimizePdf
System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
和
ImageQualityOptimizer
捕获它并写入日志。
所以总结一下,解决方案是:
添加
tiff_optimizer
作为优化处理程序:optimizer.AddOptimizationHandler(tiff_optimizer);
添加
System.Drawing.Common
作为依赖项