Aspose.Words for .NET使用文檔教程(7):如何提取目錄和設(shè)置導(dǎo)入格式選項(xiàng)
Aspose.Words For .Net是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
【下載Aspose.Words for .NET最新試用版】
提取目錄
如果希望從任何Word文檔中提取內(nèi)容表,可以使用以下代碼示例。
// 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
計(jì)算段落的行數(shù)
如果您想為任何Word文檔計(jì)算段落中的行數(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)."); }
使用導(dǎo)入格式選項(xiàng)
Aspose.Words For .Net提供ImportFormatOptions類,該類允許指定各種導(dǎo)入選項(xiàng)來(lái)格式化輸出。
▲設(shè)定智能的樣式行為
啟用此選項(xiàng)后,如果使用KeepSourceFormatting導(dǎo)入模式,則源樣式將擴(kuò)展為目標(biāo)文檔中的直接屬性。當(dāng)禁用此選項(xiàng)時(shí),只有在對(duì)源樣式進(jìn)行編號(hào)時(shí)才會(huì)展開它。不會(huì)覆蓋現(xiàn)有的目標(biāo)屬性,包括列表。
目前,這個(gè)選項(xiàng)只能與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);
▲設(shè)置保持源編號(hào)
在不同文檔之間導(dǎo)入節(jié)點(diǎn)時(shí),可能會(huì)出現(xiàn)這樣的情況:源文檔具有與目標(biāo)文檔中已經(jīng)使用的標(biāo)識(shí)符相同的列表。在這種情況下,Word總是使用目標(biāo)列表的格式。為了允許用戶選擇適當(dāng)?shù)男袨椋?strong>ImportFormatOptions類中引入了KeepSourceNumbering屬性,該屬性指定了當(dāng)編號(hào)在源文檔和目標(biāo)文檔中發(fā)生沖突時(shí)將如何導(dǎo)入編號(hào)。默認(rèn)值為false。
為了使用這個(gè)priperty,引入了一個(gè)新的公共方法,它接受新的KeepSourceNumbering選項(xiàng),如下面的示例所示。
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è)置忽略文本框
當(dāng)在不同文檔之間導(dǎo)入文本框時(shí),將對(duì)其應(yīng)用目標(biāo)文檔的格式。這與單詞的行為相對(duì)應(yīng)。為了允許用戶選擇適當(dāng)?shù)男袨椋琁mportFormatOptions類中引入了IgnoreTextBoxes選項(xiàng)。此屬性指示在導(dǎo)入期間是否忽略源目標(biāo)文本框中的格式設(shè)置,默認(rèn)值為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萬(wàn)+用戶答謝惠倒計(jì)時(shí),ASPOSE系列產(chǎn)品火熱促銷中,最高直降8萬(wàn)元!欲購(gòu)從速喲~>>立即進(jìn)入優(yōu)惠專場(chǎng)
ASPOSE技術(shù)交流QQ群現(xiàn)已開通,各類資源及時(shí)分享,歡迎交流討論!(掃描下方二維碼加入群聊)