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,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Free Spire.PDFViewer for .NETSpire.PDFViewer for .NET 的免费版本,使用 Free Spire.PDFViewer,程序员可以在 WinForm 程序中加载并查看 PDF 文档。浏览 PDF 时,用户可以自由设置浏览选项,比如:自动适应纸张/自动适应窗口,上一页/下一页,放大/缩小,等等。Free Spire.PDFViewer 支持浏览含多种元素的 PDF,这些元素包括超链接,字体(TrueType, Type 0, Type 1, Type 3, OpenType and CJK font),图形,表格, Device-Dependent Color Spaces, DCT,JPEG2000 等图片格式。 此外,该控件还支持纵向、横向和自动打印文档,支持将 PDF 保存为多种流行的图片格式(.bmp, .png, .jpeg)。作为一款独立的 PDF 查看组件,Free Spire.PDFViewer 的运行不依赖 Adobe Reader 及其他任何第三方软件。 主要功能点: 1.从文件,流,字节数组读取文档 2.加载浏览加密的 PDF 文档 3.设置浏览选项(页面跳转,缩放,自适应页面大小,旋转,单页或多页显示) 4.显示缩略图 5.识别书签目录及跳转到目标位置 6.存取附件到本地硬盘 7.支持多种打印方式:横向,纵向,自动 8.保存为图片(.bmp, .png, .jpeg) 9.支持多种PDF页面元素(超链接,字体,图形,图片,表格) 技术特点: 1.支持 .NET Framework 2.0, 3.0, 3.5, 4.0 2.仅支持 Windows Form 3.支持ASCIIHex, ASCII85, Flate, LZW, Run Length, CCITT Fax, DCT, JPX 解码 4.控件完全由 C# 代码开发 5.独立组件,无需 Adobe Reader 及其他任何第三方软件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值