彩票走势图

C#/Java/C++开发PDF压缩功能,高保真压缩率达50%!

原创|行业资讯|编辑:何跃|2021-12-23 09:58:14.847|阅读 527 次

概述:PDF压缩原理是什么呢,识别出PDF中各种布局层、图像和文字资源等,通过技术手段对布局进行优化、删除pdf中不需要的资源。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

PDF Optimizer 在各方面对PDF文件进行优化。各种复杂功能的组合允许在没有或最小的视觉质量损失的情况下实现高压缩。另一个应用是为打印优化PDF,通过合并嵌入式字体来减少线轴文件的大小,并应用设备特定的色彩空间转换。

PDF Optimizer 有不同的使用版本:作为Shell在命令行上使用,作为被监视的文件夹服务和作为API在C#、Visual Basic、Java、C/C++中使用。

主要优化项目

在代码实现中,操作比较方便,也支持Windows和Linux开发包,具体的类库和实现代码看下面:

PDF Optimizer下载

// c#代码
using (Optimizer optimizer = new Optimizer())
{
    // Choose the optimization profile for archiving
    optimizer.Profile = PDFOptimizationProfile.eOptimizationProfileArchive;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " +
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
// Java代码
optimizer = new PdfOptimize();

// Choose the optimization profile for archiving
optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfileArchive);

// Open input file
if (!optimizer.open(inputPath, "")) 
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption)) 
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
// C++代码
pOptimizer = PdfOptimizeCreateObject();

// Choose the optimization profile for archiving
PdfOptimizeSetProfile(pOptimizer, eOptimizationProfileArchive);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input amd output files
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}



标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP