將 SVG 轉(zhuǎn)換為 PDF
Spire.PDF for .NET 是一款專門對 Word 文檔進(jìn)行操作的 .NET 類庫。致力于在于幫助開發(fā)人員輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數(shù)。當(dāng)我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設(shè)置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現(xiàn)上述所有功能。本文將介紹如何將文本文件轉(zhuǎn)換為 PDF
歡迎加入spire技術(shù)交流群:767755948
SVG 是一種矢量圖形文件格式,用于創(chuàng)建可按比例縮放而不會降低質(zhì)量的圖像。然而,PDF 由于支持高質(zhì)量打印、加密、數(shù)字簽名和其他功能,更適合共享和打印。將 SVG 轉(zhuǎn)換為 PDF 可確保圖像在不同設(shè)備和環(huán)境下的良好顯示效果,并能更好地保護(hù)知識產(chǎn)權(quán)。 在本教程中,我們將向您展示如何將 SVG 轉(zhuǎn)換為 PDF,以及如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中將 SVG 圖像添加到 PDF 中。
- 在 C# 和 VB.NET 中將 SVG 轉(zhuǎn)換為 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 轉(zhuǎn)換為 PDF
Spire.PDF for .NET 提供了 PdfDocument.SaveToFile(String, FileFormat) 方法,允許用戶將 SVG 文件保存為 PDF。具體步驟如下。
- 創(chuàng)建一個 PdfDocument 對象。
- 使用 PdfDocument.LoadFromFile() 方法加載 SVG 示例文件。
- 使用 PdfDocument.SaveToFile(String, FileFormat) 方法將 SVG 文件轉(zhuǎn)換為 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 轉(zhuǎn)換為 PDF 外,它還支持將 SVG 圖像文件添加到 PDF 中的指定位置。請查看以下步驟:
	
- 創(chuàng)建一個PdfDocument對象,并使用PdfDocument.LoadFromSvg()加載一個SVG文件。LoadFromSvg()方法加載一個SVG文件。
- 使用PdfDocument.Pages[].CreateTemplate方法根據(jù)SVG文件的內(nèi)容創(chuàng)建模板。Pages[].CreateTemplate()方法創(chuàng)建模板。
- 獲取頁面上模板的寬度和高度。
- 創(chuàng)建另一個 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交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線