PDF控件Spire.PDF for .NET【转换】演示:将 SVG 转换为 PDF
SVG 是一种矢量图形文件格式,用于创建可缩放且不损失质量的图像。然而,PDF由于支持高质量打印、加密、数字签名等功能,更适合共享和打印。将SVG转换为PDF可以保证图像在不同设备和环境下都有良好的显示效果,并更好地保护知识产权。在本教程中,我们将向您展示如何将SVG 转换为 PDF,以及如何使用Spire.PDF for .NET在 C# 和 VB.NET 中将SVG 图像添加到 PDF。
Spire.Doc?是一款专门对 Word 文档进行操作的 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。?
E-iceblue?功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式(qun:767755948)
Spire.PDF for.net下载? ?Spire.PDF for java下载
安装适用于 .NET 的 Spire.PDF
首先,您需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中将 SVG 转换为 PDF
Spire.PDF for .NET 提供了PdfDocument.SaveToFile(String, FileFormat)方法,允许用户将 SVG 文件另存为 PDF。详细步骤如下。
- 创建一个PdfDocument对象。
- 使用PdfDocument.LoadFromFile()方法加载示例 SVG 文件。
- 使用PdfDocument.SaveToFile(String, FileFormat)方法将 SVG 文件转换为 PDF?。
[C#]
using Spire.Pdf;
namespace SVGtoPDF
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a sample SVG file
doc.LoadFromSvg("Sample.svg");
//Save result document
doc.SaveToFile("Result.pdf", FileFormat.PDF);
doc.Dispose();
}
}
}
[VB.NET]
Imports Spire.Pdf
Namespace SVGtoPDF
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()
'Load a sample SVG file
doc.LoadFromSvg("Sample.svg")
'Save result document
doc.SaveToFile("Result.pdf", FileFormat.PDF)
doc.Dispose();
End Sub
End Class
End Namespace

在 C# 和 VB.NET 中将 SVG 图像添加到 PDF
除了直接将SVG转换为PDF外,它还支持将SVG图像文件添加到PDF中的指定位置。请检查以下步骤:
- 创建PdfDocument对象并使用 PdfDocument 加载 SVG 文件。LoadFromSvg()方法。
- 使用 PdfDocument 根据 SVG 文件的内容创建模板。?Pages[].CreateTemplate()方法。
- 获取页面上模板的宽度和高度。
- 创建另一个PdfDocument对象并使用PdfDocument.LoadFromFile()方法加载 PDF 文件。
- 使用PdfDocument.Pages[].Canvas.DrawTemplate()方法在指定位置绘制自定义尺寸的模板。
- 使用PdfDocument.SaveToFile(String, FileFormat)方法保存为 PDF 文件。
[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddSVGImagetoPDF
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc1 = new PdfDocument();
//Load an SVG file
doc1.LoadFromSvg("C:\\Users\\Administrator\\Desktop\\sample.svg");
//Create a template based on the content of the SVG file
PdfTemplate template = doc1.Pages[0].CreateTemplate();
//Get the width and height of the template
float width = template.Width;
float height = template.Height;
//Create another PdfDocument object
PdfDocument doc2 = new PdfDocument();
//Load a PDF file
doc2.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Draw the template with a custom size at a specified location
doc2.Pages[0].Canvas.DrawTemplate(template, new PointF(0, 0), new SizeF(width * 0.8f, height * 0.8f));
//Save to PDF file
doc2.SaveToFile("AddSvgToPdf.pdf", FileFormat.PDF);
doc2.Dispose();
}
}
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing
Namespace AddSVGImagetoPDF
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument object
Dim doc1 As PdfDocument = New PdfDocument()
'Load an SVG file
doc1.LoadFromSvg("C:\Users\Administrator\Desktop\sample.svg")
'Create a template based on the content of the SVG file
Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate()
'Get the width and height of the template
Dim width As Single = template.Width
Dim height As Single = template.Height
'Create another PdfDocument object
Dim doc2 As PdfDocument = New PdfDocument()
'Load a PDF file
doc2.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
'Draw the template with a custom size at a specified location
doc2.Pages(0).Canvas.DrawTemplate(template, New PointF(0, 0), New SizeF(width * 0.8F, height * 0.8F))
'Save to PDF file
doc2.SaveToFile("AddSvgToPdf.pdf", FileFormat.PDF)
doc2.Dispose()
End Sub
End Class
End Namespace

以上便是如何在C#/VB.NET:将SVG 转换为 PDF,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!