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

问题描述

我使用 .NET 5.0 和 Nuget 包 openXML 工具在 C# 中编写了一些代码,并且出于某种原因,当我将文件合并在一起时,当合并前的文档位于口径。这是我的代码

 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"),new Source(new WmlDocument("../../q2.docx"),};

    MergeDocuments(files,outFileName);

}

解决方法

根据我的研究,OpenXml SDK 目前不支持 svg 格式的图片。

您可以通过以下链接了解:

Add SVG as ImagePartType for Office 2016 support

但是,我找到了另一种将 svg 图片插入 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的广告格式。您可以转到“插入”>“页眉”或“页脚”,然后选择“删除页眉”或“删除页脚”将其删除。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...