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: Convert SVG to PDF

在 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

以上便是如何在C#/VB.NET:将SVG 转换为 PDF,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 05:10:10       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 05:10:10       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 05:10:10       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 05:10:10       20 阅读

热门阅读

  1. js实现在线预览(PC)图片、pdf、excel、docx

    2023-12-06 05:10:10       46 阅读
  2. url转pdf或者html转pdf工具 — iText实现url转pdf

    2023-12-06 05:10:10       36 阅读
  3. 下一代实时数据库:Apache Doris 【四】扩容缩容

    2023-12-06 05:10:10       34 阅读
  4. kubekey部署k8s

    2023-12-06 05:10:10       40 阅读
  5. 初识MyBatis

    2023-12-06 05:10:10       48 阅读
  6. vue运用之echart柱状图3D效果案例代码

    2023-12-06 05:10:10       45 阅读
  7. CSS总结

    2023-12-06 05:10:10       37 阅读