彩票走势图

控件教程:Aspose.Words在word文档合并时如何控制列表编号

原创|其它|编辑:郝浩|2012-09-24 12:01:00.000|阅读 2904 次

概述:为了帮助大家在进行word文档合并时灵活地控制列表编号,Aspose.Words for .NET为大家提供了ImportFormatMode属性设置来进行相应的操作。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

为了帮助大家在进行word文档合并时灵活地控制列表编号,Aspose.Words for .NET为大家提供了ImportFormatMode属性设置来进行相应的操作。在本文中,我们会给出两个合并前的文件(目标文件和源文件),每个文件都包含一个列表,每个列表都使用了名为 MyStyle的超链接样式,具体如下图:

目标文件:

文档合并,Aspose,Aspose.Words,文档处理

源文件:

文档合并,Aspose,Aspose.Words,文档处理

在MyStyle的超链接样式下,用ImportFormatMode.KeepSourceFormatting 保留源文件格式的列表编号的代码如下:

C#

 Document dstDoc = new Document(gDataDir + "TestFile.DestinationList.doc");
Document srcDoc =  new Document(gDataDir + "TestFile.SourceList.doc");

// Append the content of the document so it flows continuously.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
dstDoc.Save(gDataDir + "TestFile.ListKeepSourceFormatting Out.doc");

Visual Basic

 Dim dstDoc As New Document(gDataDir & "TestFile.DestinationList.doc")
Dim srcDoc As New Document(gDataDir & "TestFile.SourceList.doc")

' Append the content of the document so it flows continuously.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting)
dstDoc.Save(gDataDir & "TestFile.ListKeepSourceFormatting Out.doc")

文件合并后效果图如下:

文档合并,Aspose,Aspose.Words,文档处理

但是,如果我们在MyStyle的超链接样式下,使用ImportFormatMode.UseDestinationStyles 沿用目标文件格式的列表编号,会出现列表自动连续编号,如下图:

文档合并,Aspose,Aspose.Words,文档处理

如果我们既想要沿用目标文件格式的列表编号,同时又想要防止列表的连续编号的话,我们需要运行以下代码来解决以上问题:

C#

 Document dstDoc = new Document(gDataDir + "TestFile.DestinationList.doc");
Document srcDoc =  new Document(gDataDir + "TestFile.SourceList.doc");

// Set the source document to continue straight after the end of the destination document.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

// Keep track of the lists that are created.
Hashtable newLists = new Hashtable();

// Iterate through all paragraphs in the document.
foreach (Paragraph para in srcDoc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.IsListItem)
    {
        int listId = para.ListFormat.List.ListId;

        // Check if the destination document contains a list with this ID already. If it does then this may
        // cause the two lists to run together. Create a copy of the list in the source document instead.
        if (dstDoc.Lists.GetListByListId(listId) != null)
        {
            List currentList;
            // A newly copied list already exists for this ID, retrieve the stored list and use it on
            // the current paragraph.
            if (newLists.Contains(listId))
            {
                currentList = (List)newLists[listId];
            }
            else
            {
                // Add a copy of this list to the document and store it for later reference.
                currentList = srcDoc.Lists.AddCopy(para.ListFormat.List);
                newLists.Add(listId, currentList);
            }

            // Set the list of this paragraph  to the copied list.
            para.ListFormat.List = currentList;
        }
    }
}

// Append the source document to end of the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);

// Save the combined document to disk.
dstDoc.Save(gDataDir + "TestFile.ListUseDestinationStyles Out.docx");

Visual Basic

 Dim dstDoc As New Document(gDataDir & "TestFile.DestinationList.doc")
Dim srcDoc As New Document(gDataDir & "TestFile.SourceList.doc")

' Set the source document to continue straight after the end of the destination document.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous

' Keep track of the lists that are created.
Dim newLists As New Hashtable()

' Iterate through all paragraphs in the document.
For Each para As Paragraph In srcDoc.GetChildNodes(NodeType.Paragraph, True)
    If para.IsListItem Then
        Dim listId As Integer = para.ListFormat.List.ListId

        ' Check if the destination document contains a list with this ID already. If it does then this may
        ' cause the two lists to run together. Create a copy of the list in the source document instead.
        If dstDoc.Lists.GetListByListId(listId) IsNot Nothing Then
            Dim currentList As List
            ' A newly copied list already exists for this ID, retrieve the stored list and use it on
            ' the current paragraph.
            If newLists.Contains(listId) Then
                currentList = CType(newLists(listId), List)
            Else
                ' Add a copy of this list to the document and store it for later reference.
                currentList = srcDoc.Lists.AddCopy(para.ListFormat.List)
                newLists.Add(listId, currentList)
            End If

            ' Set the list of this paragraph  to the copied list.
            para.ListFormat.List = currentList
        End If
    End If
Next para

' Append the source document to end of the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles)

' Save the combined document to disk.
dstDoc.Save(gDataDir & "TestFile.ListUseDestinationStyles Out.docx")

 代码运行后效果图如下:

文档合并,Aspose,Aspose.Words,文档处理


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP