将 MHTML 转换为 PDF – C#
要利用 PDF 格式完成特定任务,通常需要将 MHTML 转换为 PDF。PDF 具有许多其他文件不具备的优点。例如,许多程序和应用程序都支持 PDF 文档;PDF 文件针对打印进行了优化,是创建文档实体副本的理想选择;您可以配置 PDF 文件的安全设置–禁止打印、编辑、使用电子签名等。
本文将介绍如何使用 Converter 类的 ConvertMHTML() 方法将 MHTML 转换为 PDF 以及如何应用 PdfSaveOptions 和 ICreateStreamProvider 参数。
在线 MHTML 转换器
您可以检查 Aspose.HTML API 功能并实时转换 MHTML。 请从本地文件系统加载 MHTML 文件,选择输出格式并运行示例。在示例中,默认设置了保存选项。您将立即收到一个单独文件的结果。
如果您想通过编程将 MHTML 转换为 PDF 格式,请参阅以下 C# 代码示例。
通过两行代码将 MHTML 转换为 PDF
Converter](3) 类的静态方法主要用于将 MHTML 代码转换为各种格式的最简单方法。例如,只需两行代码,您就可以在 C# 应用程序中将 MHTML 转换为 PDF!
1// Convert MHTML to PDF using C#
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Invoke the ConvertMHTML() method to convert MHTML to PDF
7Converter.ConvertMHTML(stream, new PdfSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.pdf"));
将 MHTML 转换为 PDF
使用 Converter.ConvertMHTML() 方法是将 MHTML 代码转换为各种格式的最常用方法。使用 Aspose.HTML,您可以通过编程将 MHTML 转换为 PDF 格式,并完全控制各种转换参数。
下面的 C# 代码片段展示了如何使用 Aspose.HTML for .NET 将 MHTML 转换为 PDF。
- 打开现有的 MHTML 文件。
- 创建 PdfSaveOptions 类的实例。
- 使用 Converter 类的 ConvertMHTML() 方法将 MHTML 保存为 PDF 文件。您需要向 ConvertMHTML() 方法传递 MHTML 文件流、PdfSaveOptions 和输出文件路径,以实现 MHTML 到 PDF 的转换。
在示例中,我们使用 System.IO.FileStream 类的 OpenRead() 方法打开并从指定路径的文件系统中读取文件。
1// Convert MHTML to PDF in C#
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "sample-output.pdf");
8
9// Create an instance of PdfSaveOptions
10PdfSaveOptions options = new PdfSaveOptions();
11
12// Call the ConvertMHTML() method to convert MHTML to PDF
13Converter.ConvertMHTML(stream, options, savePath);
PdfSaveOptions
Aspose.HTML 允许使用默认或自定义保存选项将 MHTML 转换为 PDF。使用 PdfSaveOptions 可以自定义渲染过程;您可以指定 page size、 margins、 file permissions、 CSS media-type 等。
Property | Description |
---|---|
JpegQuality | Specifies the quality of JPEG compression for images. The default value is 95. |
Css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
DocumentInfo | This property contains information about the output PDF document. |
BackgroundColor | This property sets the color that will fill the background of every page. By default, this property is Transparent. |
PageSetup | This property gets a page setup object and uses it for configuration output page-set. |
HorizontalResolution | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
VerticalResolution | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
Encryption | This property gets or sets encryption details. If it is not set, then no encryption will be performed. |
要了解有关 PdfSaveOptions 的更多信息,请阅读 微调转换器 一文。
使用 PdfSaveOptions 将 MHTML 转换为 PDF
要使用指定的 PdfSaveOptions 将 MHTML 转换为 PDF,应遵循以下几个步骤:
- 打开现有的 MHTML 文件。
- 创建一个新的 PdfSaveOptions 对象并指定保存选项。
- 使用 ConvertMHTML() 方法将 MHTML 保存为 PDF 文件。您需要向 ConvertMHTML() 方法传递 MHTML 文件流、PdfSaveOptions 和输出文件路径,以便将 MHTML 转换为 PDF。
下面的示例展示了如何使用 PdfSaveOptions,创建带有自定义保存选项的 PDF 文件:
1// Convert MHTML to PDF in C# with custom settings
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "sample-options.pdf");
8
9// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
10PdfSaveOptions options = new PdfSaveOptions()
11{
12 PageSetup =
13 {
14 AnyPage = new Page()
15 {
16 Size = new Aspose.Html.Drawing.Size(Length.FromPixels(3000), Length.FromPixels(1000))
17 }
18 },
19 BackgroundColor = System.Drawing.Color.AliceBlue
20};
21
22// Call the ConvertMHTML() method to convert MHTML to PDF
23Converter.ConvertMHTML(stream, options, savePath);
在本例中,我们使用 System.IO.FileStream 类的 OpenRead() 方法打开并从指定路径的文件系统中读取源文件。PdfSaveOptions()构造函数初始化了一个PdfSaveOptions类实例,并将其传递给ConvertMHTML()方法。ConvertMHTML()方法接收 stream
、options
、输出文件路径 savePath
并执行转换操作。PdfSaveOptions类提供了许多属性,让你可以完全控制各种参数,并改善将MHTML转换为PDF格式的过程。
在上述示例中,我们使用
BackgroundColor
属性,用于指定背景填充的颜色。默认的 BackgroundColor 是透明色。PageSetup
属性指定 page size(以像素为单位)。
输出流提供商
如果需要将文件保存在远程存储器(如云、数据库等)中,可以实现 ICreateStreamProvider接口来手动控制文件创建过程。该接口被设计为一个回调对象,用于在文档/页面开始时创建一个流(取决于输出格式),并在渲染文档/页面后释放早期创建的流。
下面的示例展示了如何在应用程序中实现和使用自己的MemoryStreamProvider:
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35}
下面的 C# 代码演示了如何使用 MemoryStreamProvider 类和 Aspose.HTML for .NET 库将 MHTML 转换为 PDF 并将结果保存到文件中。
1// Convert MHTML to PDF in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Open an existing MHTML file for reading
7using FileStream stream = File.OpenRead(DataDir + "sample.mht");
8
9// Prepare a path to save the converted file
10string savePath = Path.Combine(OutputDir, "stream-provider.pdf");
11
12// Convert MHTML to PDF by using the MemoryStreamProvider class
13Converter.ConvertMHTML(stream, new PdfSaveOptions(), streamProvider);
14
15// Get access to the memory stream that contains the result data
16MemoryStream memory = streamProvider.Streams.First();
17memory.Seek(0, SeekOrigin.Begin);
18
19// Flush the result data to the output file
20using (FileStream fs = File.Create(savePath))
21{
22 memory.CopyTo(fs);
23}
您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML 提供免费的在线 MHTML 到 PDF 转换器,可将 MHTML 转换为高质量的 PDF 文件,简单快捷。只需上传、转换您的文件,几秒钟就能得到结果!