0

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

}

Kevin Liu
  • 27
  • 3
  • based on my test, I can not reproduced your problem. I created three docx files with different fonts, such as calibri. Times New Roman. After I run the code, theses files are merged, but the font is not changed. Therefore, I suggest that you can create new three files to check it again. – Jack J Jun May 19 '21 at 02:46
  • hmm okay thank you also do you know if there is a reason why the picture will not load when I merge them? The picture I am using is a .SVG file thank you for your help though! – Kevin Liu May 19 '21 at 15:17
  • do you mean that merge svg file and docx file or merge svg files? – Jack J Jun May 20 '21 at 01:55
  • I mean like there is a .SVG picture inside the word document I am merging and it will say "Picture can't be displayed" after I merge my documents together. I've tried using jpeg, png and they all work only .SVG pictures do not work. – Kevin Liu May 20 '21 at 15:42

1 Answers1

1

Based on my research, the OpenXml SDK does not currently have support for the svg format picture.

You can know it from the following link:

Add SVG as ImagePartType for Office 2016 support

However, I find another method to insert the svg pciture to the docx file.

First, please install 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.

Besides, the generate docx file will have the advertising format about Aspose.Words. You can Go to Insert > Header or Footer, and then select Remove Header or Remove Footer to remove it.

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27