将图元文件转换为 Svg

在本教程中,我们将引导您使用 Aspose.Words for .NET 将元文件转换为 SVG 格式的 C# 源代码。此功能允许您在将文档转换为 HTML 时将元文件转换为 SVG 格式。

步骤 1:项目设置

首先,在您最喜欢的 IDE 中创建一个新的 C# 项目。确保您的项目中引用了 Aspose.Words for .NET 库。

步骤 2:将 SVG 图像插入文档

在此步骤中,我们将在要转换的文档中插入 SVG 图像。使用以下代码使用 HTML 标记插入 SVG 图像:

string dataDir = "YOUR DOCUMENT DIRECTORY";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Here is an SVG image: ");
builder.InsertHtml(
	@"<svg height='210' width='500'>
	<polygon points='100,10 40,198 190,78 10,78 160,198' 
		style='fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;' />
</svg> ");

此代码创建DocumentDocumentBuilder构建文档。它插入一个<svg>标签包含<polygon>具有属性的元素来定义 SVG 图像的形状和样式。

步骤 3:设置 HTML 保存选项

现在我们将设置 HTML 保存选项,指定应将元文件转换为 SVG 格式。使用以下代码:

HtmlSaveOptions saveOptions = new HtmlSaveOptions { MetafileFormat = HtmlMetafileFormat.Svg };

此代码创建HtmlSaveOptions并设置MetafileFormatHtmlMetafileFormat.Svg指定在转换为 HTML 时应将元文件转换为 SVG 格式。

步骤 4:将文档转换并保存为 HTML

最后,我们将使用之前定义的 HTML 保存选项将文档转换为 HTML。使用以下代码:

doc.Save(dataDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToSvg.html", saveOptions);

此代码将文档转换为 HTML,并将其保存为将元文件转换为 SVG 的文件。

使用 Aspose.Words for .NET 将图元文件转换为 Svg 的示例源代码


	//文档目录的路径。
	string dataDir = "YOUR DOCUMENT DIRECTORY";
	Document doc = new Document();
	DocumentBuilder builder = new DocumentBuilder(doc);
	
	builder.Write("Here is an SVG image: ");
	builder.InsertHtml(
		@"<svg height='210' width='500'>
		<polygon points='100,10 40,198 190,78 10,78 160,198' 
			style='fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;' />
	</svg> ");

	HtmlSaveOptions saveOptions = new HtmlSaveOptions { MetafileFormat = HtmlMetafileFormat.Svg };

	doc.Save(dataDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToSvg.html", saveOptions);