本教程重点介绍通过使用一种流行的文档转换包将电子书文件转换为不可编辑格式的过程,并提供有关如何使用 C# 将 EPUB 转换为 PDF 的完整工作流。此外,这篇文章将指导您如何使用文档转换器 API 以在 C# 中将电子书转换器写入 PDF。以下是将 EPUB 转换为 PDF 的主要步骤以及示例应用程序。
使用 C# 将 EPUB 转换为 PDF 的步骤
- 从 .NET 应用程序中的 NuGet 包管理器安装 GroupDocs.Conversion for .NET 包以将 EPUB 转换为 PDF
- 添加对 GroupDocs.Conversion 命名空间的引用以将 EPUB 格式转换为 PDF 格式
- 创建 Converter 类的实例,用于从磁盘加载输入的 EPUB 文件以转换为 PDF 格式
- 创建和定义自定义输出 PDF 文件的属性
- 调用 Converter 类的 Convert 方法将 EPUB 保存为磁盘上的 PDF
这些是使用 C#* 实现*将 EPUB 文件转换为 PDF 的功能的要点。这些步骤与平台无关,可以在 Windows、macOS 和 Linux 等任何常见操作系统上轻松使用,无需设置任何其他软件。您必须编写几行代码,其中包含用于将电子书转换为 PDF 的上述库的简单 API 调用,您还可以通过使用 PdfConvertOptions 对象定义各种属性来自定义转换后的文件。
使用 C# 将 EPUB 转换为 PDF 的代码
using System; | |
using GroupDocs.Conversion.Options.Convert; | |
namespace ConvertEpubToPdfUsingCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to convert EPUB to PDF using C# | |
{ | |
// Remove the watermark in output PDF document by adding license | |
string licensePath = "GroupDocs.Conversion.lic"; | |
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License(); | |
lic.SetLicense(licensePath); | |
// Load the source EPUB file for conversion to PDF | |
var converter = new GroupDocs.Conversion.Converter("sample.epub"); | |
// Set the conversion options for PDF document to customize the output file | |
PdfConvertOptions options = new PdfConvertOptions(); | |
// Convert and save the EPUB in PDF format | |
converter.Convert("converted.pdf", options); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
上面的代码片段演示了如何开发应用程序以在 C# 中将 EPUB 更改为 PDF。我们通过设置所需的包并在代码中包含必要的命名空间来初始化文档转换工作流程。完成这些步骤后,我们使用 Converter 类加载输入的 EPUB 文件,并实例化 PdfConvertOptions 类来设置参数以自定义转换后的 PDF 文档。在最后一步中,我们调用了 Convert 方法将 EPUB 文件转换为 PDF 并存储在磁盘上。
我们已经讨论了在 C# 中将 EPUB 转换为 PDF 的文档转换过程,并为其开发了一个示例。最近,我们发表了一篇在 C# 中将 SVG 更改为 JPG 的文章,请查看 如何使用 C# 将 SVG 转换为 JPG 指南以获取更多信息。