在新文档中复制项目符号列表编号?

5
我有一个节点导入程序,代码如下:
Dim nodeImporter As New Aspose.Words.NodeImporter(_wordDocument, documentComponentDocument,
 Aspose.Words.ImportFormatMode.UseDestinationStyles)

我正在使用它将子节点从一个文档复制到另一个文档。我的子节点是一个项目符号列表。
documentComponentSection.Body.AppendChild(nodeImporter.ImportNode(childNode, True))

但我的问题是,子节点的某些属性(如ListLabel,即项目符号列表编号)没有被复制。

根据您的答案,我尝试了以下方法。但是当我为每个节点创建新文档时,它不起作用。

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");
            }

每个输出文档均包含列表索引1。
1个回答

2
以下代码示例将源文档中的列表项导入到新的空文档中,并保留列表标签(编号)值。
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");
    }
}

如果问题仍存在,请在Aspose.Words论坛中报告问题,提供输入和预期输出的文档。
我作为开发者大使与Aspose合作。

如果我想要复制新文档中的每个节点,那么每个文档的索引都将从1开始。我需要跟踪列表索引。 - Manjay_TBAG
请注意,Aspose.Words模仿了MS Word的行为。如果您从Word文档中复制一个列表项并将其粘贴到新的空文档中,则会获得相同的输出。列表项从1开始。为解决此问题,您可以使用我回答中的更新代码示例。希望这可以帮助您。 - Tahir Manzoor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接