这篇简短的文章解释了在 C#** 中将 Text 转换为 PDF 的分步过程。我们将使用一种流行的文档转换 API 来执行文档转换,并开发 C# Text to PDF 功能的工作示例。您可以在下面找到使用 C# 将文本转换为 PDF 的分步说明和示例代码。
在 C# 中将文本转换为 PDF 的步骤
- 在 .NET 应用程序中从 NuGet 安装 GroupDocs.Conversion for .NET 包以将文本转换为 PDF
- 添加对 GroupDocs.Conversion 命名空间的引用以开发 Text to PDF 功能
- 实例化 Converter 类并将输入文本文件传递给它的构造函数
- 创建 PdfConvertOptions 类的对象并定义自定义 PDF 文档的属性
- 调用 Converter 类的 Convert 方法将 Text 保存为 PDF 文档
convert TXT to PDF C# 功能可以按照以上几点来实现。文档转换可以通过编写几行代码来完成,这些代码由文档转换库的 API 调用组成。这些步骤不需要安装任何第三方软件,并且可以在支持 .NET 环境的任何操作系统上使用,例如 MS Windows、Linux 和 Mac OS。
在 C# 中将文本转换为 PDF 的代码
using System; | |
using GroupDocs.Conversion.Options.Convert; | |
namespace ConvertTextToPdfInCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to convert Text 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 Text file for conversion to PDF | |
var converterObj = new GroupDocs.Conversion.Converter("sample.txt"); | |
// Set the conversion options for PDF document to customize the output file | |
PdfConvertOptions options = new PdfConvertOptions(); | |
// Convert and save the TXT in PDF format | |
converterObj.Convert("converted.pdf", options); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
在上面的代码片段中,我们创建了 C# convert TXT to PDF 功能以进行演示。您可以在示例中看到,Converter 类用于在添加基本命名空间后加载输入文本文件以进行转换。然后,实例化 PdfConvertOptions 类,用于定义自定义输出 PDF 文档的参数。最后,Convert 方法可以将生成的文件存储到光盘中。
我们已经讨论了创建文本到 PDF C# 功能的文档转换过程。最近,我们发表了一篇使用 C# 将 SVG 转换为 PDF 的文章,请查看 如何在 C# 中将 SVG 转换为 PDF 指南了解更多信息。