Caption on image is being repeated

Hello,

I am using some code directly copied over from the GitHub repo in order to include a caption on an image in my document. The caption is being repeated on the image as shown in the attached document. Here is a copy of my code. Can you please verify and provide a solution? 

Thanks. 


            var document = new WordDocument();

            IWSection section = document.AddSection();

            //Sets margin of the section.

            section.PageSetup.Margins.All = 72;

            //Adds a paragraph to the section.

            IWParagraph paragraph = section.AddParagraph();

            paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

            //Adds image to the paragraph.

            IWPicture picture = paragraph.AppendPicture(tImage);

            //Adds Image caption.

            IWParagraph lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);

            //Aligns the caption.

            lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

            //Sets after spacing.

            lastParagragh.ParagraphFormat.AfterSpacing = 12f;

            //Sets before spacing.

            lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;

            //Adds a paragraph to the section.

            paragraph = section.AddParagraph();

            paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

            //Adds image to the paragraph.

            picture = paragraph.AppendPicture(tImage);

            //Adds Image caption.

            lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);

            //Aligns the caption.

            lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

            //Sets before spacing.

            lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;


Attachment: ImageCaptionRepeat_e500ca82.zip

3 Replies

SB Sneha Biju Syncfusion Team January 10, 2024 09:53 AM UTC

Hi Natalie,

Based on the provided code snippet, we have found that your requirement is to add a caption to the image using Roman numerals. In order to achieve this, you need to update the document fields before saving the document, as the caption numbers are placed inside a SEQ field that needs to be updated to display the correct result.

You can refer to the following documentation for adding a caption
https://help.syncfusion.com/file-formats/docio/working-with-paragraph#add-image-caption

You can find the modified code snippet below:

WordDocument document = new WordDocument();

IWSection section = document.AddSection();

//Sets margin of the section.

section.PageSetup.Margins.All = 72;

//Adds a paragraph to the section.

IWParagraph paragraph = section.AddParagraph();

paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

//Adds image to the paragraph.

IWPicture picture = paragraph.AppendPicture(tImage);

//Adds Image caption.

IWParagraph lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);

//Aligns the caption.

lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

//Sets after spacing.

lastParagragh.ParagraphFormat.AfterSpacing = 12f;

//Sets before spacing.

lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;

//Adds a paragraph to the section.

paragraph = section.AddParagraph();

paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

//Adds image to the paragraph.

picture = paragraph.AppendPicture(tImage);

//Adds Image caption.

lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);

//Aligns the caption.

lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

//Sets before spacing.

lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;

//Updates the fields in Word document.

document.UpdateDocumentFields();


Regards,
Sneha.



NW Natalie Westphal January 10, 2024 05:05 PM UTC

Excellent. Is it possible to add a caption that only includes the given text and does not use the CaptionNumberingFormat or hides the CaptionNumberingFormat? 



SB Sneha Biju Syncfusion Team January 11, 2024 08:35 AM UTC

Natalie, we have found that your requirement is to display only the text, excluding the number format, as the caption for the image. In the Microsoft Word application itself, we cannot set only the text as the caption; instead, we can only set the number format. Please find the screenshot.


Therefore, in the Syncfusion DocIO library, it is also not possible to set only the text label as the caption.

As a workaround, we suggest manually adding a text to the next paragraph as a caption for the image. You can find the code snippet for the above suggestion.

WordDocument document = new WordDocument();

IWSection section = document.AddSection();

//Sets margin of the section.

section.PageSetup.Margins.All = 72;

//Adds a paragraph to the section.

IWParagraph paragraph = section.AddParagraph();

paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

//Adds image to the paragraph.

IWPicture picture = paragraph.AppendPicture(tImage);

//Adds Image caption.

IWParagraph lastParagragh = section.AddParagraph();

//Add the caption text

lastParagragh.AppendText("Figure");

//Aligns the caption.

lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

//Sets after spacing.

lastParagragh.ParagraphFormat.AfterSpacing = 12f;

//Sets before spacing.

lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;



Loader.
Up arrow icon