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


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

目标文件:

Aspose.Words在word文档合并时如何控制列表编号_第1张图片

源文件:

Aspose.Words在word文档合并时如何控制列表编号_第2张图片

在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.Words在word文档合并时如何控制列表编号_第3张图片

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

Aspose.Words在word文档合并时如何控制列表编号_第4张图片

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

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.Words在word文档合并时如何控制列表编号_第5张图片

你可能感兴趣的:(aspose)