Apply formatting to image caption numbering or table numbering

Hi, 

I am currently using the following code to set a caption on an image but I would like to apply specific formatting to the "Figure 1" text that is added to the document. Is there a way to do that? Currently, the defined format is only applied to the text added. 


var caption = image.AddCaption("Figure ", CaptionNumberingFormat.Number, CaptionPosition.AfterImage) as WParagraph;

caption.AppendText(": " + reportImage.Text).ApplyCharacterFormat(format);


I would also like to do the same for tables. I am using the following code and want to apply the formatting to the number field that is being added. 

 paragraph.AppendText("Table ").ApplyCharacterFormat(format);

 var tableNumberField = paragraph.AppendField("Table", FieldType.FieldSequence);

 paragraph.AppendText(": " + reportTable.Title).ApplyCharacterFormat(format);


I have tried using the following code to apply fomatting to the field but it does not work correctly: 

                var tableNumberField = paragraph.AppendField("Table", FieldType.FieldSequence);

                ApplyFieldFormatting(tableNumberField, format);


        private static void ApplyFieldFormatting(IEntity entity, WCharacterFormat format)

        {

            //Iterates to sibling items until Field End

            while (entity.NextSibling != null)

            {

                if (entity is WTextRange)

                {

                    //Apply character format for text ranges

                    (entity as WTextRange).ApplyCharacterFormat(format);

                }

                else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd)

                    break;

                //Gets next sibling item.

                entity = entity.NextSibling;

            }

        }


5 Replies 1 reply marked as answer

DS Dharanya Sakthivel Syncfusion Team June 4, 2024 03:12 PM UTC

Hi Natalie,

Based on the information provided, we have determined that your requirement is to apply specific formatting to the image caption and the table numbering fields. We have created a sample to meet your requirement.

The sample we have provided follows these steps:
1. Create a Word document.
2. Add sections and paragraphs in the document.
3. Add an image to the paragraph.
5. Add a caption to the image.
6. Apply the same format for the image caption.
7. Add a new table into the Word document and specify the rows and columns.
8. Add fields to the table.
9. Update the fields in the Word document.
10. Call the ApplyCharacterFormat method to the field and text range.

You can find the prepared sample in the attachment below.

Regards,
Dharanya.



Attachment: Formatimagecaptionandtablename_2fb9da44.zip

Marked as answer

NW Natalie Westphal June 24, 2024 11:21 PM UTC

After applying these changes I have been using the following code: 

var paragraph = section.AddParagraph();

var format = ReportFormatExtensions.GetCharacterFormat(serviceProvider, ReportItemType.Table1Header, document, paragraph); //Gets my own pre-defined format


paragraph.AppendText("Table ").ApplyCharacterFormat(format);

var tableNumberField = paragraph.AppendField("Table", FieldType.FieldSequence) as WField;

document.UpdateDocumentFields();

ApplyFieldFormatting(tableNumberField, format);


paragraph.AppendText(": " + reportTable.Title).ApplyCharacterFormat(format);

paragraph.ApplyStyle(BuiltinStyle.Caption);

paragraph.ParagraphFormat.KeepFollow = true;


However, calling UpdateDocumentFields every time I add a new table is severely slowing down generation of my report, as some of the reports contain 20-30 tables. Is it possible to do this another 



DS Dharanya Sakthivel Syncfusion Team June 25, 2024 11:00 AM UTC

Natalie, in our previous provided sample, we called UpdateDocumentFields() method before applying the format to the table numbering. We suspect that you misunderstood that need to call UpdateDocumentFields() method for each table.

You don't need to call the UpdateDocumentFields() method for each table. Instead, you can call the UpdateDocumentFields() method once at the end, after all your manipulations and just before saving the Word document, as shown below:


//Adds a new table into Word document

 IWTable table = section.AddTable();

 table.Title = "Details";

 //Specifies the total number of rows & columns

 table.ResetCells(3, 2);

 paragraph = section.AddParagraph();

 paragraph.AppendText("Table ").ApplyCharacterFormat(characterFormat);

 WField tableNumberField = paragraph.AppendField("Table", FieldType.FieldSequence) as WField;

 ApplyStyletoField(tableNumberField, characterFormat);

 paragraph.AppendText(": " + table.Title).ApplyCharacterFormat(characterFormat);

 

 //Updates the fields in Word document

 document.UpdateDocumentFields();

 

 //Saves and closes the document

 document.Save((Path.GetFullPath(@"../../Data/Result.docx")), FormatType.Docx);

 document.Close();


We have modified the previously shared sample and it can be downloaded from the below attachment.


The UpdateDocumentFields() API updates all the fields present in the Word document, so you only need to call it once at the end. Please refer to our UG documentation to learn more about this:
Updating fields

Please refer to our UG documentation to know more about how to add a sequence field,
Sequence field

Regards,
Dharanya.


Attachment: Formatimagecaptionandtablename_4e9b963f.zip


NW Natalie Westphal July 25, 2024 02:55 PM UTC

Hello,

I am adding a table in two parts because the information in the table is too wide for one page. So I will be adding the table as Table Name - part 1 of 2 and Table Name - part 2 of 2. Is it possible, when using the above method to add the table numbering, that both tables could have the same table number? So in the table of contents, they would both be shown as "Table 2: Table Name - Part # or 2".


Thanks. 



DS Dharanya Sakthivel Syncfusion Team July 26, 2024 10:41 AM UTC

Hi Natalie,

Based on the information provided, we found that your requirement is to add a list of tables in TOC, as both tables may have the same table number, as shown below.

Table 1: Table Name – Part 1 of 2
Table 1: Table Name – Part 2 of 2


As per Microsoft Word behavior, it is not feasible to set the same number for different tables as caption, because it uses the SEQ field to update numbering. Each table must have a unique number. Therefore, it is not feasible to achieve this in DocIO.

Regards,
Dharanya.


Loader.
Up arrow icon