渲染器 – Renderers – 用 C# 渲染 HTML、MHTML、EPUB 和 SVG
渲染器 – Renderers
Aspose.Html.Rendering 命名空间由许多呈现器对象和相应的底层选项类组成,它们负责将文档呈现到 IDevice实现中。Aspose.HTML for .NET API 提供了以下呈现器实现: HtmlRenderer、 SvgRenderer、 MhtmlRenderer 和 EpubRenderer,它们分别用于呈现 HTML、SVG、MHTML 和 EPUB 文档。
虽然 Document 类的 RenderTo() 方法可以让你向输出渲染设备发送单个文档,但直接使用 Renderer 实例,你可以一次发送多个文件。因此,您可以 合并 HTML、MHTML、EPUB 和 SVG 文档,并应用渲染选项来转换输出文件。
本文介绍了使用 Renderer 类的 Render() 方法将基于 HTML 的文件转换和合并为其他流行格式的支持场景。
HtmlRenderer
使用 HtmlRenderer 类将 HTML 文件渲染为其他流行格式–这是通过自定义渲染选项和控制渲染过程输出来转换 HTML 的另一种方法。 让我们看看如何使用 HtmlRenderer 类将 HTML 渲染为带有自定义渲染选项的 PDF:
- 初始化 HTML 文档。使用 HTMLDocument() 构造函数之一创建 HTMLDocument 实例。
- 创建一个新的 HtmlRenderer 对象。
- 创建 PdfRenderingOptions 实例,并为输出 PDF 文档设置自定义选项。在示例中,我们指定了 PageSetup 类的 AnyPage 属性,该属性可设置一个页面大小为 600 像素乘 200 像素的新页面对象,还指定了 Encryption 属性,该属性可识别用户和所有者密码、允许的权限以及输出 PDF 文件的加密算法。
- 使用
PdfDevice(
options
,savePath
) 构造函数创建一个 PdfDevice 类对象。 - 最后,调用
Render(
device
,document
)方法,以指定的渲染选项将 HTML 文档渲染为输出 PDF 文件。
1// Render HTML to PDF with custom page settings using C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "file.html");
5
6// Initialize an HTML document from the file
7using HTMLDocument document = new HTMLDocument(documentPath);
8
9// Create an instance of HTML Renderer
10using HtmlRenderer renderer = new HtmlRenderer();
11
12// Prepare a path to save the converted file
13string savePath = Path.Combine(OutputDir, "convert-html-options.pdf");
14
15// Create the instance of Rendering Options and set a custom page-size
16PdfRenderingOptions options = new PdfRenderingOptions();
17options.PageSetup.AnyPage = new Page(new Size(600, 200));
18options.Encryption = new PdfEncryptionInfo(
19 "user_pwd",
20 "owner_pwd",
21 PdfPermissions.PrintDocument,
22 PdfEncryptionAlgorithm.RC4_128);
23
24// Create an instance of PDF device
25using PdfDevice device = new PdfDevice(options, savePath);
26
27// Render HTML to PDF
28renderer.Render(device, document);
渲染选项可让你对渲染过程进行额外控制。如需了解更多信息,请阅读 渲染选项 一文。
要了解有关渲染过程的更多信息,请阅读 渲染设备 一文。
如果您想了解如何使用呈现选项将文档页面大小调整为内容大小,反之亦然,请访问文章 如何在从 HTML 转换过程中调整文档大小
SvgRenderer
通过 SvgRenderer,您可以将 SVG 文件渲染为其他文件格式,如 PDF、XPS、DOCX 和图像格式。您可以使用各种渲染选项自定义输出文件。下面的示例演示了如何使用 SvgRenderer 类将 SVG 渲染为指定了自定义页面大小的 PDF:
- 初始化 SVG 文档。使用 SVGDocument() 构造函数之一创建 SVGDocument 实例。
- 创建一个新的 SvgRenderer对象。
- 创建 PdfRenderingOptions 实例,并为输出 PDF 文档设置自定义选项。在示例中,我们指定了 PageSetup.AnyPage 属性,该属性设置了一个页面大小为 600x500 像素的新页面对象。
- 使用
PdfDevice(
options
,savePath
) 构造函数创建一个 PdfDevice 类对象。 - 最后,调用
Render(
device
,document
) 方法,以指定的渲染选项将 SVG 文档渲染为输出 PDF 文件。
1// Render SVG to PDF using C#
2
3// Initialize an SVG document from the file
4using SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svg"));
5
6// Create an instance of SVG Renderer
7using SvgRenderer renderer = new SvgRenderer();
8
9// Prepare a path to save the converted file
10string savePath = Path.Combine(OutputDir, "rendering-svg.pdf");
11
12// Create the instance of Rendering Options and set a custom page-size
13PdfRenderingOptions options = new PdfRenderingOptions();
14options.PageSetup.AnyPage = new Page(new Size(600, 500));
15
16// Create an instance of the Pdfdevice class
17using PdfDevice device = new PdfDevice(options, savePath);
18
19// Render SVG to PDF
20renderer.Render(device, document);
MhtmlRenderer
下面的示例演示了如何使用 MhtmlRenderer 类指定渲染选项,将 MHTML 转换为 PDF:
- 打开现有的 MHTML 文档。
- 使用 MhtmlRenderer() 构造函数创建 MhtmlRenderer 实例。
- 初始化 PdfRenderingOptions 类并设置渲染选项。
- 创建 PdfDevice 类的实例。
- 调用
Render(
device
,stream
) 方法将 MHTML 渲染成 PDF。
1// Convert MHTML to PDF with custom page settings using C#
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Create an instance of MHTML Renderer
7using MhtmlRenderer renderer = new MhtmlRenderer();
8
9// Prepare a path to save the converted file
10string savePath = Path.Combine(OutputDir, "convert-mhtml-options.pdf");
11
12// Create the instance of Rendering Options and set a custom page-size and background color
13PdfRenderingOptions options = new PdfRenderingOptions();
14options.PageSetup.AnyPage = new Page(new Size(600, 200));
15options.BackgroundColor = System.Drawing.Color.Azure;
16
17// Create an instance of PDF device
18using PdfDevice device = new PdfDevice(options, savePath);
19
20// Convert MHTML to PDF
21renderer.Render(device, stream);
EpubRenderer
通过 EpubRenderer 类,您可以将 EPUB 文件转换为 PDF、XPS、DOCX 和图像等其他格式。下面的 C# 示例展示了如何通过自定义页面大小设置将 EPUB 转换为 DOCX:
- 打开现有 EPUB 文件
- 创建 EpubRenderer 类的实例。
- 初始化 DocRenderingOptions 类并设置渲染选项。
- 创建 DocDevice 类的实例。
- 调用
Render(
device
,stream
) 方法将 EPUB 渲染为 DOCX。
1// Convert EPUB to DOCX with custom page size using C#
2
3// Open an existing EPUB file for reading
4using FileStream stream = File.OpenRead(DataDir + "input.epub");
5
6// Create an instance of EPUB Renderer
7using EpubRenderer renderer = new EpubRenderer();
8
9// Prepare a path to save the converted file
10string savePath = Path.Combine(OutputDir, "convert-epub-options.docx");
11
12// Create the instance of Rendering Options and set a custom page-size
13DocRenderingOptions options = new DocRenderingOptions();
14options.PageSetup.AnyPage = new Page(new Size(800, 400));
15
16// Create an instance of the DocDevice class
17using DocDevice device = new DocDevice(options, savePath);
18
19// Render EPUB to DOCX
20renderer.Render(device, stream);
合并 HTML
Aspose.HTML for .NET API提供了 Renderer类,用于将HTML、MHTML、EPUB和SVG文档渲染和合并为流行格式。 RenderTo()方法可让您向输出呈现设备发送单个文档,而直接使用 Renderer 实例,您可以一次发送多个文件。 使用呈现器的实现:HtmlRenderer、SvgRenderer、MhtmlRenderer 和 EpubRenderer,你可以分别合并 HTML、SVG、MHTML 和 EPUB 文档。下一个示例演示了如何使用 HtmlRenderer 渲染多个 HTML 文档:
1// Merge HTML to PDF using C#
2
3// Prepare HTML code
4string code1 = @"<br><span style='color: green'>Hello, World!!</span>";
5string code2 = @"<br><span style='color: blue'>Hello, World!!</span>";
6string code3 = @"<br><span style='color: red'>Hello, World!!</span>";
7
8// Create three HTML documents to merge later
9using HTMLDocument document1 = new HTMLDocument(code1, ".");
10using HTMLDocument document2 = new HTMLDocument(code2, ".");
11using HTMLDocument document3 = new HTMLDocument(code3, ".");
12
13// Create an instance of HTML Renderer
14using HtmlRenderer renderer = new HtmlRenderer();
15
16// Prepare a path to save the converted file
17string savePath = Path.Combine(OutputDir, "merge-html.pdf");
18
19// Create an instance of PDF device
20using PdfDevice device = new PdfDevice(savePath);
21
22// Merge all HTML documents into PDF
23renderer.Render(device, document1, document2, document3);
设置超时
超时是呈现器可用的另一个重要功能。你可以用它来指定准备等待与文档生命周期相关的所有内部流程(如资源加载、活动计时器等)完成的时间。当然,您可以指定无限的等待时间。但是,如果文档包含一个无尽循环的脚本,您将无限期地等待。下面的示例演示了如何使用超时参数:
1// Render HTML to PDF with timeout settings using C#
2
3// Prepare HTML code
4string code = @"
5<script>
6 var count = 0;
7 setInterval(function()
8 {
9 var element = document.createElement('div');
10 var message = (++count) + '. ' + 'Hello, World!! I know how to use Renderers!';
11 var text = document.createTextNode(message);
12 element.appendChild(text);
13 document.body.appendChild(element);
14 }, 1000);
15</script>";
16
17// Initialize an HTML document based on prepared HTML code
18using HTMLDocument document = new HTMLDocument(code, ".");
19
20// Create an instance of HTML Renderer
21using HtmlRenderer renderer = new HtmlRenderer();
22
23// Prepare a path to save the converted file
24string savePath = Path.Combine(OutputDir, "output-timeout.pdf");
25
26// Create an instance of the PdfDevice class
27using PdfDevice device = new PdfDevice(savePath);
28
29// Render HTML to PDF
30renderer.Render(device, TimeSpan.FromSeconds(5), document);
在这个示例中,我们创建了一个 HTML 文档,每秒钟添加一个新的 <div>
元素,其中包含一个顺序号和一条信息 “Hello, World!!!"。我们使用
Render(device
, timeout
, document
) 方法中的 TimeSpan
参数来设置超时。该设置指定了呈现器呈现文档的最长时间。在本例中,总时间被设置为 5 秒。如果呈现器在指定时间内未生成文档,它将停止并呈现部分呈现的文档。运行这个示例,你将得到一个包含五行 “Hello, World!“的文档!我知道如何使用渲染器了!“的信息,如下所示: