PDF控件Spire.PDF for .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下载

图像是一种设备友好的文件格式,可以在各种设备之间轻松共享。然而,在某些情况下,需要更专业的格式(例如PDF)来代替图像。在本文中,您将了解如何使用Spire.PDF for .NET在 C# 和 VB.NET 中将图像转换为 PDF。(qun:767755948)

Spire.PDF 没有提供将图像转换为 PDF 的简单方法。但您可以创建一个新的 PDF 文档并在特定页面的指定位置绘制图像。根据是否生成与图像页面大小相同的 PDF,本主题可以分为以下两个子主题。

安装 Spire.PDF for.NET

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

PM> Install-Package Spire.PDF
将图像添加到 PDF 的指定位置

以下是使用 Spire.PDF for .NET 添加图像作为新 PDF 文档的一部分的步骤。

  • 创建一个PdfDocument对象。
  • 使用PdfDocument.PageSettings.SetMargins()方法设置页边距。
  • 使用PdfDocument.Pages.Add()方法添加页面
  • 使用Image.FromFile()方法加载图像,并获取图像的宽度和高度。
  • 如果图像宽度大于页面(内容区域)宽度,请调整图像大小以使其适合页面宽度。
  • 基于缩放图像或原始图像创建PdfImage对象。
  • 使用PdfPageBase.Canvas.DrawImage()方法在第一页 (0, 0) 处绘制 PdfImage 对象。
  • 使用PdfDocument.SaveToFile()方法将文档保存为 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

将图像转换为具有相同宽度和高度的 PDF

以下是使用 Spire.PDF for .NET 将图像转换为与图像具有相同页面宽度和高度的 PDF 的步骤。

  • 创建一个PdfDocument对象。
  • 使用PdfDocument.PageSettings.SetMargins()方法将页边距设置为零。
  • 使用Image.FromFile()方法加载图像,并获取图像的宽度和高度。
  • 使用PdfDocument.Pages.Add()方法根据图像的大小将页面添加到 PDF 。
  • 根据图像创建一个PdfImage对象。
  • 使用PdfPageBase.Canvas.DrawImage()方法从坐标 (0, 0) 在第一页上绘制 PdfImage 对象。
  • 使用PdfDocument.SaveToFile()方法将文档保存为 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,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,~

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/131410625
今日推荐