c# - 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
解决方案
In your example, you don't add
tiff_optimizer
as an optimization handler. And inreport.txt
there is no logging fromImageQualityOptimizer
. That means, that there was no image quality optimization. But as I understand, if you addtiff_optimizer
you will see the following line inreport.txt
:[ERROR] PdfOptimizer/ImageQualityOptimizer: Unable to optimize image with reference 7 0 R of type TIFF
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.To understand source of problem I debugged the method
PdfOptimizer#Optimize
and found out that the exception is thrown inImageQualityOptimizer#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:
Add
tiff_optimizer
as optimization handler:optimizer.AddOptimizationHandler(tiff_optimizer);
Add
System.Drawing.Common
as dependency
推荐阅读
- android - Mediapipe 样本与最新版本的“androidx.camera”不兼容
- postgresql - 类型ORM外键
- elasticsearch - Logstash、Filebeat、ELK 堆栈
- javascript - 无法多次按下重置按钮
- c# - 有没有更好的方法来减少 if 语句
- ejb - Jboss 使用 JTS 而不是 JTA
- python - 我不断收到无效的语法,但我找不到它
- vue.js - Vuejs - 无法解析 'app/src' 中的 'moment' | 在项目克隆和 npm install 之后
- sql-server - 在 T-SQL 中连接 XML 片段
- javascript - 检查值在编辑函数中返回 undefiend