将 SVG 转换为 PDF
Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。
行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将文本文件转换为 PDF
欢迎加入spire技术交流群:767755948
SVG 是一种矢量图形文件格式,用于创建可按比例缩放而不会降低质量的图像。然而,PDF 由于支持高质量打印、加密、数字签名和其他功能,更适合共享和打印。将 SVG 转换为 PDF 可确保图像在不同设备和环境下的良好显示效果,并能更好地保护知识产权。 在本教程中,我们将向您展示如何将 SVG 转换为 PDF,以及如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中将 SVG 图像添加到 PDF 中。
- 在 C# 和 VB.NET 中将 SVG 转换为 PDF
- 在 C# 和 VB.NET 中为 PDF 添加 SVG 图像
安装 Spire.PDF for .NET
首先,您需要将 Spire.PDF for .NET 软件包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。这些 DLL 文件既可以从这个链接下载,也可以通过 NuGet 安装。
1 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 文件
01 using Spire.Pdf;
02
03 namespace SVGtoPDF
04 {
05 class Program
06 {
07 static void Main(string[] args)
08 {
09 //Create a PdfDocument object
10 PdfDocument doc = new PdfDocument();
11
12 //Load a sample SVG file
13 doc.LoadFromSvg("Sample.svg");
14
15 //Save result document
16 doc.SaveToFile("Result.pdf", FileFormat.PDF);
17 doc.Dispose();
18 }
19 }
20 }
[VB.NET]
01 Imports Spire.Pdf
02
03 Namespace SVGtoPDF
04 Friend Class Program
05 Private Shared Sub Main(ByVal args As String())
06 'Create a PdfDocument object
07 Dim doc As PdfDocument = New PdfDocument()
08
09 'Load a sample SVG file
10 doc.LoadFromSvg("Sample.svg")
11
12 'Save result document
13 doc.SaveToFile("Result.pdf", FileFormat.PDF)
14 doc.Dispose();
15
16 End Sub
17 End Class
18 End Namespace
在 C# 和 VB.NET 中为 PDF 添加 SVG 图像
除了直接将 SVG 转换为 PDF 外,它还支持将 SVG 图像文件添加到 PDF 中的指定位置。请查看以下步骤:
- 创建一个PdfDocument对象,并使用PdfDocument.LoadFromSvg()加载一个SVG文件。LoadFromSvg()方法加载一个SVG文件。
- 使用PdfDocument.Pages[].CreateTemplate方法根据SVG文件的内容创建模板。Pages[].CreateTemplate()方法创建模板。
- 获取页面上模板的宽度和高度。
- 创建另一个 PdfDocument 对象,并使用 PdfDocument.LoadFromFile() 方法加载 PDF 文件。
- 使用PdfDocument.Pages[].Canvas.DrawTemplate()方法在指定位置绘制自定义尺寸的模板。
- 使用 PdfDocument.SaveToFile(String, FileFormat) 方法保存到 PDF 文件。
[C#]
01 using Spire.Pdf;
02 using Spire.Pdf.Graphics;
03 using System.Drawing;
04
05 namespace AddSVGImagetoPDF
06 {
07 class Program
08 {
09 static void Main(string[] args)
10 {
11 //Create a PdfDocument object
12 PdfDocument doc1 = new PdfDocument();
13
14 //Load an SVG file
15 doc1.LoadFromSvg("C:\\Users\\Administrator\\Desktop\\sample.svg");
16
17 //Create a template based on the content of the SVG file
18 PdfTemplate template = doc1.Pages[0].CreateTemplate();
19
20 //Get the width and height of the template
21 float width = template.Width;
22 float height = template.Height;
23
24 //Create another PdfDocument object
25 PdfDocument doc2 = new PdfDocument();
26
27 //Load a PDF file
28 doc2.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
29
30 //Draw the template with a custom size at a specified location
31 doc2.Pages[0].Canvas.DrawTemplate(template, new PointF(0, 0), new SizeF(width * 0.8f, height * 0.8f));
32
33 //Save to PDF file
34 doc2.SaveToFile("AddSvgToPdf.pdf", FileFormat.PDF);
35 doc2.Dispose();
36 }
37 }
38 }
[VB.NET]
01 Imports Spire.Pdf
02 Imports Spire.Pdf.Graphics
03 Imports System.Drawing
04
05 Namespace AddSVGImagetoPDF
06 Friend Class Program
07 Private Shared Sub Main(ByVal args As String())
08 'Create a PdfDocument object
09 Dim doc1 As PdfDocument = New PdfDocument()
10
11 'Load an SVG file
12 doc1.LoadFromSvg("C:\Users\Administrator\Desktop\sample.svg")
13
14 'Create a template based on the content of the SVG file
15 Dim template As PdfTemplate = doc1.Pages(0).CreateTemplate()
16
17 'Get the width and height of the template
18 Dim width As Single = template.Width
19 Dim height As Single = template.Height
20
21 'Create another PdfDocument object
22 Dim doc2 As PdfDocument = New PdfDocument()
23
24 'Load a PDF file
25 doc2.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
26
27 'Draw the template with a custom size at a specified location
28 doc2.Pages(0).Canvas.DrawTemplate(template, New PointF(0, 0), New SizeF(width * 0.8F, height * 0.8F))
29
30 'Save to PDF file
31 doc2.SaveToFile("AddSvgToPdf.pdf", FileFormat.PDF)
32 doc2.Dispose()
33 End Sub
34 End Class
35 End Namespace
若想从生成的文档中删除评估信息,或解除功能限制,申请 30 天试用许可证。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线