DocX
DocX copied to clipboard
Inserting Captions to an Image
Is there a way to insert captions to image just like MS word allows you to insert caption when right clicking an image?
@XceedBoucherS @xceedsoftware After doing research for about 2 days I found a way myself to insert Caption in Word exactly like "Insert Caption" option when we right click an image or a table.
Actually in MS Word the figure or table numbers are Document Fields. Yes that might sound a bit new but these field are there and can be viewed by right clicking any inserted caption in the document and clicking Toggle Field Codes. One can say these are actually formulas that represent different meaning. For instance the {SEQ} means initializing a sequence that starts from 1.
Ok that was enough on what actually is caption. In short we need to insert a "Document Field" with a formula to insert caption to images.
Actually this can be implemented in Xceed Opensource library by using the Simple Fields. Simple Fields are a exact replica of Document fields. And the Xceed examples uses simple for the implementation of adding page number to the document in its HeaderFooter example.
I have implemented my function that is similar to the AppendPageNumber(PageNumberFormat pnf) function.
You can use this for the implementation of captions in the
Paragraph.cs
public Paragraph InsertNumberedSequence()
{
XElement fldSimple = new XElement( XName.Get( "fldSimple", Document.w.NamespaceName ) );
fldSimple.Add( new XAttribute( XName.Get( "instr", Document.w.NamespaceName ), @"SEQ " ) );
XElement content = XElement.Parse
(
@"<w:r w:rsidR='001D0226' xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>1</w:t>
</w:r>"
);
fldSimple.Add( content );
Xml.Add( fldSimple );
return this;
}
And for adding caption one can use
document.InsertParagraph($"captionLabel ").InsertNumberedSequence().Append(" my caption text");
I would request to please add this in the Xceed framework.
Thank you for your feedback, we will look into this and give you some news on this.
Hi, This will be added in v1.8. Thank you.
Hi, Thanks for the response sir. @XceedBoucherS Sir can you help me in one thing. I am able to add numbered caption to the image. It is working fine. But I cannot handle the case when the image is almost near the end of the page and the caption when I add goes to the next page. So in short I want to add the caption but if its going on the next page the image should also move to the next page.
Hi,
I was able to do it successfully. :-)
When inserting the image I enabled the .keepWithNextParagraph property to true. Then I inserted the caption. Now they always come next to each other.
Yes, that is the solution. Good guess :-)