翻譯|行業資訊|編輯:胡濤|2023-12-18 11:30:41.037|閱讀 72 次
概述:在本文中,您將學習如何使用Spire.PDF for .NET 在 C# 和 VB.NET 中將多個圖像合并到單個 PDF 文檔中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
如果您想要將多個圖像合并為一個文件以便于分發或存儲,將它們轉換為單個 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 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.PDF
為了將文件夾中的所有圖像轉換為 PDF,我們迭代每個圖像,向 PDF 添加一個與圖像大小相同的新頁面,然后將圖像繪制到新頁面上。以下是詳細步驟。
【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,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn