[英]OpenXML and C# default font of a docx file is in Times New Roman
I have wrote some code in C# using .NET 5.0 with Nuget package openXML tools and for some reason when I merge the files together the font it merges itself in changes into Times New Roman on its own when the documents before merging were in calibri. 这是我的代码:
public static void MergeDocuments(string[] fileNames, string outputFilePath)
{
using (var outputFileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite))
{
var sources = new List<Source>();
foreach (string fileName in fileNames)
{
byte[] allBytes = File.ReadAllBytes(fileName);
var openXmlPowerToolsDocument = new OpenXmlPowerToolsDocument(allBytes);
var source = new Source(new WmlDocument(openXmlPowerToolsDocument), true);
sources.Add(source);
}
MergeXmlDocuments(outputFileStream, sources);
}
}
public static void MergeXmlDocuments(Stream outStream, List<Source> sources)
{
WmlDocument buildDocument = DocumentBuilder.BuildDocument(sources);
buildDocument.WriteByteArray(outStream);
}
static void Main(string[] args)
{
string[] files = {"cover.docx", "q3_0_0_5_0.docx", "q2.docx"};
string outFileName = "merged.docx";
List<Source> sources = null;
sources = new List<Source>() // initialize sources that would like to be merged
{
new Source(new WmlDocument("../../cover.docx"), true),
new Source(new WmlDocument("../../q3_0_0_5_0.docx"), true),
new Source(new WmlDocument("../../q2.docx"), true),
};
MergeDocuments(files, outFileName);
}
根据我的研究,OpenXml SDK 目前不支持 svg 格式的图片。
您可以从以下链接了解它:
添加 SVG 作为 ImagePartType 以支持 Office 2016
但是,我找到了另一种将 svg pciture 插入 docx 文件的方法。
首先,请安装 nuget-package-> Aspose.Words
。
其次,你可以试试下面的代码来做。
using Aspose.Words;
using DocumentBuilder = Aspose.Words.DocumentBuilder;
Document doc = new Document("D:\\1.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.Cleanup();
builder.InsertImage("D:\\1.svg");
builder.Writeln();
doc.Save("test.docx");
最后,你可以得到一个由一个docx文件和一个svg文件组合而成的docx文件。
此外,生成的 docx 文件会有关于 Aspose.Words 的广告格式。 您可以 Go 插入 > Header 或页脚,然后 select 删除 ZBF50D5E661106DAFEEABE72 或页脚删除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.