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


为了帮助大家在进行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,文档处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值