翻譯|使用教程|編輯:李顯亮|2019-06-26 10:44:06.227|閱讀 1674 次
概述:Aspose.Words For .Net是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words For .Net是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現(xiàn)和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
【下載Aspose.Words for .NET最新試用版】
如果希望從任何Word文檔中提取內容表,可以使用以下代碼示例。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); string fileName = "TOC.doc"; Aspose.Words.Document doc = new Aspose.Words.Document(dataDir + fileName); foreach (Field field in doc.Range.Fields) { if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink)) { FieldHyperlink hyperlink = (FieldHyperlink)field; if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc")) { Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph); Console.WriteLine(tocItem.ToString(SaveFormat.Text).Trim()); Console.WriteLine("------------------"); if (tocItem != null) { Bookmark bm = doc.Range.Bookmarks[hyperlink.SubAddress]; // Get the location this TOC Item is pointing to Paragraph pointer = (Paragraph)bm.BookmarkStart.GetAncestor(NodeType.Paragraph); Console.WriteLine(pointer.ToString(SaveFormat.Text)); } } // End If }// End If }// End Foreach
如果您想為任何Word文檔計算段落中的行數(shù),可以使用下面的代碼示例。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); string fileName = "Properties.doc"; Document document = new Document(dataDir + fileName); var collector = new LayoutCollector(document); var it = new LayoutEnumerator(document); foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true)) { var paraBreak = collector.GetEntity(paragraph); object stop = null; var prevItem = paragraph.PreviousSibling; if (prevItem != null) { var prevBreak = collector.GetEntity(prevItem); if (prevItem is Paragraph) { it.Current = collector.GetEntity(prevItem); // para break it.MoveParent(); // last line stop = it.Current; } else if (prevItem is Table) { var table = (Table)prevItem; it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break it.MoveParent(); // cell it.MoveParent(); // row stop = it.Current; } else { throw new Exception(); } } it.Current = paraBreak; it.MoveParent(); // We move from line to line in a paragraph. // When paragraph spans multiple pages the we will follow across them. var count = 1; while (it.Current != stop) { if (!it.MovePreviousLogical()) break; count++; } const int MAX_CHARS = 16; var paraText = paragraph.GetText(); if (paraText.Length > MAX_CHARS) paraText = $"{paraText.Substring(0, MAX_CHARS)}..."; Console.WriteLine($"Paragraph '{paraText}' has {count} line(-s)."); }
Aspose.Words For .Net提供ImportFormatOptions類,該類允許指定各種導入選項來格式化輸出。
啟用此選項后,如果使用KeepSourceFormatting導入模式,則源樣式將擴展為目標文檔中的直接屬性。當禁用此選項時,只有在對源樣式進行編號時才會展開它。不會覆蓋現(xiàn)有的目標屬性,包括列表。
目前,這個選項只能與DocumentBuilder類的新公共方法一起使用,如下面的示例所示:
Document srcDoc = new Document(dataDir + "source.docx"); Document dstDoc = new Document(dataDir + "destination.docx"); DocumentBuilder builder = new DocumentBuilder(dstDoc); builder.MoveToDocumentEnd(); builder.InsertBreak(BreakType.PageBreak); ImportFormatOptions options = new ImportFormatOptions(); options.SmartStyleBehavior = true; builder.InsertDocument(srcDoc, ImportFormatMode.UseDestinationStyles, options);
在不同文檔之間導入節(jié)點時,可能會出現(xiàn)這樣的情況:源文檔具有與目標文檔中已經(jīng)使用的標識符相同的列表。在這種情況下,Word總是使用目標列表的格式。為了允許用戶選擇適當?shù)男袨椋?strong>ImportFormatOptions類中引入了KeepSourceNumbering屬性,該屬性指定了當編號在源文檔和目標文檔中發(fā)生沖突時將如何導入編號。默認值為false。
為了使用這個priperty,引入了一個新的公共方法,它接受新的KeepSourceNumbering選項,如下面的示例所示。
Document srcDoc = new Document(dataDir + "source.docx"); Document dstDoc = new Document(dataDir + "destination.docx"); ImportFormatOptions importFormatOptions = new ImportFormatOptions(); // Keep source list formatting when importing numbered paragraphs. importFormatOptions.KeepSourceNumbering = true; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions); ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs; foreach (Paragraph srcPara in srcParas) { Node importedNode = importer.ImportNode(srcPara, false); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.Save(dataDir + "output.docx");
當在不同文檔之間導入文本框時,將對其應用目標文檔的格式。這與單詞的行為相對應。為了允許用戶選擇適當?shù)男袨椋琁mportFormatOptions類中引入了IgnoreTextBoxes選項。此屬性指示在導入期間是否忽略源目標文本框中的格式設置,默認值為true。
Document srcDoc = new Document(dataDir + "source.docx"); Document dstDoc = new Document(dataDir + "destination.docx"); ImportFormatOptions importFormatOptions = new ImportFormatOptions(); // Keep the source text boxes formatting when importing. importFormatOptions.IgnoreTextBoxes = false; NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions); ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs; foreach (Paragraph srcPara in srcParas) { Node importedNode = importer.ImportNode(srcPara, true); dstDoc.FirstSection.Body.AppendChild(importedNode); } dstDoc.Save(dataDir + "output.docx");
慧都20萬+用戶答謝惠倒計時,ASPOSE系列產品火熱促銷中,最高直降8萬元!欲購從速喲~>>立即進入優(yōu)惠專場
ASPOSE技術交流QQ群現(xiàn)已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn