Aspose.Words For .Net是一種高級Word文檔處理API,用於執行各類文檔管理和操做任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持全部流行的Word處理文件格式,並容許將Word文檔導出或轉換爲固定佈局文件格式和最經常使用的圖像/多媒體格式。佈局
【下載Aspose.Words for .NET最新試用版】ui
若是但願從任何Word文檔中提取內容表,能夠使用如下代碼示例。this
// 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文檔計算段落中的行數,能夠使用下面的代碼示例。spa
// 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類,該類容許指定各類導入選項來格式化輸出。信息。code
▲設定智能的樣式行爲orm
啓用此選項後,若是使用KeepSourceFormatting導入模式,則源樣式將擴展爲目標文檔中的直接屬性。當禁用此選項時,只有在對源樣式進行編號時纔會展開它。不會覆蓋現有的目標屬性,包括列表。教程
目前,這個選項只能與DocumentBuilder類的新公共方法一塊兒使用,以下面的示例所示:ip
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);
|
▲設置保持源編號ci
在不一樣文檔之間導入節點時,可能會出現這樣的狀況:源文檔具備與目標文檔中已經使用的標識符相同的列表。在這種狀況下,Word老是使用目標列表的格式。爲了容許用戶選擇適當的行爲,ImportFormatOptions類中引入了KeepSourceNumbering屬性,該屬性指定了當編號在源文檔和目標文檔中發生衝突時將如何導入編號。默認值爲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"
);
|
▲設置忽略文本框
當在不一樣文檔之間導入文本框時,將對其應用目標文檔的格式。這與單詞的行爲相對應。爲了容許用戶選擇適當的行爲,ImportFormatOptions類中引入了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"
);
|
更多教程資源可關注ASPOSE技術交流QQ羣(642018183)哦~歡迎交流討論!