本分步教程向您展示了如何在 C# 中将图像转换为 PDF。此示例代码可帮助您使用几行代码快速将 JPG 图像转换为 PDF 文档。此外,您可以使用 C#** 轻松地将图像转换为 PDF,用于其他图像格式,例如 PNG、TIFF、BMP 等。
在 C# 中将图像转换为 PDF 的步骤
- 从 NuGet 设置 GroupDocs.Conversion for .NET 以使用 C# 将图像转换为 PDF
- 添加 GroupDocs.Conversion 命名空间
- 创建 Converter 类的实例并加载图像以转换为 PDF
- 创建 PdfConvertOptions 类的实例
- 调用 Converter 类的 Convert 方法,提供 PDF 文件的名称和选项
这些步骤涵盖了您需要了解的有关设置项目和使用 C# 图像到 PDF 转换 功能的所有信息。要将 JPG 文件转换为 PDF,您必须首先通过 NuGet 包管理器安装所需的库。最后,您可以定义转换后的 PDF 文件的设置并将其保存到磁盘。
在 C# 中将图像转换为 PDF 的代码
using System; | |
using GroupDocs.Conversion.Options.Convert; | |
namespace ConvertImageToPdfInCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to convert Image 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 Image file for conversion to PDF | |
var converter = new GroupDocs.Conversion.Converter("sample.jpg"); | |
// Set the convert options for PDF format | |
var options = new PdfConvertOptions(); | |
// Save converted PDF file | |
converter.Convert("converted.pdf", options); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
我们在本教程中讨论了如何在 C#* 中将 *Image 转换为 PDF。此外,您可以将图像转换为其他文档格式,包括 Microsoft Word、Excel 和 Powerpoint 格式。在本例中,我们使用 JPG 转换为 PDF,您可以使用 PNG、BMP、GIF 等其他图像格式进行转换。
如果您想在 C# 中将 PDF 转换为 Word,请参阅我们的其他指南。