翻譯|行業資訊|編輯:胡濤|2023-11-28 10:31:29.113|閱讀 116 次
概述:在本教程中,我們將向您展示如何將SVG 轉換為 PDF,以及如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將SVG 圖像添加到 PDF。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.PDF
Spire.PDF for .NET 提供了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
除了直接將SVG轉換為PDF外,它還支持將SVG圖像文件添加到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,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn