Aspose word keepSourceNumbering

Hi All,

I try to copy part of docx to another docx with " dstDoc.FirstSection.Body.AppendChild(importedNode);", and i want to keep the same source formating and numbering, for this i do this code:

ImportFormatOptions importFormatOptions = new ImportFormatOptions();
importFormatOptions.KeepSourceNumbering = keepSourceNumbering;

NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions);

the keep source formatting is working good, but not the keepSourceNumbering.
I have this on my source doc:
3 Annexe 3

and this on my destination doc:
1 Annexe 3

Best regards,
Jugurtha

@Jugurtha_MAHDAD Please, ZIP and attach the source code and source document here we will check the issue and provide you more information.

Source.zip (91.7 KB)

source code:

public List<(String, bool)> AsposeDocToHtml(string articleId, string file, int sectionNumber, string categorie, string tabSection)
{
    tabSection = tabSection.Replace("[", "").Replace("]", "");
    List<int> table = tabSection.Split(',').Select(int.Parse).ToList();
    categorie = categorie.Replace("'", "''");
    byte[] data = GetWordContent(articleId, file, categorie);
    Stream stream = new MemoryStream(data);
    string html = "";
    //string url = correctFiles.LinkUrl;
    Aspose.Words.Document doc = new Aspose.Words.Document(stream);

    DocumentBuilder builder = new DocumentBuilder(doc);
    var j = 0;
    var i = 0;
    var k = 0;
    bool revision = false;
    StyleIdentifier style = StyleIdentifier.Heading1;
    ArrayList listOfParagraphs = new ArrayList();

    foreach (Field field in doc.Range.Fields)
    {

        // Field field = doc.Range.Fields[sectionNumber];
        if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
        {
            FieldHyperlink hyperlink = (FieldHyperlink)field;
            if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
            {


                //if (tocItem != null && tocItem.Range.Replace(ControlChar.Tab, ControlChar.Tab) > 1)
                //{
                Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);
                Bookmark bm = doc.Range.Bookmarks[hyperlink.SubAddress];
                // Get the location this TOC Item is pointing to
                Paragraph pointer = null;
                if (bm != null)
                {
                    pointer = (Paragraph)bm.BookmarkEnd.GetAncestor(NodeType.Paragraph);
                    if (k == 0)
                    {
                        style = pointer.ParagraphFormat.StyleIdentifier;
                        k++;
                    }
                    if (pointer.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 || pointer.ParagraphFormat.StyleIdentifier == style)
                    {


                        listOfParagraphs.Add(pointer);
                    }
                    //listOfParagraphs.Add(pointer);
                    if (j == sectionNumber)
                    {
                        i = listOfParagraphs.Count - 1;
                    }
                }

                //}
            }
        }
        j++;
    }


    Paragraph startPara = (Paragraph)listOfParagraphs[i];
    Paragraph endPara = null;

    if (i + 1 == listOfParagraphs.Count)
        endPara = doc.LastSection.Body.LastParagraph;
    else
        endPara = (Paragraph)listOfParagraphs[i + 1];

    ArrayList extractedNodes = ExtractContent(startPara, endPara, true).Item1;
    revision = ExtractContent(startPara, endPara, true).Item2;
    // Insert the content into a new separate document and save it to disk.
    Document dstDoc = GenerateDocument(doc, extractedNodes);

    /*foreach(Node n in extractedNodes)
    {
        if (HasRevisions(n))
        {
            revision = HasRevisions(n);
        }

    }*/

    dstDoc.LastSection.Body.LastParagraph.Remove();

    html = ConvertDocumentToHtml(dstDoc);
    List<(String, bool)> liste = new List<(String, bool)>();
    if (table != null)
    {
        int a = doc.Sections.Count;
        //doc.Sections[sortTable(table, sectionNumber)])

    }
    liste.Add((html, revision));
    return liste;
}

public static Document GenerateDocument(Document srcDoc, ArrayList nodes)
{
    // Create a blank document.
    Document dstDoc = new Document();
    // Remove the first paragraph from the empty document.
    dstDoc.FirstSection.Body.RemoveAllChildren();

    // Import each node from the list into the new document. Keep the original formatting of the node.
    ImportFormatOptions importFormatOptions = new ImportFormatOptions { KeepSourceNumbering = true };

    NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions);
    //Paragraph para = new Paragraph("1 title 1");

    /*DocumentBuilder builder = new DocumentBuilder(dstDoc);
    builder.MoveTo(dstDoc.FirstSection.Body.FirstParagraph);
    List lst = dstDoc.FirstSection.Body.FirstParagraph.ListFormat.List;
    Paragraph par = new Paragraph(dstDoc);
    par.AppendChild(new Run(dstDoc, "1 This is new list item."));
    par.ListFormat.List = lst;
    dstDoc.FirstSection.Body.AppendChild(par);*/

    //dstDoc.FirstSection.Body.AppendParagraph(para.ToString());//.ListFormat.List.ListLevels[0].StartAt=1;
    foreach (Node node in nodes)
    {
        Node importNode = importer.ImportNode(node, true);
        dstDoc.FirstSection.Body.AppendChild(importNode);
    }
    dstDoc.UpdateListLabels();
    // Return the generated document.
    return dstDoc;
}

@Jugurtha_MAHDAD KeepSourceNumbering = true as follows from the summary, uses source numbering in case of clashes with destination numbering. In your case, there are no clashes, so KeepSourceNumbering has no effect on the output. KeepSourceNumbering does not synchronize start value for level numbering. You can do it manually.

foreach (Node node in nodes)
{
    Node importNode = importer.ImportNode(node, true);
    Paragraph para = (Paragraph)importNode;
    para.ListFormat.List.ListLevels[0].StartAt = 2;
    dstDoc.FirstSection.Body.AppendChild(importNode);
}