Free Support Forum - aspose.com

Table of Content Style

Want to change table of content styles to show heading in black color but in other place it should be in green.

@sakhan14,

Thanks for your inquiry. Please attach 1) your input Word document, 2) Aspose.Words generated output document (DOCX file) showing the undesired behavior and 3) expected Word document which shows the correct TOC formatting here for testing. You can create expected Word document by using MS Word. We will then investigate the scenario on our end and provide you more information.

Example.zip (17.0 KB)
builder.InsertTableOfContents("\o “1-3” \h \z \u"); Using this green color.
Now problem is, i want to List of Content in black color. and other headings in the following page in green color.

We used for Figure Title.
I want to show Fig Title in bold and It is coming bold but also List of figures content also coming in bold. Code given below

builder.InsertTableOfContents("\h \z \t “FigTitleModified”\c");

public void FigureHeading(DocumentBuilder builder, string TableTitle)
{
builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.Font.Name = strFont;
builder.InsertCell();
builder.CellFormat.ClearFormatting();
builder.ParagraphFormat.StyleName = “FigTitleModified”;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(47, 40, 61);
builder.RowFormat.Height = 13;
builder.CellFormat.Width = 520;
builder.Font.Bold = true;
builder.Write(TableTitle.ToString());
builder.RowFormat.AllowAutoFit = true;
builder.EndRow();
}

Aspose.Words.Style FigTitleModified = doc.Styles.Add(Aspose.Words.StyleType.Paragraph, “FigTitleModified”);
FigTitleModified.Font.Color = Color.White;
FigTitleModified.Font.Name = strFont;
FigTitleModified.Font.Size = 10;
FigTitleModified.Font.Bold = false;
FigTitleModified.ParagraphFormat.SpaceAfter = 3;
FigTitleModified.ParagraphFormat.SpaceBefore = 3;
FigTitleModified.ParagraphFormat.LineSpacingRule = LineSpacingRule.Exactly;
FigTitleModified.ParagraphFormat.LineSpacing = 12;

@sakhan14,

The only document (ex.docx) you attached earlier was empty i.e. there is no content other than TOC fields. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your actual input word document
  • Your expected document which shows the correct output. Please note that in this document when we use “Update Field” command of MS Word, the layout/formatting of TOC entries should not be changed.
  • Aspose.Words generated output document which currently shows the undesired behavior

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.

XUZ.zip (48.3 KB)

Hi Awaiz.

I am sending only 5 pages of report with changed information.
Issue.
Page 1 : I want to show TOC in black color not in green color.
Code for Pag1
builder.InsertTableOfContents("\o “1-3” \h \z \u");

Page 3 : I want to show Font not in bold.

And on Page 7. Figure heading should be in bold. So when i making bold, list of figures of page 3 also displaying in bold. Codes given below

builder.InsertTableOfContents("\h \z \t “FigTitleModified”\c");
public void FigureHeading(DocumentBuilder builder, string TableTitle)
{
builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.Font.Name = strFont;
builder.InsertCell();
builder.CellFormat.ClearFormatting();
builder.ParagraphFormat.StyleName = “FigTitleModified”;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(47, 40, 61);
builder.RowFormat.Height = 13;
builder.CellFormat.Width = 520;
builder.Font.Bold = true;
builder.Write(TableTitle.ToString());
builder.RowFormat.AllowAutoFit = true;
builder.EndRow();
}

Aspose.Words.Style FigTitleModified = doc.Styles.Add(Aspose.Words.StyleType.Paragraph, “FigTitleModified”);
FigTitleModified.Font.Color = Color.White;
FigTitleModified.Font.Name = strFont;
FigTitleModified.Font.Size = 10;
FigTitleModified.Font.Bold = false;
FigTitleModified.ParagraphFormat.SpaceAfter = 3;
FigTitleModified.ParagraphFormat.SpaceBefore = 3;
FigTitleModified.ParagraphFormat.LineSpacingRule = LineSpacingRule.Exactly;
FigTitleModified.ParagraphFormat.LineSpacing = 12;

Please help asap.

Thanks,

@sakhan14,

Every node of TOC field is represented by a HYPERLINK field. When you click on a TOC item, the control jumps to a particular location in your document pointed by a hidden Bookmark. You can implement the following workflow to achieve what you are looking for:

Document doc = new Document("D:\\temp\\XUZ\\XUZ COmpany.docx");

foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field;
        if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
        {
            Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);
            if (tocItem != null)
            {
                foreach (Run run in tocItem.Runs)
                {
                    run.Font.Color = Color.Black;
                    run.Font.Bold = false;
                }
            }
        }
    }
}

doc.Save("D:\\Temp\\XUZ\\18.6.docx");

Aspose.words.range does not contains definition for Fields in c#
showing error for *Fields in foreach (Field field in doc.Range.Fields)

@sakhan14,

Please add the following namespace:
using Aspose.Words.Fields;

I added but still error is showing ‘Aspose.words.range’ does not contain the definition for Field accepting a first argument of type ‘Aspose.Words.range could be found’

I am using version 10.1.0.0 of Aspose.Words.Dll

Thanks,

@sakhan14,

Please upgrade to the latest version of Aspose.Words for .NET i.e. 18.6. Hope this helps.

You may also try the following code:

Document doc = new Document("D:\\temp\\XUZ\\XUZ COmpany.docx");

foreach (FieldStart field in doc.GetChildNodes(NodeType.FieldStart, true)
{
    if (field.FieldType.Equals(FieldType.FieldHyperlink))
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field.GetField();
        if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
        {
            Paragraph tocItem = (Paragraph)field.GetAncestor(NodeType.Paragraph);
            if (tocItem != null)
            {
                foreach (Run run in tocItem.Runs)
                {
                    run.Font.Color = Color.Black;
                    run.Font.Bold = false;
                }
            }
        }
    }
}

doc.Save("D:\\Temp\\XUZ\\18.6.docx");

Hi,

Now, It is showing FieldHyperlink ould not be found.
Please reply asap.

Thanks,
Sharique

@sakhan14,

You are using a very old version of Aspose.Words on your end. We suggest you please upgrade to the latest version of Aspose.Words for .NET i.e. 18.6. Hope this helps.

If i update with latest version, then i have to change whole code because latest version is very different.
Can you suggest me solution with version 10.1.0.0.

Thanks,
Sharique

@sakhan14,

Unfortunately, we do not provide support for older versions of Aspose.Words. Please note that we do not provide any fixes or patches for old versions of Aspose.Words either. All fixes and new features are always added into new versions of Aspose.Words. So, you will have to upgrade if you need new features or fixes.

Please check the following sections of documentation: