将 SVG 转换为 DOCX – C# 示例

DOCX 格式之所以流行,是因为它为用户编写任何类型的文档提供了多种选择。这种文件格式是使用最广泛的文件格式之一,可通过许多程序使用。使用 Aspose.HTML for .NET,您可以通过编程将 SVG 转换为 DOCX 格式。本文将介绍如何使用转换器类的 ConvertSVG() 方法将 SVG 转换为 DOCX,以及如何应用 DocSaveOptions 方法。此外,您还可以尝试使用在线 SVG 转换器来测试 Aspose.HTML for .NET API 的功能并即时转换 SVG。

在线 SVG 转换器

您可以使用 Aspose.HTML API 将 SVG 实时转换为其他格式。请从本地文件系统加载 SVG,选择输出格式并运行示例。保存选项为默认设置。您将立即以单独文件的形式收到转换结果。

                
            

如果您想通过编程将 SVG 转换为 DOCX 文件,请参阅以下 C# 代码示例。

只需一行代码即可将 SVG 转换为 DOCX

Converter 类的静态方法主要用作将 SVG 文件转换为各种格式的最简单方法。只需一行代码,您就可以在 C# 应用程序中将 SVG 转换为 DOCX!

在下面的示例中,我们将本地文件系统中的 SVG 文件( shapes.svg)转换并保存到本地文件系统中。

1// Convert SVG to DOCX using C#
2
3// Invoke the ConvertSVG() method to convert SVG to DOCX
4Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new DocSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.docx"));

将 SVG 转换为 DOCX

使用 ConvertSVG() 方法将文件转换为另一种格式是一系列操作,其中包括文件加载和保存。在下面的示例中,我们通过代码创建一个 SVG 文件。

  1. 为 SVG 文档编写代码
  2. 创建一个新的 DocSaveOptions 对象。
  3. 使用 Converter 类的 ConvertSVG(content, baseUri, options, outputPath) 方法将 SVG 保存为 DOCX 文件。

请看下面的 C# 代码片段,它显示了使用 Aspose.HTML for .NET 将 SVG 转换为 DOCX 的过程。

 1// Convert SVG to DOCX in 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='10' />" +
 6              "</svg>";
 7
 8// Prepare a path for converted file saving
 9string savePath = Path.Combine(OutputDir, "circle.docx");
10
11// Initialize DocSaveOptions
12DocSaveOptions options = new DocSaveOptions();
13
14// Convert SVG to DOCX
15Converter.ConvertSVG(code, ".", options, savePath);

您可以从 GitHub 下载完整的示例和数据文件。

使用 DocSaveOptions 将 SVG 转换为 DOCX

要使用指定的 DocSaveOptions 将 SVG 转换为 DOCX,应遵循以下几个步骤:

  1. 使用 SVGDocument 类的 SVGDocument() 构造函数之一加载 SVG 文件。
  2. 创建一个新的 DocSaveOptions 对象并指定保存选项。
  3. 使用 ConvertSVG() 方法将 SVG 保存为 DOCX 文件。您需要向 ConvertSVG() 方法传递 SVGDocument、DocSaveOptions 和输出文件路径,以便将 SVG 转换为 DOCX。

以下 C# 代码片段展示了如何使用自定义保存选项将 SVG 转换为 DOCX:

 1// Convert SVG to DOCX in C# with custom page settings
 2
 3// Prepare a path to a source SVG file
 4string documentPath = Path.Combine(DataDir, "shapes.svg");
 5
 6// Prepare a path for converted file saving
 7string savePath = Path.Combine(OutputDir, "shapes-options.docx");
 8
 9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Initialize DocSaveOptions. Set up the page-size and margins
13DocSaveOptions options = new DocSaveOptions();
14options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 10, 10, 10));
15
16// Convert SVG to DOCX
17Converter.ConvertSVG(document, options, savePath);

DocSaveOptions() 构造函数初始化 DocSaveOptions 类的实例,并将其传递给 ConvertSVG() 方法。ConvertSVG() 方法接收 documentoptions、输出文件路径 savePath 并执行转换操作。在示例中,我们使用 PageSetup 属性指定 DOCX 文档的页面大小。要了解有关 DocSaveOptions 的更多信息,请阅读 微调转换器 一文。

使用我们的在线 SVG 转换器 查看 SVG 转换质量。上传、转换文件,几秒钟内就能得到结果。现在就免费试用我们强大的 SVG 转换器吧!

文本 “SVG 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.