【发布时间】:2021-05-18 15:41:20
【问题描述】:
我在 C# 中使用 .NET 5.0 和 Nuget 包 openXML 工具编写了一些代码,由于某种原因,当我将文件合并在一起时,当合并之前的文档在时,它会自行合并为 Times New Roman口径。这是我的代码:
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);
}
【问题讨论】:
-
根据我的测试,我无法重现您的问题。我创建了三个具有不同字体的 docx 文件,例如 calibri。英语字体格式一种。运行代码后,这些文件被合并,但字体没有改变。所以,我建议你可以新建三个文件再检查一遍。
-
嗯,好的,谢谢你,你知道我合并它们时图片无法加载的原因吗?我使用的图片是 .SVG 文件,不过感谢您的帮助!
-
你的意思是合并svg文件和docx文件还是合并svg文件?
-
我的意思是我正在合并的 word 文档中有一个 .SVG 图片,在我将文档合并在一起后会显示“图片无法显示”。我尝试过使用 jpeg、png,但它们都只能使用 .SVG 图片不起作用。
标签: c# .net visual-studio openxml-powertools