ImportFormatMode does not work at all

Dear Aspose

I’ve created a Word document using the builder method and everything works properly. Now I’m going to attach another document to it. actually, I need to load it from the disk. here is what I have done.

var newFile = File.ReadAllBytes("D:/file.docx");

using (MemoryStream stream = new MemoryStream(newFile))
{
    var docM = new Document(stream);
    builder.InsertDocument(docM, ImportFormatMode.UseDestinationStyles);
}

The loaded file from the disk contains a different style from the file created with Aspose and I need the newFile to get the Aspose word document’s style for example, font family, font size, etc.

I’ve noticed that I get exactly the same result with different options from ImportFormatMode enum. I mean KeepSourceFormatting , KeepDifferentStyles, and UseDestinationStyles have no impact on the style of newFile that is added to the Aspose document.

How could I fix that?

@mohammad19i ImportFormatMode specifies how styles are handled upon importing nodes from one document to another. If formatting in your source document is not specified via styles, then ImportFormatMode will not affect such nodes. You can clear formatting of nodes and reapply styles to make sure all formatting is specified via style. For example for paragraphs you can achieve this using the following code:

// Make sure there is no formatting applied to paragraphs explicitely.
doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().ToList()
    .ForEach(p => { Style s = p.ParagraphFormat.Style; p.ParagraphFormat.ClearFormatting(); p.ParagraphFormat.Style = s; });