5

I have a node importer which goes like this

Dim nodeImporter As New Aspose.Words.NodeImporter(_wordDocument, documentComponentDocument,
 Aspose.Words.ImportFormatMode.UseDestinationStyles)

I am using it to copy childnode from one document to other. My child node is a bullet list.

documentComponentSection.Body.AppendChild(nodeImporter.ImportNode(childNode, True))

But my problem is that some properties of childnode like ListLabel i.e. bullet list numbering is not getting copied

As per your answer I tried following. But it is not working when I am creating new document for each node.

Aspose.Words.Document srcDoc = new Aspose.Words.Document(Mydir + "input.docx");

            Aspose.Words.Document dstDoc = new Aspose.Words.Document();
            var ctr = 0;
            int listid = 0;
            Aspose.Words.Lists.List dstList = null;
            foreach (Aspose.Words.Paragraph paragraph in srcDoc.GetChildNodes(Aspose.Words.NodeType.Paragraph, true))
            {
                Aspose.Words.NodeImporter imp = new Aspose.Words.NodeImporter(srcDoc, dstDoc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);
                Aspose.Words.Node impNode = imp.ImportNode(paragraph, true);
                if (((Aspose.Words.Paragraph)impNode).IsListItem)
                {
                    ((Aspose.Words.Paragraph)impNode).ListFormat.ListLevel.StartAt = paragraph.ListFormat.List.ListId;
                    if (listid != paragraph.ListFormat.List.ListId)
                    {
                        listid = paragraph.ListFormat.List.ListId;
                        dstList = dstDoc.Lists.AddCopy(paragraph.ListFormat.List);
                    }


                    ((Aspose.Words.Paragraph)impNode).ListFormat.List = dstList;
                }
                dstDoc.FirstSection.Body.RemoveAllChildren();
                dstDoc.FirstSection.Body.AppendChild(impNode);
                var index = ctr++;
                dstDoc.Save(MyDir + index.ToString() + ".docx");
            }

Each output doc contains list index as 1.

Manjay_TBAG
  • 2,176
  • 3
  • 23
  • 43

1 Answers1

2

Following code example imports the list item from source document into new empty document and keep the list label (numbered) value.

Aspose.Words.Document srcDoc = new Aspose.Words.Document(MyDir  + "input.docx");
DocumentBuilder builder = new DocumentBuilder(srcDoc);
srcDoc.UpdateListLabels();

Aspose.Words.Document dstDoc = new Aspose.Words.Document();
int ctr = 0;
Aspose.Words.NodeImporter imp = new Aspose.Words.NodeImporter(srcDoc, dstDoc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);

foreach (Aspose.Words.Paragraph paragraph in srcDoc.GetChildNodes(Aspose.Words.NodeType.Paragraph, true))
{
    if (paragraph.IsListItem)
    {
        ListLabel label = paragraph.ListLabel;
        builder.MoveTo(paragraph);
        builder.StartBookmark("bookmark_" + label.LabelValue);
        builder.EndBookmark("bookmark_" + label.LabelValue);

        Aspose.Words.Node impNode = imp.ImportNode(paragraph, true);

        dstDoc.FirstSection.Body.RemoveAllChildren();
        dstDoc.FirstSection.Body.AppendChild(impNode);

        foreach (Bookmark bookmark in ((Aspose.Words.Paragraph)impNode).Range.Bookmarks)
        {
            if (!bookmark.Name.StartsWith("bookmark_"))
                continue;

            String listLabel = bookmark.Name.Replace("bookmark_", "");

            try
            {
                ((Aspose.Words.Paragraph)impNode).ListFormat.ListLevel.StartAt = Convert.ToInt32(listLabel);
                break;
            }
            catch (Exception ex)
            {
            }
        }

        ctr++;
        dstDoc.Range.Bookmarks.Clear();
        dstDoc.Save(MyDir + ctr.ToString() + ".docx");
    }
}

If the problem still remains, please report the issue in Aspose.Words forum with input and expected output documents.

I work with Aspose as Developer evangelist.

Tahir Manzoor
  • 597
  • 2
  • 9
  • What if I am willing to copy every node in new document. In that case every document's index will start from 1. I need to keep track of list index. – Manjay_TBAG Sep 24 '15 at 07:20
  • Note that Aspose.Words mimics the same behavior as MS Word does. If you copy one list item from Word document and paste it in new empty document, you will get the same output. The list item starts from 1. To workaround this issue, you can use the updated code example in my answer. Hope this helps you. – Tahir Manzoor Sep 28 '15 at 09:40