渲染设备 – Rendering Device – C#

使用转换器类的 ConvertHTML() 方法是将 HTML 文档转换为各种格式的最常用方法。不过, Aspose.HTML for .NET API也提供了其他渲染 HTML 文档的方法,可以让您在 C# 应用程序中对渲染过程进行更多控制。

本文将介绍如何使用渲染设备– PdfDeviceXpsDeviceDocDeviceImageDevice 将 HTML 渲染为 PDF、XPS、DOCX 和图像。在 C# 示例中,渲染过程使用默认渲染选项。要了解更多信息,请阅读 Rendering Options 一文。

什么是 Aspose.HTML 中的渲染设备?

渲染设备封装了一个二维绘图表面,其 API 使用 IDevice 接口实现。目前,API 提供了以下实现:PdfDevice、XpsDevice、DocDevice 和 ImageDevice,分别用于生成 PDF、XPS、DOCX 和 Image 文件格式。

Aspose.HTML for .NET API 中的渲染设备有 PdfDeviceXpsDeviceDocDeviceImageDevice。这些类具有构造函数、属性和方法,分别表示将 HTML 渲染成 PDF、XPS、DOCX 和图像文档。每种渲染设备都有自己独特的选项集,分别由 PdfRenderingOptions, XpsRenderingOptions, DocRenderingOptions,ImageRenderingOptions 类实现。

我们的 C# 库提供了一组包含类、方法和属性的命名空间,用于将 HTML 呈现为输出格式。让我们来详细了解一下。

PdfDevice

Aspose.Html.Rendering.Pdf 名称空间提供了一个特定的 PdfDevice 类以及一些负责渲染为 PDF 文档的渲染选项类。PdfDevice 类实现了 IDevice 接口,该接口定义了渲染 HTML 文档的基本功能。

下一个 C# 示例展示了如何使用 PdfDevice 将 HTML 渲染为 PDF。渲染过程使用默认渲染选项:

 1// Render HTML to PDF using C#
 2
 3// Prepare HTML code
 4string code = @"<span>Hello, World!!</span>";
 5
 6// Prepare a path to save a converted file 
 7string savePath = Path.Combine(OutputDir, "document.pdf");
 8
 9// Initialize an HTML document from the HTML code
10using HTMLDocument document = new HTMLDocument(code, ".");
11
12// Create a PDF Device and specify the output file to render
13using PdfDevice device = new PdfDevice(savePath);
14
15// Render HTML to PDF
16document.RenderTo(device);

让我们来解释一下上面的 C# 代码片段:

  1. 首先,我们用一串代码创建一个 HTML 文档。因此,在 code 变量中,我们放入将转换为 PDF 的 content 内容。
  2. 然后,我们为保存转换后的文件准备一个路径–savePath
  3. 使用 HTMLDocument(content,baseUri) 构造函数,我们创建了一个 HTMLDocument 实例。
  4. 我们创建一个渲染设备– PdfDevice类的实例。为此,我们使用 PdfDevice(savePath) 构造函数。
  5. 最后,我们使用指定的设备调用 RenderTo(device) 方法。

渲染选项可让你对渲染过程进行额外控制。如需了解更多信息,请阅读 渲染选项 一文。

如果您想了解如何使用呈现选项将文档页面大小调整为内容大小,反之亦然,请访问文章 如何在从 HTML 转换过程中调整文档大小

ImageDevice

Aspose.Html.Rendering.Image 命名空间提供了一个特定的 ImageDevice 类以及一些负责将 HTML 文件渲染为光栅格式的渲染选项类:JPG、PNG、BMP、GIF 和 TIFF。

在 С# 示例中,要将 HTML 转换为 JPG 图像,我们需要遵循几个步骤:

  1. 使用 HTMLDocument(documentPath) 构造函数从本地文件系统加载源文件。
  2. 创建一个指定了 ImageFormatImageRenderingOptions 类实例。默认情况下,图像格式为 PNG。
  3. 创建渲染设备– ImageDevice 类的实例。使用 ImageDevice(imageOptions, savePath) 构造函数,它将渲染选项和输出文件路径作为参数。
  4. 使用 RenderTo(device) 方法将 HTML 渲染成 JPG 图像。该方法将 ImageDevice 类的一个实例作为参数。
 1// Render HTML to JPG using C#
 2
 3// Prepare path to a source HTML file
 4string documentPath = Path.Combine(DataDir, "spring.html");
 5
 6// Prepare a path to save the converted file
 7string savePath = Path.Combine(OutputDir, "spring-output.jpg");
 8
 9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create an instance of the ImageRenderingOptions class
13ImageRenderingOptions imageOptions = new ImageRenderingOptions(ImageFormat.Jpeg);
14
15// Create the Image Device and specify the output file to render
16using ImageDevice device = new ImageDevice(imageOptions, savePath);
17
18// Render HTML to JPG
19document.RenderTo(device);

XpsDevice

Aspose.Html.Rendering.Xps 名称空间提供了一个特定的 XpsDevice 类,用于将 HTML 文件渲染为 XPS 文档。在下面的 C# 示例中,我们使用默认的渲染选项来考虑渲染过程:

 1// Render HTML to XPS using C#
 2
 3// Prepare path to a source HTML file
 4string documentPath = Path.Combine(DataDir, "spring.html");
 5
 6// Prepare a path to save the converted file
 7string savePath = Path.Combine(OutputDir, "spring.xps");
 8
 9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create an instance of the XpsDevice and specify the output file to render
13using XpsDevice device = new XpsDevice(savePath);
14
15// Render HTML to XPS
16document.RenderTo(device);

DocDevice

Aspose.Html.Rendering.Doc 名称空间提供了一个特定的 DocDevice 类,用于将 HTML 文件渲染为 DOCX 格式。在下面的 C# 示例中,我们使用默认的渲染选项来考虑渲染过程:

 1// Render HTML to DOCX using C#
 2
 3// Prepare a path to save the converted file
 4string savePath = Path.Combine(OutputDir, "document.docx");
 5
 6// Load a document from 'https://docs.aspose.com/html/files/document.html' web page
 7using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/files/document.html");
 8
 9// Create an instance of the DocRenderingOptions class
10DocRenderingOptions docOptions = new DocRenderingOptions();
11
12// Create the DocDevice object and specify the output file to render
13using DocDevice device = new DocDevice(docOptions, savePath);
14
15// Render HTML to DOCX
16document.RenderTo(device);

您可以从 GitHub 下载完整的 C# 示例和数据文件。

Aspose.HTML 提供免费的在线 转换器,可将 HTML、XHTML、MHTML、EPUB、XML 和 Markdown 文件转换为一系列流行格式。你可以轻松地将 HTML 文档转换为 PDF、XPS、DOCX、JPG、PNG、GIF、TIFF 等格式。只需选择文件,选择要转换的格式,就大功告成了。最重要的是,它完全免费!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.