將多個圖像轉換成一個 PDF 文件
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發人員輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入spire技術交流群:767755948
如果您有多個圖像,想合并成一個文件以便于分發或存儲,將它們轉換成一個 PDF 文檔是一個很好的解決方案。這一過程不僅能節省空間,還能確保所有圖像都保存在一個文件中,方便共享或傳輸。在本文中,您將學習如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中將多個圖像合并為一個 PDF 文檔。
安裝 Spire.PDF for .NET
首先,您需要將 Spire.PDF for.NET 軟件包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。DLL 文件既可以從這個鏈接下載,也可以通過 NuGet 安裝。
1 PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中將多個圖像合并為一個 PDF
為了將文件夾中的所有圖像轉換為 PDF,我們需要遍歷每張圖像,在 PDF 中添加與圖像大小相同的新頁面,然后將圖像繪制到新頁面上。以下是詳細步驟。
- 創建一個 PdfDocument 對象。
- 使用 PdfDocument.PageSettings.SetMargins() 方法將頁邊距設置為零。
- 獲取存儲圖像的文件夾。
- 遍歷文件夾中的每個圖像文件,并獲取特定圖像的寬度和高度。
- 使用PdfDocument.Pages.Add()方法在PDF文檔中添加寬度和高度與圖像相同的新頁面.
- 使用 PdfPageBase.Canvas.DrawImage() 方法在頁面上繪制圖像。
- 使用PdfDocument.SaveToFile()方法保存文檔。
01 using Spire.Pdf; 02 using Spire.Pdf.Graphics; 03 using System.Drawing; 04 05 namespace ConvertMultipleImagesIntoPdf 06 { 07 class Program 08 { 09 static void Main(string[] args) 10 { 11 //Create a PdfDocument object 12 PdfDocument doc = new PdfDocument(); 13 14 //Set the page margins to 0 15 doc.PageSettings.SetMargins(0); 16 17 //Get the folder where the images are stored 18 DirectoryInfo folder = new DirectoryInfo(@"C:\Users\Administrator\Desktop\Images"); 19 20 //Iterate through the files in the folder 21 foreach (FileInfo file in folder.GetFiles()) 22 { 23 //Load a particular image 24 Image image = Image.FromFile(file.FullName); 25 26 //Get the image width and height 27 float width = image.PhysicalDimension.Width; 28 float height = image.PhysicalDimension.Height; 29 30 //Add a page that has the same size as the image 31 PdfPageBase page = doc.Pages.Add(new SizeF(width, height)); 32 33 //Create a PdfImage object based on the image 34 PdfImage pdfImage = PdfImage.FromImage(image); 35 36 //Draw image at (0, 0) of the page 37 page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height); 38 } 39 40 //Save to file 41 doc.SaveToFile("CombinaImagesToPdf.pdf"); 42 doc.Dispose(); 43 } 44 } 45 }
[VB.NET]
01 Imports Spire.Pdf 02 Imports Spire.Pdf.Graphics 03 Imports System.Drawing 04 05 Namespace ConvertMultipleImagesIntoPdf 06 Class Program 07 Shared Sub Main(ByVal args() As String) 08 'Create a PdfDocument object 09 Dim doc As PdfDocument = New PdfDocument() 10 11 'Set the page margins to 0 12 doc.PageSettings.SetMargins(0) 13 14 'Get the folder where the images are stored 15 Dim folder As DirectoryInfo = New DirectoryInfo("C:\Users\Administrator\Desktop\Images") 16 17 'Iterate through the files in the folder 18 Dim file As FileInfo 19 For Each file In folder.GetFiles() 20 'Load a particular image 21 Dim image As Image = Image.FromFile(file.FullName) 22 23 'Get the image width and height 24 Dim width As single = image.PhysicalDimension.Width 25 Dim height As single = image.PhysicalDimension.Height 26 27 'Add a page that has the same size as the image 28 Dim page As PdfPageBase = doc.Pages.Add(New SizeF(width,height)) 29 30 'Create a PdfImage object based on the image 31 Dim pdfImage As PdfImage = PdfImage.FromImage(image) 32 33 'Draw image at (0, 0) of the page 34 page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height) 35 Next 36 37 'Save to file 38 doc.SaveToFile("CombinaImagesToPdf.pdf") 39 doc.Dispose() 40 End Sub 41 End Class 42 End Namespace

申請臨時許可證
若想從生成的文檔中刪除評估信息,或解除功能限制,申請 30 天試用許可證。