【问题标题】:Copy bullet list number in new document?在新文档中复制项目符号列表编号?
【发布时间】:2015-12-20 16:01:20
【问题描述】:

我有一个这样的节点导入器

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。

【问题讨论】:

    标签: .net aspose aspose.words


    【解决方案1】:

    以下代码示例将列表项从源文档导入新的空文档并保留列表标签(编号)值。

    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 forum 中报告问题,并提供输入和预期输出文档。

    我与 Aspose 合作,担任开发人员传道者。

    【讨论】:

    • 如果我愿意复制新文档中的每个节点怎么办。在这种情况下,每个文档的索引都将从 1 开始。我需要跟踪列表索引。
    • 请注意,Aspose.Words 模仿与 MS Word 相同的行为。如果您从 Word 文档中复制一个列表项并将其粘贴到新的空文档中,您将获得相同的输出。列表项从 1 开始。要解决此问题,您可以在我的答案中使用更新的代码示例。希望这对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2021-08-06
    • 2020-12-08
    相关资源
    最近更新 更多