本分步教程向您展示如何在 C# 中将 SVG 转换为 PDF。在 C# 中,使用 Aspose.SVG for .NET 可以轻松将 SVG 转换为 PDF,并且只需几行代码即可实现。
在 C# 代码中将 SVG 转换为 PDF 的步骤
- 从 NuGet.org 安装 Aspose.SVG for .NET 包
- 在 C# 中包含将 SVG 导出为 PDF 所需的四个命名空间
- 为 .NET 应用 Aspose.SVG 许可证以避免评估水印
- 将输入的 SVG 文件读入 SVGDocument 对象
- 使用 PdfRenderingOptions object 指定输出 PDF 属性
- 最后,通过将 SVG 渲染为 PdfDevice object 来保存输出 PDF
如果您按照上述步骤操作,创建 C# converter for SVG 到 PDF 格式的过程非常简单。在加载 SVG 图形图像格式并将其渲染到输出 PDF 设备对象之间,您可以指定输出 PDF 文件的属性。
在 C# 中将 SVG 转换为 PDF 的代码
using System; | |
//Add reference to Aspose.SVG for .NET API | |
//Use following namespaces to Convert SVG to PDF format | |
using Aspose.Svg; | |
using Aspose.Svg.Drawing; | |
using Aspose.Svg.Rendering; | |
using Aspose.Svg.Rendering.Pdf; | |
namespace ConvertSVGToPDF | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before converting SVG to PDF file format | |
//using Aspose.SVG for .NET | |
Aspose.Svg.License AsposeSVGLicense = new Aspose.Svg.License(); | |
AsposeSVGLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Load input SVG file | |
SVGDocument InputSVGDocument = new SVGDocument("InputSVGImage.svg"); | |
//Set PDF rendering options as you prefer | |
PdfRenderingOptions PDFRenderingOptions = new PdfRenderingOptions(); | |
PDFRenderingOptions.PageSetup.AnyPage = new Page( | |
new Size(600, 800), | |
new Margin(10, 10, 10, 10)); | |
PDFRenderingOptions.HorizontalResolution = 300; | |
PDFRenderingOptions.VerticalResolution = 300; | |
PDFRenderingOptions.JpegQuality = 95; | |
//Save output PDF by rendering to PDF Device | |
IDevice PDFDevice = new PdfDevice("PDFConvertedFromSVG.pdf"); | |
InputSVGDocument.RenderTo(PDFDevice); | |
} | |
} | |
} |
在上面的代码中,我们将 HorizontalResolution、VerticalResolution 和 JpegQuality 设置为默认值 300、300 和 95,只是为了让您了解如何在 C# SVG 到 PDF 转换中设置这些属性。但是,您可以根据需要更改这些属性值。