原創|其它|編輯:郝浩|2012-09-24 12:01:00.000|閱讀 2904 次
概述:為了幫助大家在進行word文檔合并時靈活地控制列表編號,Aspose.Words for .NET為大家提供了ImportFormatMode屬性設置來進行相應的操作。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
為了幫助大家在進行word文檔合并時靈活地控制列表編號,Aspose.Words for .NET為大家提供了ImportFormatMode屬性設置來進行相應的操作。在本文中,我們會給出兩個合并前的文件(目標文件和源文件),每個文件都包含一個列表,每個列表都使用了名為 MyStyle的超鏈接樣式,具體如下圖:
目標文件:
源文件:
在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")
文件合并后效果圖如下:
但是,如果我們在MyStyle的超鏈接樣式下,使用ImportFormatMode.UseDestinationStyles 沿用目標文件格式的列表編號,會出現列表自動連續編號,如下圖:
如果我們既想要沿用目標文件格式的列表編號,同時又想要防止列表的連續編號的話,我們需要運行以下代碼來解決以上問題:
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")
代碼運行后效果圖如下:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網