簡體   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. 這是我的代碼:

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM