Image Caption Upper and Lower

Hello,

We would like to find an example of how to program the image caption for both upper and lower in VB.NET.

We searched the forum, and other areas for some examples but couldn’t find a solution.
Thank you in advance.

Raymond, I am sorry but I don’t understand the request. What do you mean by image caption? Can you provide an example?

Hi Raymond,

Here is an example of adding a caption with C#:

static void Main()
{
    var document = new DocumentModel();
    var picture = new Picture(document, "image.png");

    document.Sections.Add(
        new Section(document,
            new Paragraph(document, picture)));

    AddCaption(picture, "My caption.", "Figure", true);

    // PaginatorOptions.UpdateFields will result in updating all SEQ fields.
    document.GetPaginator(new PaginatorOptions() { UpdateFields = true });

    document.Save("output.docx");
}

static void AddCaption(Picture picture, string caption, string figure, bool isBelow)
{
    var document = picture.Document;
    var pictureParagraph = picture.Parent as Paragraph;
    var captionParagraph = pictureParagraph.Clone(false);

    pictureParagraph.ParagraphFormat.KeepWithNext = true;

    // Get or create a caption paragraph's style.
    ParagraphStyle captionStyle = document.Styles.FirstOrDefault(s => s.Name == "Caption") as ParagraphStyle;
    if (captionStyle == null)
    {
        captionStyle = new ParagraphStyle("Caption")
        {
            CharacterFormat = { Italic = true, Size = 9 },
            ParagraphFormat = { LineSpacing = 1, LineSpacingRule = LineSpacingRule.Multiple }
        };
        document.Styles.Add(captionStyle);
    }
    captionParagraph.ParagraphFormat.Style = captionStyle;

    // Add caption paragraph's content.
    if (!string.IsNullOrEmpty(figure))
    {
        captionParagraph.Inlines.Add(new Run(document, figure + " "));
        captionParagraph.Inlines.Add(new Field(document, FieldType.Seq, figure + " \\* ARABIC "));
    }
    captionParagraph.Inlines.Add(new Run(document, " " + caption));

    // Insert caption paragraph.
    int index = pictureParagraph.ParentCollection.IndexOf(pictureParagraph);
    if (isBelow)
        index++;
    pictureParagraph.ParentCollection.Insert(index, captionParagraph);
}

And here is with VB.NET:

Sub Main()
    Dim document = New DocumentModel()
    Dim picture = New Picture(document, "image.png")

    document.Sections.Add(
        New Section(document,
            New Paragraph(document, picture)))

    AddCaption(picture, "My caption.", "Figure", True)

    ' PaginatorOptions.UpdateFields will result in updating all SEQ fields.
    document.GetPaginator(New PaginatorOptions() With {.UpdateFields = True})

    document.Save("output.docx")
End Sub

Sub AddCaption(picture As Picture, caption As String, figure As String, isBelow As Boolean)
    Dim document = picture.Document
    Dim pictureParagraph = TryCast(picture.Parent, Paragraph)
    Dim captionParagraph = pictureParagraph.Clone(False)

    pictureParagraph.ParagraphFormat.KeepWithNext = True

    ' Get or create a caption paragraph's style.
    Dim captionStyle As ParagraphStyle = TryCast(document.Styles.FirstOrDefault(Function(s) s.Name = "Caption"), ParagraphStyle)
    If captionStyle Is Nothing Then
        captionStyle = New ParagraphStyle("Caption")
        captionStyle.CharacterFormat.Italic = True
        captionStyle.CharacterFormat.Size = 9
        captionStyle.ParagraphFormat.LineSpacing = 1
        captionStyle.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple
        document.Styles.Add(captionStyle)
    End If
    captionParagraph.ParagraphFormat.Style = captionStyle

    ' Add caption paragraph's content.
    If Not String.IsNullOrEmpty(figure) Then
        captionParagraph.Inlines.Add(New Run(document, figure & " "))
        captionParagraph.Inlines.Add(New Field(document, FieldType.Seq, figure & " \* ARABIC "))
    End If
    captionParagraph.Inlines.Add(New Run(document, " " & caption))

    ' Insert caption paragraph.
    Dim index = pictureParagraph.ParentCollection.IndexOf(pictureParagraph)
    If isBelow Then index += 1
    pictureParagraph.ParentCollection.Insert(index, captionParagraph)
End Sub

Regards,
Mario