将 SVG 转换为 PDF – C#
让我们来看看如何将 SVG 文档转换为便携文档格式(PDF)文件格式。使用 Aspose.HTML,您可以通过编程将 SVG 转换为 PDF 格式,并完全控制各种转换参数。
在本文中,您将了解如何使用 Converter 类的 ConvertSVG() 方法将 SVG 转换为 PDF,以及如何应用 PdfSaveOptions 和 ICreateStreamProvider 参数。 此外,您还可以尝试使用在线 SVG 转换器来测试 Aspose.HTML API 的功能并即时转换 SVG。
在线 SVG 转换器
您可以使用 Aspose.HTML API 将 SVG 实时转换为其他格式。请从本地文件系统加载 SVG,选择输出格式并运行示例。保存选项为默认设置。您将立即以单独文件的形式收到转换结果。
如果您想通过编程将 SVG 转换为 PDF 文件,请参阅以下 C# 代码示例。
只需一行代码即可将 SVG 转换为 PDF
Converter 类的静态方法主要用作将 SVG 文件转换为各种格式的最简单方法。只需一行代码,您就可以在 C# 应用程序中将 SVG 转换为 PDF!
在下面的示例中,我们将本地文件系统中的 SVG 文件( shapes.svg)转换并保存到本地文件系统中。
1// Convert SVG to PDF in C#
2
3// Invoke the ConvertSVG() method for SVG to PDF conversion
4Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));
将 SVG 转换为 PDF
使用 ConvertSVG() 方法将文件转换为另一种格式是一系列操作,其中包括文件加载和保存。在下面的示例中,我们通过代码创建一个 SVG 文件。
- 为 SVG 文档编写代码
- 创建一个新的 PdfSaveOptions 对象。
- 使用 Converter 类的
ConvertSVG(
content
,baseUri
,options
,outputPath
) 方法将 SVG 保存为 PDF 文件。
请看下面的 C# 代码片段,它显示了使用 Aspose.HTML for .NET 将 SVG 转换为 PDF 的过程。
1// Convert SVG to PDF using C#
2
3// Prepare SVG code
4string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
5 "<circle cx ='100' cy ='100' r ='50' fill='none' stroke='red' stroke-width='5' />" +
6 "</svg>";
7
8// Prepare a path for converted file saving
9string savePath = Path.Combine(OutputDir, "circle.pdf");
10
11// Initialize PdfSaveOptions
12PdfSaveOptions options = new PdfSaveOptions();
13
14// Convert SVG to PDF
15Converter.ConvertSVG(code, ".", options, savePath);
您可以从 GitHub 下载完整的示例和数据文件。
PdfSaveOptions
Aspose.HTML 允许使用默认或自定义保存选项将 SVG 转换为 PDF。通过 PdfSaveOptions,您可以自定义渲染过程;您可以指定页面大小、页边距、背景颜色、文件权限、Css 等。
Property | Description |
---|---|
JpegQuality | Specifies the quality of JPEG compression for images. The default value is 95. |
Css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
DocumentInfo | This property contains information about the output PDF document. |
BackgroundColor | This property sets the color that will fill the background of every page. By default, this property is Transparent. |
PageSetup | This property gets a page setup object and uses it for configuration output page-set. |
HorizontalResolution | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
VerticalResolution | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
Encryption | This property gets or sets encryption details. If it is not set, then no encryption will be performed. |
要了解有关 PdfSaveOptions 的更多信息,请阅读 微调转换器 一文。
使用 PdfSaveOptions 将 SVG 转换为 PDF
要使用指定的 PdfSaveOptions 将 SVG 转换为 PDF,应遵循以下几个步骤:
- 使用 SVGDocument 类的 SVGDocument() 构造函数之一加载 SVG 文件。
- 创建一个新的 PdfSaveOptions 对象并指定保存选项。
- 使用 ConvertSVG() 方法将 SVG 保存为 PDF 文件。您需要向 ConvertSVG() 方法传递 SVGDocument、PdfSaveOptions 和输出文件路径,以便将 SVG 转换为 PDF。
以下 C# 代码片段展示了如何使用自定义保存选项将 SVG 转换为 PDF:
1// Convert SVG to PDF in C# with custom page settings
2
3// Prepare a path to a source SVG file
4string documentPath = Path.Combine(DataDir, "aspose.svg");
5
6// Prepare a path for converted file saving
7string savePath = Path.Combine(OutputDir, "aspose-options.pdf");
8
9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Initialize PdfSaveOptions. Set up the page-size, margins, resolutions, JpegQuality, and change the background color to AliceBlue
13PdfSaveOptions options = new PdfSaveOptions()
14{
15 HorizontalResolution = 200,
16 VerticalResolution = 200,
17 BackgroundColor = System.Drawing.Color.AliceBlue,
18 JpegQuality = 100
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 10, 10, 10));
21
22// Convert SVG to PDF
23Converter.ConvertSVG(document, options, savePath);
PdfSaveOptions()构造函数初始化了一个PdfSaveOptions类的实例,并将其传递给ConvertSVG()方法。ConvertSVG()方法接收 “document”、“options”、输出文件路径 savePath
并执行转换操作。
在上述示例中,我们使用
BackgroundColor
属性,用于设置填充背景的颜色。默认的 BackgroundColor 是透明色。HorizontalResolution
和VerticalResolution
属性以每英寸像素为单位设置输出图像的水平/垂直分辨率。默认情况下,这些属性为 300 dpi。JpegQuality
属性,用于指定图像的 JPEG 压缩质量。PageSetup
属性指定 page size 和 margins。
PdfSaveOptions 类提供了大量属性,可让你完全控制各种参数,并改善将 SVG 转换为 PDF 格式的过程。在这些属性中,JpegQuality 使您能够指定图像的 JPEG 压缩质量。默认值为 95,但您可以设置所需的值。
图中展示了指定页面大小、背景颜色等的 aspose-options.pdf 文件。
输出流提供商
如果需要将文件保存在远程存储器(如云、数据库等)中,可以实现 ICreateStreamProvider接口来手动控制文件创建过程。该接口被设计为一个回调对象,用于在文档/页面开始时创建一个流(取决于输出格式),并在渲染文档/页面后释放早期创建的流。
下面的示例展示了如何在应用程序中实现和使用自己的MemoryStreamProvider:
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35}
1// Convert SVG to PDF in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Prepare SVG code
7string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
8 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />" +
9 "</svg>";
10
11// Convert SVG to PDF using the MemoryStreamProvider
12Converter.ConvertSVG(code, ".", new PdfSaveOptions(), streamProvider);
13
14// Get access to the memory stream that contains the result data
15MemoryStream memory = streamProvider.Streams.First();
16memory.Seek(0, SeekOrigin.Begin);
17
18// Flush the result data to the output file
19using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.pdf")))
20{
21 memory.CopyTo(fs);
22}
使用我们的在线 SVG 到 PDF 转换器 查看 SVG 转换为 PDF 的质量。上传、转换您的文件并在几秒钟内获得结果。现在就免费试用我们强大的 SVG 到 PDF 转换器!
下载 Aspose.HTML for .NET库,它能让您成功、快速、轻松地将 HTML、MHTML、EPUB、SVG 和 Markdown 文档转换为最流行的格式。
您可以从 GitHub 下载完整的示例和数据文件。