Dynamic Caption Labels of Figures

Hi Team,
I am using Aspose.Word.dll 10.3.0.0 license for generating word document.
We are dynamically adding image, chart in the word document. But we are unable to add caption for these graphics (image, chart).Please find the attached document for your reference. Like the attached document we have to add caption in generate document.
Therefore, can you please provide any solution or code that we can use to generate caption in word document.
we are using below code but it is not generating the require output:

builder.InsertField(“SEQ Section \* ARABIC \s”);
Or
builder.InsertField(“SEQ Section \* ARABIC \s”, “Testtext”);

Require Output:-

Section 1.10
---------------
---------------

Section 1.10.1 : Testtext

Section 1.10.2 : Testtext
---------------
---------------

Section 1.10.3 : Testtext
---------------

Please take it as a high priority and provide the solution as soon as possible.

Thanks in advance.

Thanks,
Samanvay

Hi Samanvay,

Thanks for your request.

Once you get the Shape/DrawingML node that is containing your picture, you can insert caption either above or below the picture. I think the following code snippet will be helpful to you:

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

DrawingML image = doc.GetChildNodes(NodeType.DrawingML, true)[0] as DrawingML;
if (image.HasImage)
{
    builder.MoveTo(image.ParentParagraph);
    builder.Writeln();
    builder.Write("Figure:");
    builder.InsertField(@"SEQ Section * ARABIC", "");
}

doc.Save(@"C:\test\imageCaption_out.docx");

Moreover, please note that Aspose.Words does not evaluate SEQ fields. So you should update fields inside the document manually (Alt+ F9).

Please let us know if you need more information, We are always glad to help you.

Best Regards,

Hi,

A little correction to my previous reply, the latest version of Aspose.Words i.e. 10.7.0 does support evaluation of SEQ fields e.g:

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

DrawingML image = doc.GetChildNodes(NodeType.DrawingML, true)[0] as DrawingML;
if (image.HasImage)
{
    builder.MoveTo(image.ParentParagraph);
    builder.Writeln();
    builder.Write("Figure: ");
    builder.InsertField(@"SEQ Section \* ARABIC", "");
}

doc.UpdateFields();

doc.Save(@"C:\test\imageCaption_out.docx");

Moreover, for a list of fields supported during update, please visit the following link:
https://docs.aspose.com/words/java/update-field/

Best Regards,

Hi,

Thnaks for reply.

we are able to generate the Caption as “Figure: 1”, “Figure: 2”, “Figure: 3”.

But we want to generate the numbering according to the section number for example :-
For section 1.10 it should be as “Figure: 1.10.1”, “Figure: 1.10.2”, “Figure: 1.10.3” and
For section 2.10 it should be as “Figure: 2.10.1”, “Figure: 2.10.2”, “Figure: 2.10.3”.

Please provied the solution for this.

Thanks & Regards,
Samanvay

Hi,

Thanks for your inquiry. I think you have to define caption numbering template for every section. For example, for all images in section 1.10, the caption numbering template would be as follows:

builder.Write("Figure: 1.10.");
builder.InsertField(@"SEQ Section1.10 \* ARABIC", "");

For section 2.10 the template would be as follows:

builder.Write("Figure: 2.10.");
builder.InsertField(@"SEQ Section2.10 \* ARABIC", "");

and for section N.10 the template would be as follows:

builder.Write("Figure: N.10.");
builder.InsertField(@"SEQ SectionN.10 \* ARABIC", "");

Please let us know if you need more information, we are always glad to help you.

Best Regards,

Hi,

We have to generate the section number dynamically. on the basis of section number. and we have to restart the figure number for diffrent section

ex:-

section 1.8

figure 1.8.1
figure 1.8.2
figure 1.8.3

section 1.9

figure 1.9.1

figure 1.9.2

figure 1.9.3

also we have to generate the TOC for figure number.

Thanks,
Samanvay

Hi,

Thanks for your inquiry. I think, in this case, you have to generate your entire document section by section dynamically by using DocumentBuilder class:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/

Moreover, could you please create your target document manually by using MS WORD and attach it here? I will visualize it and then will try to solve your task.

Secondly, you can create a Table of Figures by using the following code snippet:

Document doc = new Document(@"C:\test\Doc1.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection dMLs = doc.GetChildNodes(NodeType.DrawingML, true);
foreach (DrawingML image in dMLs)
{
    if (image.HasImage)
    {
        builder.MoveTo(image.ParentParagraph);
        builder.Write("Figure: ");
        builder.InsertField(@"SEQ Section \* ARABIC", "");
    }
}
builder.MoveToDocumentStart();
builder.InsertTableOfContents("\\h \\z \\c \"Section\"");
doc.UpdateFields();
doc.Save(@"C:\test\out.docx");

Doc1.docx files is attached.

I hope, this will help.

Best Regards,

if image can moveto image.parentParagraph,but I want insert caption labels of table above,how to do ?

builder.MoveTo(image.ParentParagraph);
builder.Writeln();
builder.Write("Figure: ");
builder.InsertField(@"SEQ Section \* ARABIC", "");

Hi,

Thanks for your inquiry. I have answered your questions here in this post. Please follow that thread for further proceedings.

Best Regards,