Override the default styles for document

Hi there,
I am new to the Aspose word and I would like to know if there is a way to override the default styles of the document.
I try the following approach (I don’t need the builder as all I want to achieve here is merging multi docs with source format)
Thanks in advance.

Document outputDoc = new Document();
Document docTitle = new Document(MyDir + "documentTitle.md");
//Override heading 1 style 
Style docTitleStyle = docTitle.Styles[StyleIdentifier.Heading1];

docTitleStyle.Font.Name = "Times New Roman";
docTitleStyle.Font.Bold = true;
docTitleStyle.Font.Size = 16;
docTitleStyle.AutomaticallyUpdate = true;
Document docModifyHistory = new Document(MyDir +  "modificationHistory.md");
Style docMHh2Style = docTitle.Styles[StyleIdentifier.Heading2];

docMHh2Style.Font.Name = "Times New Roman";
docMHh2Style.Font.Bold = true;
docMHh2Style.Font.Size = 16;
docMHh2Style.AutomaticallyUpdate = true;

Style docMHtableStyle = docTitle.Styles[StyleIdentifier.TableNormal];

docMHtableStyle.Font.Name = "Times New Roman";
docMHtableStyle.Font.Bold = true;
docMHtableStyle.Font.Size = 12;
docMHtableStyle.AutomaticallyUpdate = true;
docMHtableStyle.Font.Border.LineStyle = LineStyle.None;

outputDoc.AppendDocument(docTitle, ImportFormatMode.KeepSourceFormatting);
outputDoc.AppendDocument(docModifyHistory, ImportFormatMode.KeepSourceFormatting);
outputDoc.Save(MyDir + "output.docx");

in the above code snippet, I can see the title updated with the override style. But for the second one, I can still see the table border.

It would be nice if we can override the default style in a simpler way. and where can we find the default style list in document.

Kind regards

@frankxie You can define the styles in a separate document and then copy the styles from the template into your destination document using Document.CopyStylesFromTemplate method.

Hi Alex,
thanks for your quick reply.
you mentioned we can use a separate document to record the styles then copy styles Document.CopyStylesFromTemplate from the template document.

However, we will need the 2 documents to define and generate the output file with desired format.
In my case, I will have to merge 10+ markdown file into one particular output file and each document will need import its own style.
In total, we may end up declare 20+ documents before we save the output file.

Is there a way that we can override the existing default style via update document object itself?
Thanks

@frankxie Unfortunately, your requirements are not clear enough. Markdown document does not have style definition, so when you open markdown document using Aspose.Words in the created document the default styles will be used, i.e. the same styles will be used in all opened markdown documents.

If possible, could you please attach your input, output and expected output documents? This will help us to better understand your requirements.

no problem, the following attachment is the example we want to achieve.
md1 to 4 docx files are the md file save as MS word (so that I can upload)
you will see the font and style requirement as md file’s content
and the output file is the single file that merge 4 md files with different formats.

thanks

md1.docx (12.7 KB)

md2.docx (12.8 KB)

md3.docx (12.8 KB)

md4.docx (13.0 KB)

multi md output.docx (19.9 KB)

Also when use the document.Append method is there a way to append the document directly from the last paragraph instead of start a new page.
and Following is the merged output:
mergedOutput.docx (24.5 KB)

Document outputDoc = new Document();
Document docTitle = new Document(MyDir + "mds/" + "md1.md");
//Override heading 1 style 
Style md1Title = docTitle.Styles[StyleIdentifier.Title];

md1Title.Font.Name = "Calibri Light";
md1Title.Font.Bold = true;
md1Title.Font.Size = 28;
md1Title.AutomaticallyUpdate = true;
Document md2 = new Document(MyDir + "mds/" + "md2.md");
Style md2Normal = docTitle.Styles[StyleIdentifier.Normal];

md2Normal.Font.Name = "Times New Roman";
md2Normal.Font.Bold = false;
md2Normal.Font.Size = 12;
md2Normal.AutomaticallyUpdate = true;

Document md3 = new Document(MyDir + "mds/" + "md3.md");

Style md3H2 = md3.Styles[StyleIdentifier.Heading2];

md3H2.Font.Name = "Arial";
md3H2.Font.Bold = false;
md3H2.Font.Size = 18;
md3H2.Font.Color = Color.Blue;
md3H2.AutomaticallyUpdate = true;

Document md4 = new Document(MyDir + "mds/" + "md4.md");

Style md4h2 = md4.Styles[StyleIdentifier.Heading2];

md4h2.Font.Name = "Calibri";
md4h2.Font.Bold = false;
md4h2.Font.Size = 18;
md4h2.Font.Color = Color.Yellow;
md4h2.AutomaticallyUpdate = true;
Style md4table = md4.Styles[StyleIdentifier.TableClassic1];

md4table.Font.Name = "Times New Roman";
md4table.Font.Bold = false;
md4table.Font.Size = 12;
md4table.Font.Border.LineStyle = LineStyle.None;
md4table.AutomaticallyUpdate = true;

outputDoc.AppendDocument(docTitle, ImportFormatMode.KeepSourceFormatting);
outputDoc.AppendDocument(md2, ImportFormatMode.KeepSourceFormatting);
outputDoc.AppendDocument(md3, ImportFormatMode.KeepSourceFormatting);
outputDoc.AppendDocument(md4, ImportFormatMode.KeepSourceFormatting);

outputDoc.Save(MyDir + "mds/" + "mergedOutput.docx");

@frankxie Thank you for additional information.

  1. In md1 file you are using **bold text** syntax, this text is not imported as a paragraph with Title style, so change you make in the Title style through the code.

  2. In the md4 file there is a table. By default table does not have style applied so changes made in the TableClassic1 style does not affect the table formatting.

  3. When you append documents whole sections are copied from source document into the destination document. By default section has section start as new page. You can set section start as continuous.

Document md1 = new Document(@"C:\Temp\md1.md");
Document md2 = new Document(@"C:\Temp\md2.md");
Document md3 = new Document(@"C:\Temp\md3.md");
Document md4 = new Document(@"C:\Temp\md4.md");

// Chnage section start.
md1.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
md2.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
md3.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
md4.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

// Change formatting of bold text in the md1 file.
md1.GetChildNodes(NodeType.Run, true).Cast<Run>().Where(r => r.Font.Bold).ToList()
    .ForEach(r => { r.Font.Name = "Calibri Light";  r.Font.Size = 28; });

// Chnage style of the Normal style in md2 file.
md2.Styles[StyleIdentifier.Normal].Font.Name = "Times New Roman";
md2.Styles[StyleIdentifier.Normal].Font.Bold = false;
md2.Styles[StyleIdentifier.Normal].Font.Size = 12;

// Change Heading 2 style in md3
md3.Styles[StyleIdentifier.Heading2].Font.Name = "Arial";
md3.Styles[StyleIdentifier.Heading2].Font.Bold = false;
md3.Styles[StyleIdentifier.Heading2].Font.Size = 18;
md3.Styles[StyleIdentifier.Heading2].Font.Color = Color.Blue;

// Change Heading 2 style in md4
md4.Styles[StyleIdentifier.Heading2].Font.Name = "Calibri";
md4.Styles[StyleIdentifier.Heading2].Font.Bold = false;
md4.Styles[StyleIdentifier.Heading2].Font.Size = 18;
md4.Styles[StyleIdentifier.Heading2].Font.Color = Color.Yellow;

// Change formatting of the tables in md4
foreach (Table t in md4.GetChildNodes(NodeType.Table, true))
{
    t.GetChildNodes(NodeType.Run, true).Cast<Run>().ToList()
        .ForEach(r => { r.Font.Name = "Times New Roman"; r.Font.Size = 12; });

    t.SetBorders(LineStyle.None, 0, Color.Empty);
}

// Merge documents.
Document result = Merger.Merge(new Document[] { md1, md2, md3, md4 }, MergeFormatMode.KeepSourceFormatting);

result.Save(@"C:\Temp\out.docx");

input.zip (730 Bytes)
out.docx (7.2 KB)

1 Like

Thanks Alex @alexey.noskov , the code snippet does solve the merge document with multi style issue.
However, when I try to apply the header and footer update merge documents. only first page has the header/footer. ( I am using ASPOSE-CSHARP-/CSharp/Programming-Documents/Document/CreateHeaderFooterUsingDocBuilder.cs at 0c1256b7ad13f977c886462d05264a31ecbf565d · makhan7/ASPOSE-CSHARP- · GitHub as example.
result as following:

I would like to know how to apply the header/footer on every page to the merged document

thanks

@frankxie Actually, in your case the problem partially occurs because you are using Aspose.Words in evaluation mode. Aspose.Words in evaluation mode injects an evaluation watermark into the document header, so each section in the resulting document has it’s own header/footer and does not inherit headers/footers from the first section.
If you would like to test Aspose.Words without evaluation version limitations, you can request a free 30-days temporary license .

You can also explicitly link headers/footer to previous section using code like the following:

// Merge documents.
Document result = Merger.Merge(new Document[] { md1, md2, md3, md4 }, MergeFormatMode.KeepSourceFormatting);
            
// Add header.
DocumentBuilder builder = new DocumentBuilder(result);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("This is my cool header");

// Link header/footer of the following section to the first section.
foreach (Section s in result.Sections)
{
    if (s == result.FirstSection)
        continue;

    s.HeadersFooters.LinkToPrevious(true);
}

result.Save(@"C:\Temp\out.docx");

@alexey.noskov thanks Alex, does that mean each section will represent a document (from md file) here. And each section has the header, footer attribute can be defined. however, one page may contain multiple sections.

how can we define the header/footer per page instead of per section?

thanks

@frankxie MS Word documents are flow by their nature, so there is no “page” concept. Consumer applications reflows document content into pages on the fly. In MS Word document headers/footers are defined per section, to have a different headers/footers on each page, each page must be a separate section.

1 Like