Inserting caption with heading numbering

Hi, I’m struggling with inserting a caption.

My scenario:

  1. I build word document using Aspose builder.
  2. I insert html (includes text, tables etc.)
  3. I do post processing - I try to find tables and figures inserted from html and I append a caption. The issue is that I don’t know how to add heading numbering to the caption. Any ideas? Is there any special pattern for it (instead “* ARABIC”) or I should somehow serach for the closest heading using Word node API (if yes, how to? :slight_smile: ).

Example (expected result):

Figure 1-1 (heading “1-” + figure number “1” as the first in that section)
Figure 1-2 (heading “1-” + figure number “2” as the second in that section)
Figure 2-1 (heading “2-” + figure number “1”)

My code:

    public sealed class FindCaptionsAndReplaceItWithNativeOne : IReplacingCallback
    {
        ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
        {
            Node currentNode = e.MatchNode;
            var builder = new DocumentBuilder(e.MatchNode.Document as Document);

            builder.MoveTo(currentNode);
            builder.Write($"{e.Replacement} "); // Replacement is "Figure" or "Table" (passed as an argument)

            // How to add heading numbering instead of arabic numbering?
            builder.InsertField($@"SEQ {e.Replacement} \* ARABIC", string.Empty);

            currentNode.Remove();

            return ReplaceAction.Skip;
        }
    }

Manual operation in Word:
Word editor menu -> References -> Insert Caption -> Numbering… -> Include chapter number with hyphen separator -> OK -> OK

@krykar

Could you please ZIP and attach your input Word document that you created using Aspose.Words?

Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

I attached an example, which was created manually. I cannot attach created report, because it contains some company data, but I can give some overview:

builder.ParagraphFormat.ClearFormatting();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln(text);
builder.ParagraphFormat.ClearFormatting();

builder.WriteHtmlContent(html);

// more paragraphs etc.

Html is created using a web control, so it contains html tables, figures, text etc (one big html). At the end of building the report I replace caption html convention by native word captions (user’s html -> html processing (placeholders) -> Aspose replace). I use this function:

private void ReplaceCaptions(DocumentBuilder builder)
{
    var findReplaceOptions = new FindReplaceOptions
    {
        ReplacingCallback = new FindCaptionsAndReplaceItWithNativeOne(),
        PreserveMetaCharacters = true
    };

    builder.Document.Range.Replace("__CAPTION_TABLE__", "Table", findReplaceOptions);
    builder.Document.Range.Replace("__CAPTION_FIGURE__", "Figure", findReplaceOptions);
}

Captions with chapter heading.zip (25.9 KB)

@krykar

Please use the following modified code to get the desired output. Moreover, please call the Document.UpdateFields method before saving the document.

public sealed class FindCaptionsAndReplaceItWithNativeOne : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Node currentNode = e.MatchNode;
        var builder = new DocumentBuilder(e.MatchNode.Document as Document);

        builder.MoveTo(currentNode);
        builder.Write($"{e.Replacement} "); // Replacement is "Figure" or "Table" (passed as an argument)

        // How to add heading numbering instead of arabic numbering?
        builder.InsertField($@"STYLEREF \s 1", string.Empty);
        builder.Write("-");
        builder.InsertField($@"SEQ {e.Replacement} \* ARABIC \s 1", string.Empty);

        currentNode.Remove();

        return ReplaceAction.Skip;
    }
}

Thank you very much, it has solved my problem :grinning:

I’ve spotted an issue with updating the fields. In my report I have also a list of tables / figures which comes from a template (I read existing word document with header, footer, prepared list of content etc. and then I add a content using the Aspose library). Before saving the document I call UpdateFields, but the result it like that:

image.png (840 Bytes)

My workaround is to call UpdateFields() twice, then I have got a proper result:

image.png (830 Bytes)

I guess that variables could be sorted by the Aspose library on updating (UpdateFields()) to avoid this side effect.

@krykar

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please create a simple console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.