简体   繁体   English

OpenXML 和 C# docx 文件的默认字体为 Times New Roman

[英]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. 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. Here is my code:这是我的代码:

 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);

} }

Based on my research, the OpenXml SDK does not currently have support for the svg format picture.根据我的研究,OpenXml SDK 目前不支持 svg 格式的图片。

You can know it from the following link:您可以从以下链接了解它:

Add SVG as ImagePartType for Office 2016 support添加 SVG 作为 ImagePartType 以支持 Office 2016

However, I find another method to insert the svg pciture to the docx file.但是,我找到了另一种将 svg pciture 插入 docx 文件的方法。

First , please install nuget-package-> Aspose.Words .首先,请安装 nuget-package-> Aspose.Words

Second , you can try the following code to do it.其次,你可以试试下面的代码来做。

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");

Finally , you can get a docx file combined by a docx file and a svg file.最后,你可以得到一个由一个docx文件和一个svg文件组合而成的docx文件。

Besides , the generate docx file will have the advertising format about Aspose.Words.此外,生成的 docx 文件会有关于 Aspose.Words 的广告格式。 You can Go to Insert > Header or Footer, and then select Remove Header or Remove Footer to remove it.您可以 Go 插入 > Header 或页脚,然后 select 删除 ZBF50D5E661106DAFEEABE72 或页脚删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM