Free Support Forum - aspose.com

Change Font Format Name Size Color of Heading Paragraphs in Word DOCX Document using C# .NET

Hi,

I attached document called "Doc1.docx ". In that I have Main Headings, Sub Headings and Paragraph.

Now,

  1. I am want to change the Font Size : 13, font-format: Arial for paragraph only.
  • If you can see 1st page of Document. In paragraph there are bold text and there are some text in Red color also. I want keep that format as it is but want to its font-format and font-size must be change as mentioned.

  • If you can see 2nd page of document. Paragraph includes bullet point also. I don’t want to make any change in formats. Just want to change the “Font Size : 13, font-format: Arial”

  1. I want to change the font-size: 16 and color: green and font-format: Arial for all sub-Heading in document. (i.e. " Where does it come from?- SubHeading - 1", “Where does it come from?- Sub Heading 2”, " Where does it come from?- Sub Heading 2.1’,“Where does it come from?- Sub Heading 2.2”)Learning.zip (21.0 KB)

@rs43733,

We are checking these scenarios and will get back to you soon.

It would be great If you reply early for point 1. Below I mentioned point - 1.

  1. I am want to change the Font Size : 13, font-format: Arial for paragraph only .
  • If you can see 1st page of Document. In paragraph there are bold text and there are some text in Red color also. I want keep that format as it is but want to its font-format and font-size must be change as mentioned.
  • If you can see 2nd page of document. Paragraph includes bullet point also. I don’t want to make any change in formats. Just want to change the “Font Size : 13, font-format: Arial”

Learning.zip (21.0 KB)

@rs43733,

What I understand is that criteria to apply [Font Size : 13, font-format: Arial] is only the Paragraphs that are either bullet points or contain bold text or both. Please try the following code:

Document doc = new Document("C:\\Temp\\Learning\\Doc1.docx");

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    // Process Headings
    if (para.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2)
    {
        foreach (Run run in para.GetChildNodes(NodeType.Run, true))
        {
            run.Font.Name = "Arial";
            run.Font.Size = 16;
            run.Font.Color = Color.Green;
        }
    }
    else if (para.IsListItem || ContainsBold(para))
    {
        foreach (Run run in para.GetChildNodes(NodeType.Run, true))
        {
            run.Font.Name = "Arial";
            run.Font.Size = 13;
        }
    }
}

doc.Save("C:\\temp\\Learning\\20.9.docx");

public static bool ContainsBold(Paragraph para)
{
    bool flag = false;
    foreach (Run run in para.GetChildNodes(NodeType.Run, true))
    {
        if (run.Font.Bold)
        {
            flag = true;
            break;
        }
    }
    return flag;
}