使用 Rust 通过 C++ 优化 PDF 资源

优化 PDF 资源

优化文档中的资源:

  1. 未在文档页面使用的资源将被移除。
  2. 相同的资源将合并为单个对象。
  3. 未使用的对象将被删除。

优化可能包括压缩图像、移除未使用的对象以及优化字体以减小文件大小并提升性能。此操作期间的任何错误都会被记录并终止程序。

  1. 打开 method 将指定的 PDF 文件(sample.pdf)加载到内存中。
  2. 使用 对 PDF 中的资源进行优化以提升效率 optimize_resource method.
  3. save_as method 将已优化的 PDF 保存到新文件。

以下代码片段展示了如何优化 PDF 文档。


  use asposepdf::Document;

  fn main() -> Result<(), Box<dyn std::error::Error>> {
      // Open a PDF-document named "sample.pdf"
      let pdf = Document::open("sample.pdf")?;

      // Optimize PDF-document resources
      pdf.optimize_resource()?;

      // Save the optimized PDF-document with new filename
      pdf.save_as("sample_optimize_resource.pdf")?;

      Ok(())
  }