Nodes Types and Shapes in MS Word Document

Hello Support,

We are using Aspose.Words in our production site. We want to know few details about Node Type and Shapes

For Shape we have the following queries:

  1. How can we specify a name for a Shape in MS Word?
  2. How can we use that name of the shape in the code to set text color, background color, fonts etc.
    We have got the following sample code far to check the shapes in a document:
Document doc = new Document(@"D:\Test\TestDoc1.doc");
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in shapes)
{
    Response.Write(shape.ShapeType.ToString()); 
}

For Nodes we have the following queries:

  1. How can we get the HTML elements like Header1, Header2, Image, Paragraph, Text box from word
    document and change color, background color, font etc.?
  2. Can we set any Id or Name for a Node? If yes how can we do it in MS Word?

Please let me know all the details.

Thanks,
Somnath

Hi Somnath,

Thanks for your inquiry. Please refer to the following article:
Aspose.Words Document Object Model

*somnathdas:

  1. How can we specify a name for a Shape in MS Word?*

Please use Shape.AlternativeText property to set the alternative text of Shape node. In MS Word, you can set it from “Format” tab. See the attached image (Alt Text.png) for detail.

somnathdas:
2. How can we use that name of the shape in the code to set text color, background color, fonts etc.

You can use Shape.AlternativeText property to set a unique name/ID to identify the it. Please read about font and paragraph formatting from following link.
Specifying Formatting

*somnathdas:

  1. How can we get the HTML elements like Header1, Header2, Image, Paragraph, Text box from word document and change color, background color, font etc.?*

Please use Node.ToString(SaveFormat.Html) to convert a node to Html. Could you please share some more detail about this query? We will then provide you more information about this.

somnathdas:
2. Can we set any Id or Name for a Node? If yes how can we do it in MS Word?

We suggest you please bookmark the Node to identify it. You can insert bookmark in the document using MS Word from “Insert tab”. See the attached image (bookmark.png) for detail.

Hello Support,

For a specific shape I am able to set Alternate Text and can loop through the shapes in code and identify them based on AlternativeText property.

For Nodes please check the below points:

  1. How can we change the color, background color, font etc. for Header1, Header2, Image, Paragraph, Text box in a word document?
    This means in a word document if we add Heading1, Heading2, Title, Paragraph, Textbox then we want to identify these elements in code and change the color, font and background color etc.
    So how can we achieve this?

Thanks,
Somnath

Hi Somnath,

Thanks for your inquiry. Please Run.Font property to get access to the font formatting of this object and use Paragraph.ParagraphFormat to get access to the paragraph formatting properties.

Could you please share your input and expected output documents here for our reference? We will then provide you more information about your query.

Hello Support,

We have prepared few screenshots where we have mentioned the things we want to do.
Also we have attached the word documents from where we took the screenshots.
Please check and let me know.

Thanks,
Somnath

Hi Somnath,

Thanks for sharing the detail. All text of the document is stored in runs of text. Please use Run.Font.Color to get/set the color of Run node. To change the cell’s background color, please use Shading.BackgroundPatternColor property. Please use Shape.Fill.Color property to fill the shape the specified color.

Please check following code examples for your kind reference. Hope this helps you.

Document doc = new Document(MyDir + "HR360+Datasheet+V1.doc");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    // Change the fill color of shape
    shape.Fill.Color = Color.Blue;
    shape.Fill.On = true;
    if (shape.HasChildNodes)
    {
        // Change the font color of text
        foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
        {
            run.Font.Color = Color.Yellow;
        }
    }
}
doc.Save(MyDir + "Out.doc");
Document doc = new Document(MyDir + "DOL+Health+Plan+Audit.docx");
// Change the background color of table's cell
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
    foreach (Row row in table.Rows)
        foreach (Cell cell in row.Cells)
        {
            cell.CellFormat.Shading.ClearFormatting();
            cell.CellFormat.Shading.BackgroundPatternColor = Color.Yellow;
        }
doc.Save(MyDir + "Out.doc");

Hello Support,

We have one question that is:

  1. How can we change the font color for all the Heading1 in a word document.
  2. How can we change the font color for all the Heading2 in a word document.

We do not want to go with shape. What we need is that we just have to change the font color to all Heading1 and Heading2.

Please let me know how this is possible in code.

Thanks,
Somnath

Hi Somnath,

Thanks for your inquiry. Please use Style.Font.Color property to set the font color of a style. Please check the following code example for your kind reference. Hope this helps you.

Document doc = new Document(MyDir + "DOL+Health+Plan+Audit.docx");
doc.Styles["font style name..."].Font.Color = Color.Yellow;
doc.Save(MyDir + "Out.doc");

Moreover, please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting:

  1. to Run nodes by using Character Styles e.g. a Glyph Style,
  2. to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles)
  3. you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

Hello Support,

We have used the following set of code:

doc.Styles["Heading 1"].Font.Color = System.Drawing.Color.DarkRed; 
doc.Styles["Heading 2"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Heading 3"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Heading 6"].Font.Color = System.Drawing.Color.DarkRed; 
doc.Styles["Table Headline 2"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Sub Header"].Font.Color = System.Drawing.Color.DarkRed;

But except “Heading 1”, “Heading 2” and “Sub Header” nothing is working. We have attached two screenshots where we have mentioned what we want. And we have also attached the respective word document.

Please check and let us know the solution.

Thanks,
Somnath

Hi Somnath,

Thanks for your inquiry. In your document, some paragraph have direct formatting. Please check my previous post about text formatting.

Please use following code example to remove the direct formatting applied to Run nodes.

The Font.ClearFormatting method resets to default font formatting. Moreover, ParagraphFormat.ClearFormatting method resets to default paragraph formatting. Default paragraph formatting is Normal style, left aligned, no indentation, no spacing, no borders and no shading.

Document doc = new Document(MyDir + "HR360+Brochure.doc");
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    String stylename = para.ParagraphFormat.StyleName;
    if (stylename == "Heading 1" || stylename == "Heading 2" || stylename == "Heading 3" || stylename == "Heading 6" ||
    stylename == "Table Headline 2" || stylename == "Sub Header")
    {
        foreach (Run run in para.Runs)
        {
            run.Font.ClearFormatting();
        }
    }
}
doc.Styles["Heading 1"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Heading 2"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Heading 3"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Heading 6"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Table Headline 2"].Font.Color = System.Drawing.Color.DarkRed;
doc.Styles["Sub Header"].Font.Color = System.Drawing.Color.DarkRed;
doc.Save(MyDir + "Output v16.12.0.docx");