翻譯|使用教程|編輯:胡濤|2023-06-26 13:42:06.977|閱讀 171 次
概述:在本文中,您將了解如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將圖像轉換為 PDF,,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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下載
圖像是一種設備友好的文件格式,可以在各種設備之間輕松共享。然而,在某些情況下,需要更專業的格式(例如PDF)來代替圖像。在本文中,您將了解如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將圖像轉換為 PDF。
Spire.PDF 沒有提供將圖像轉換為 PDF 的簡單方法。但您可以創建一個新的 PDF 文檔并在特定頁面的指定位置繪制圖像。根據是否生成與圖像頁面大小相同的 PDF,本主題可以分為以下兩個子主題。
首先,您需要將包含在 Spire.PDF for.NET 包中的 DLL 文件添加為您的 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.PDF
以下是使用 Spire.PDF for .NET 添加圖像作為新 PDF 文檔的一部分的步驟。
【C#】
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace AddImageToPdf
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margins
doc.PageSettings.SetMargins(20);
//Add a page
PdfPageBase page = doc.Pages.Add();
//Load an image
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\announcement.jpg");
//Get the image width and height
float width = image.PhysicalDimension.Width;
float height = image.PhysicalDimension.Height;
//Declare a PdfImage variable
PdfImage pdfImage;
//If the image width is larger than page width
if (width > page.Canvas.ClientSize.Width)
{
//Resize the image to make it to fit to the page width
float widthFitRate = width / page.Canvas.ClientSize.Width;
Size size = new Size((int)(width / widthFitRate), (int)(height / widthFitRate));
Bitmap scaledImage = new Bitmap(image, size);
//Load the scaled image to the PdfImage object
pdfImage = PdfImage.FromImage(scaledImage);
} else
{
//Load the original image to the PdfImage object
pdfImage = PdfImage.FromImage(image);
}
//Draw image at (0, 0)
page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height);
//Save to file
doc.SaveToFile("AddImage.pdf");
}
}
}
【VB.NET】
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace AddImageToPdf
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()
'Set the margins
doc.PageSettings.SetMargins(20)
'Add a page
Dim page As PdfPageBase = doc.Pages.Add()
'Load an image
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\announcement.jpg")
'Get the image width and height
Dim width As single = image.PhysicalDimension.Width
Dim height As single = image.PhysicalDimension.Height
'Declare a PdfImage variable
Dim pdfImage As PdfImage
'If the image width is larger than page width
If width > page.Canvas.ClientSize.Width Then
'Resize the image to make it to fit to the page width
Dim widthFitRate As single = width / page.Canvas.ClientSize.Width
Dim size As Size = New Size(CType((width / widthFitRate),(Integer)(height / widthFitRate), Integer))
Dim scaledImage As Bitmap = New Bitmap(image,size)
'Load the scaled image to the PdfImage object
pdfImage = PdfImage.FromImage(scaledImage)
Else
'Load the original image to the PdfImage object
pdfImage = PdfImage.FromImage(image)
End If
'Draw image at (0, 0)
page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height)
'Save to file
doc.SaveToFile("AddImage.pdf")
End Sub
End Class
End Namespace
	 
 
以下是使用 Spire.PDF for .NET 將圖像轉換為與圖像具有相同頁面寬度和高度的 PDF 的步驟。
【C#】
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace ConvertImageToPdfWithSameSize
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the margins to 0
doc.PageSettings.SetMargins(0);
//Load an image
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\announcement.jpg");
//Get the image width and height
float width = image.PhysicalDimension.Width;
float height = image.PhysicalDimension.Height;
//Add a page of 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("ConvertPdfWithSameSize.pdf");
}
}
}
【VB.NET】
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace ConvertImageToPdfWithSameSize
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()
'Set the margins to 0
doc.PageSettings.SetMargins(0)
'Load an image
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\announcement.jpg")
'Get the image width and height
Dim width As single = image.PhysicalDimension.Width
Dim height As single = image.PhysicalDimension.Height
'Add a page of 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)
'Save to file
doc.SaveToFile("ConvertPdfWithSameSize.pdf")
End Sub
End Class
End Namespace
	 
以上便是如何將圖像轉換為 PDF,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn