We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to create TOF

Hello,
I want to generate Table of figures in my application that merges n number of word files and then generate TOC and TOF programmatically. I am able to generate TOC but not TOF.

@dchan77

You can use DocumentBuilder.InsertTableOfContents method as shown below to insert the table of figures into document. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentStart();
builder.InsertParagraph();
builder.InsertTableOfContents("\\h \\z \\c \"Figure\"");

doc.UpdateFields();
doc.Save(MyDir + "output.docx");

Hello Tahir,

Thanks for your reply.

I have applied your code already. But the problem is – this code requires all the images to have a caption already set. In my case, I am merging n number of word documents into one and many of them may contain images which do not have captions. We have to set it programmatically. That is not being possible.

image001.png (4.39 KB)

image002.png (918 Bytes)

@dchan77

Please note that Seq field should be present in the document for images. You need to insert this field to get the correct Table of Figures. If you lost these fields while merging documents, please ZIP and attach the documents and code example to reproduce the same issue at our end. We will investigate the issue and provide you more information on it.

Can we set those Seq fields for the images programmatically?

I think if we could set a field code like “{ SEQ Figure \* ARABIC }” for each of the images in documents, then we would be able to create the TOF.

@dchan77

Yes, your understanding is correct. Please let us know if you face any issue while using Aspose.Words.

Actually, it is the problem. I am not being able to set image caption with that field code.

I used the following code to set caption to pictures, but none of them worked (2nd and 3rd try commented):

foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
    if (shape.HasImage)
    {
           currentPictureCounter++;
           ///1st try
           shape.Title = "Figure " + currentPictureCounter.ToString();
           //2nd try
           //builder.MoveTo(shape.ParentParagraph);
           //builder.Write("Figure ");
           //builder.InsertField(@"SEQ Numlist \r", "");
           ////builder.InsertField(@"SEQ Figure Numlist \r", "");
           ///
           //3rd try
           //builder.MoveTo(shape);
           //builder.InsertParagraph();
           //builder.InsertField("Figure { SEQ Figure \\* ARABIC }");
      }
}
doc.UpdateFields();
doc.Save(TempFolder + "\\ExportedReport.docx");

builder.MoveToBookmark("TOF");
builder.InsertField("TOC \\h \\z \\c \"Figure\"", null);
doc.UpdateFields();
doc.Save(TempFolder + "\\ExportedReport.docx");

Please tell me, if I am missing something.

Can anyone help me on this?

@dchan77

Following code example shows how to insert SEQ field and Table of Figures field. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentStart();
builder.InsertParagraph();
builder.InsertTableOfContents("\\h \\z \\c \"Figure\"");

builder.InsertParagraph();
builder.InsertParagraph();
builder.InsertParagraph();
builder.Write("Figure ");
builder.InsertField("SEQ Figure \\* ARABIC ");

doc.UpdateFields();
doc.Save(MyDir + "output.docx");

Sorry to say, your code doesn’t help us at all.

I have already told Mr. Tahir Manzoor that I have already used that code and we had lot more discussion about the topic.

I expected much better response from Aspose.

We are planning to purchase a license for Aspose.Words for .Net but this kind of support is really bad.

image001.png (4.39 KB)

image002.png (918 Bytes)

@dchan77

Please accept my apologies for your inconvenience.

We already shared about how to insert TOF in this thread. Please check the following line of code.

builder.InsertTableOfContents("\\h \\z \\c \"Figure\"");

If the caption of desired images is not set, the TOF field does not generate for these images.

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

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

PS: To attach these resources, please zip and upload them.

@dchan77 Your code is almost correct. If I understand your requirements correctly, you need a caption under the pictures. I modified your code a bit to achieve this:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
int currentPictureCounter = 0;
foreach (Aspose.Words.Drawing.Shape shape in shapes)
{
    if (shape.HasImage)
    {
        currentPictureCounter++;

        Run emptyRun = new Run(doc);
        shape.ParentNode.InsertAfter(emptyRun, shape);
        builder.MoveTo(emptyRun);
        builder.Writeln();
        builder.Write("Figure ");
        builder.InsertField("SEQ Figure \\* ARABIC");
    }
}
doc.UpdateFields();

builder.MoveToBookmark("TOF");
builder.InsertField("TOC \\h \\z \\c \"Figure\"", null);
doc.UpdateFields();

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

Also see the attached input and output documents.
in.docx (24.1 KB)
out.docx (22.1 KB)

This is exactly what I wanted. Thanks a lot for sticking to it and find a solution.