在 Rust 中将 PDF 转换为图像格式

将 PDF 转换为图像

在本文中,我们将向您展示将 PDF 转换为图像格式的选项。

以前扫描的文档通常会保存为 PDF 文件格式。但是,您是否需要在图形编辑器中编辑它,或进一步以图像格式发送?我们为您提供了一个通用工具,使用 Aspose.PDF for Rust via C++ 将 PDF 转换为图像。 最常见的任务是当您需要将整个 PDF 文档或文档的某些特定页面保存为一组图像时。Aspose.PDF for Rust via C++ 允许您将 PDF 转换为 JPG 和 PNG 格式,以简化从特定 PDF 文件获取图像所需的步骤。

Aspose.PDF for Rust via C++ 支持将 PDF 转换为各种图像格式。请检查该部分。 Aspose.PDF 支持的文件格式.

将 PDF 转换为 JPEG

提供的 Rust 代码片段演示了如何使用 Aspose.PDF 库将 PDF 文档的第一页转换为 JPEG 图像:

  1. 打开 PDF 文档。
  2. 使用将页面转换为 JPEG page_to_jpg 函数。

  use asposepdf::Document;

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

      // Convert and save the specified page as Jpg-image
      pdf.page_to_jpg(1, 100, "sample_page1.jpg")?;

      Ok(())
  }

将 PDF 转换为 TIFF

提供的 Rust 代码片段演示了如何使用 Aspose.PDF 库将 PDF 文档的第一页转换为 TIFF 图像:

  1. 打开 PDF 文档。
  2. 使用将 Page 转换为 TIFF page_to_tiff 函数。

  use asposepdf::Document;

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

      // Convert and save the specified page as Tiff-image
      pdf.page_to_tiff(1, 100, "sample_page1.tiff")?;

      Ok(())
  }

将 PDF 转换为 PNG

提供的 Rust 代码片段演示了如何使用 Aspose.PDF 库将 PDF 文档的第一页转换为 PNG 图像:

  1. 打开 PDF 文档。
  2. 使用将页面转换为 PNG 页面转PNG 函数。

  use asposepdf::Document;

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

      // Convert and save the specified page as Png-image
      pdf.page_to_png(1, 100, "sample_page1.png")?;

      Ok(())
  }

可伸缩矢量图形 (SVG) 是一系列基于 XML 的文件格式规范,用于二维矢量图形,既包括静态也包括动态(交互式或动画)。SVG 规范是一项开放标准,自 1999 年起由万维网联盟 (W3C) 开发。

将 PDF 转换为 SVG

提供的 Rust 代码片段演示了如何使用 Aspose.PDF 库将 PDF 文档的首页转换为 SVG 图像:

  1. 打开 PDF 文档。
  2. 使用将页面转换为 SVG 页面转SVG 函数。

  use asposepdf::Document;

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

      // Convert and save the specified page as Svg-image
      pdf.page_to_svg(1, "sample_page1.svg")?;

      Ok(())
  }

将 PDF 转换为 SVG ZIP 压缩包

下面的示例将 PDF 文档转换为 SVG 存档,其中每页被保存为 ZIP 容器内的单独 SVG 文件。

  1. 打开源 PDF 文档。
  2. 将文档保存为包含 SVG 文件的 ZIP 存档。

  use asposepdf::Document;

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

      // Convert and save the previously opened PDF-document as SVG-archive
      pdf.save_svg_zip("sample_svg.zip")?;

      Ok(())
  }

将 PDF 转换为 DICOM

提供的 Rust 代码片段演示了如何使用 Aspose.PDF 库将 PDF 文档的第一页转换为 DICOM 图像:

  1. 打开 PDF 文档。
  2. 使用将 Page 转换为 DICOM 页面_to_DICOM 函数。

  use asposepdf::Document;

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

      // Convert and save the specified page as DICOM-image
      pdf.page_to_dicom(1, 100, "sample_page1.dcm")?;

      Ok(())
  }

将 PDF 转换为 BMP

提供的 Rust 代码片段演示了如何使用 Aspose.PDF 库将 PDF 文档的首页转换为 BMP 图像:

  1. 打开 PDF 文档。
  2. 使用将页面转换为 BMP 页面转BMP 函数。

  use asposepdf::Document;

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

      // Convert and save the specified page as Bmp-image
      pdf.page_to_bmp(1, 100, "sample_page1.bmp")?;

      Ok(())
  }