我正在尝试从字节数组中生成一个 pdf,然后尝试对其进行压缩。初始文档是 tiff 图像。然后将该 tiff 转换为 pdf,然后我尝试压缩该 pdf。我正在使用 Pdftron 压缩它。对于某些 tiff 图像,压缩效果很好,而对于其他转换后的 pdf 图像看起来褪色、乱码和不可读。我的代码如下所示:
要优化转换后的 pdf:
private void OptimizePdf(string path)
{
var documentBytes = File.ReadAllBytes(path);
var request = new ConvertDocumentRequest();
var response = new ConvertDocumentResponse();
request.Document = documentBytes;
request.ConversionType = PdfConversionType.PdfToOptimizedPdf;
using (var proxy = _dynamicProxyFactory.Create<Services.PdfModification.Interfaces.IPdfModificationService>())
{
response = proxy.Invoke(x => x.ConvertDocument(request));
}
File.WriteAllBytes(path, response.Document);
}
CovertDocument 的代码如下:
internal sealed class PdfToOptimizedPdfCommand : IConversionCommand
{
private readonly IConvertDocumentCriteria _criteria;
private const double MaxDpi = 144;
private const double ResamplingDpi = 96;
private const int ImageQuality = 1;
private const bool ForceCompression = true;
public PdfToOptimizedPdfCommand(IConvertDocumentCriteria criteria)
{
_criteria = criteria;
}
public byte[] Convert()
{
var doc = new PDFDoc(_criteria.Document, _criteria.Document.Length);
var settings = new Optimizer.OptimizerSettings();
var imageSettings = new Optimizer.ImageSettings();
imageSettings.SetImageDPI(MaxDpi, ResamplingDpi);
imageSettings.SetQuality(ImageQuality);
imageSettings.ForceRecompression(ForceCompression);
settings.SetColorImageSettings(imageSettings);
Optimizer.Optimize(doc, settings);
return doc.Save(SDFDoc.SaveOptions.e_remove_unused | SDFDoc.SaveOptions.e_linearized | SDFDoc.SaveOptions.e_omit_xref);
}