原創|其它|編輯:郝浩|2012-09-18 11:57:57.000|閱讀 848 次
概述:當我們復制一個文件的某些內容到另一個文件時,我們經常會對ImportFormat模式進行選擇。ImportFormatMode.KeepSourceFormatting將保留在源文件中使用的原始格式的,而ImportFormatMode.UseDestinationStyles會使用在目標文檔中定義的格式。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
當我們復制一個文件的某些內容到另一個文件時,我們經常會對Aspose.Words .NET 導入模式進行選擇。
ImportFormatMode.KeepSourceFormatting將保留在源文件中使用的原始格式的,而ImportFormatMode.UseDestinationStyles會使用在目標文檔中定義的格式。微軟Word也在復制內容到另一個文件時,提供了此選項選擇。
那么,Aspose.Words for .NET又是如何實現這兩種模式的選擇的呢?答案就在下面:
保留原格式的代碼如下:
C#
// Load the documents to join.
Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");
// Keep the formatting from the source document when appending it to the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
// Save the joined document to disk.
dstDoc.Save(gDataDir + "TestFile.KeepSourceFormatting Out.docx");
Visual Basic
' Load the documents to join.
Dim dstDoc As New Document(gDataDir & "TestFile.Destination.doc")
Dim srcDoc As New Document(gDataDir & "TestFile.Source.doc")
' Keep the formatting from the source document when appending it to the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting)
' Save the joined document to disk.
dstDoc.Save(gDataDir & "TestFile.KeepSourceFormatting Out.docx")
運行以上代碼后,目標文件不存在的格式也被復制過來了
而我們使用目標格式就不會有新的格式被創建,代碼如下:
C#
// Load the documents to join.
Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");
// Append the source document using the styles of the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
// Save the joined document to disk.
dstDoc.Save(gDataDir + "TestFile.UseDestinationStyles Out.doc");
Visual Basic
' Load the documents to join.
Dim dstDoc As New Document(gDataDir & "TestFile.Destination.doc")
Dim srcDoc As New Document(gDataDir & "TestFile.Source.doc")
' Append the source document using the styles of the destination document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles)
' Save the joined document to disk.
dstDoc.Save(gDataDir & "TestFile.UseDestinationStyles Out.doc")
效果圖如下:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網