PDF控件Spire.PDF for .NET【转换】演示:将多个图像转换为单个 PDF

如果您想要将多个图像合并为一个文件以便于分发或存储,将它们转换为单个 PDF 文档是一个很好的解决方案。这一过程不仅节省空间,还确保所有图像都保存在一个文件中,方便共享或传输。在本文中,您将学习如何使用Spire.PDF for .NET 在 C# 和 VB.NET 中将多个图像合并到单个 PDF 文档中

Spire.Doc 是一款专门对 Word 文档进行操作的 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式 (qun:767755948)

Spire.PDF for.net下载   Spire.PDF for java下载

安装适用于 .NET 的 Spire.PDF

首先,您需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.PDF
使用 C# 和 VB.NET 将多个图像合并为单个 PDF

为了将文件夹中的所有图像转换为 PDF,我们迭代每个图像,向 PDF 添加一个与图像大小相同的新页面,然后将图像绘制到新页面上。以下是详细步骤。

  • 创建一个PdfDocument对象。
  • 使用PdfDocument.PageSettings.SetMargins()方法将页边距设置为零。
  • 获取存储图像的文件夹。
  • 遍历文件夹中的每个图像文件,并获取特定图像的宽度和高度。
  • 使用PdfDocument.Pages.Add()方法将与图像具有相同宽度和高度的新页面添加到 PDF 文档中。
  • 使用PdfPageBase.Canvas.DrawImage()方法在页面上绘制图像。
  • 使用PdfDocument.SaveToFile()方法保存文档。

【C#】

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace ConvertMultipleImagesIntoPdf
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Set the page margins to 0
doc.PageSettings.SetMargins(0);

//Get the folder where the images are stored
DirectoryInfo folder = new DirectoryInfo(@"C:\Users\Administrator\Desktop\Images");

//Iterate through the files in the folder
foreach (FileInfo file in folder.GetFiles())
{
//Load a particular image
Image image = Image.FromFile(file.FullName);

//Get the image width and height
float width = image.PhysicalDimension.Width;
float height = image.PhysicalDimension.Height;

//Add a page that has the same size as the image
PdfPageBase page = doc.Pages.Add(new SizeF(width, height));

//Create a PdfImage object based on the image
PdfImage pdfImage = PdfImage.FromImage(image);

//Draw image at (0, 0) of the page
page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height);
}

//Save to file
doc.SaveToFile("CombinaImagesToPdf.pdf");
doc.Dispose();
}
}
}

【VB.NET】

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace ConvertMultipleImagesIntoPdf
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()

'Set the page margins to 0
doc.PageSettings.SetMargins(0)

'Get the folder where the images are stored
Dim folder As DirectoryInfo = New DirectoryInfo("C:\Users\Administrator\Desktop\Images")

'Iterate through the files in the folder
Dim file As FileInfo
For Each file In folder.GetFiles()
'Load a particular image
Dim image As Image = Image.FromFile(file.FullName)

'Get the image width and height
Dim width As single = image.PhysicalDimension.Width
Dim height As single = image.PhysicalDimension.Height

'Add a page that has the same size as the image
Dim page As PdfPageBase = doc.Pages.Add(New SizeF(width,height))

'Create a PdfImage object based on the image
Dim pdfImage As PdfImage = PdfImage.FromImage(image)

'Draw image at (0, 0) of the page
page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height)
Next

'Save to file
doc.SaveToFile("CombinaImagesToPdf.pdf")
doc.Dispose()
End Sub
End Class
End Namespace

C#/VB.NET:将多个图像转换为单个 PDF

以上便是如何在C#/VB.NET中将多个图像转换为单个 PDF,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

最近更新

  1. TCP协议是安全的吗?

    2023-12-19 08:10:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-19 08:10:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-19 08:10:03       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-19 08:10:03       20 阅读

热门阅读

  1. 服务器过载有哪些情况

    2023-12-19 08:10:03       40 阅读
  2. 第一次使用ThreadPoolExecutor遇到的问题

    2023-12-19 08:10:03       46 阅读
  3. 聊聊reactor-logback的AsyncAppender

    2023-12-19 08:10:03       39 阅读
  4. Scrapy+Selenium项目实战--携程旅游信息爬虫

    2023-12-19 08:10:03       31 阅读
  5. MySQL数据库的缓存

    2023-12-19 08:10:03       41 阅读
  6. Vue中表单数据和过滤器的简单使用

    2023-12-19 08:10:03       36 阅读
  7. 51单片机4线并发IO口控制1602LCD

    2023-12-19 08:10:03       38 阅读
  8. 第五章 图论 邻接矩阵存图

    2023-12-19 08:10:03       31 阅读
  9. strings

    2023-12-19 08:10:03       37 阅读
  10. Rust 嵌入式开发

    2023-12-19 08:10:03       39 阅读