Aspose.Words for .NET是一種高級Word文檔處理API,用於執行各類文檔管理和操做任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。經過集成API,開發人員能夠執行的一些基本任務,例如設計具備標準郵件合併字段的全功能Microsoft Word報告,幾種經常使用格式之間的可靠轉換,頁面的高保真渲染,全部文檔元素的格式化等等。api
Aspose.Words for .NET更新至v19.7,爲Markdown格式實現基本的讀寫器,同時實現了檢測SmartArt形狀的功能!接下來咱們給你們介紹一下新版中引入的公告API的更改,並用示例實際闡述一下。>>下載Aspose.Words for .NET最新試用版app
在Revision類中添加了如下新屬性:ide
////// Gets the revision group. Returns null if the revision does not belong to any group.
///////// Revision has no group if revision type is RevisionType.StyleDefinitionChange or
/// if the revision is not longer exist in document context (accepted/rejected).
///public RevisionGroup Group
|
使用案例:字體
Document doc =
new
Document(@
"source.docx"
);
foreach (Revision revision
in
doc.Revisions)
{
string groupText = revision.Group !=
null
?
"Revision group text: "
+ revision.Group.Text
:
"Revision has no group"
;
Console.WriteLine(
"Type: "
+ revision.RevisionType);
Console.WriteLine(
"Author: "
+ revision.Author);
Console.WriteLine(
"Date: "
+ revision.DateTime);
Console.WriteLine(
"Revision text: "
+ revision.ParentNode.ToString(SaveFormat.Text));
Console.WriteLine(groupText);
}
|
暫時支持如下Markdown功能:ui
增長了新的公開枚舉:this
LoadFormat.Markdown
SaveFormat.Markdown
FileFormat.Markdown
|
添加了新的TxtSaveOptionsBase類:spa
////// The base class for specifying additional options when saving a document into a text based formats.
///public abstract class TxtSaveOptionsBase : SaveOptions
|
一些成員從TxtSaveOptions類移動到TxtSaveOptionsBase類:設計
////// Specifies the encoding to use when exporting in text formats.
/// Default value isEncoding.UTF8'UTF-8' Charset.
///public Encoding Encoding
////// Specifies the string to use as a paragraph break when exporting in text formats.
/////////The default value is.///public string ParagraphBreak
////// Specifies whether the program should attempt to preserve layout of tables when saving in the plain text format.
/// The default value is false.
///public bool PreserveTableLayout
//////Allows to specify whether the page breaks should be preserved during export.///The default value is false.///////// The property affects only page breaks that are inserted explicitly into a document.
/// It is not related to page breaks that MS Word automatically inserts at the end of each page.
///public bool ForcePageBreaks
////// Specifies the way headers and footers are exported to the text formats.
/// Default value is.
///public TxtExportHeadersFootersMode ExportHeadersFootersMode
|
功能的實現主要遵循CommonMark規範。在AW模型中,Markdown功能表示爲相應的樣式或直接格式。所以,粗體和斜體表示爲Font.Bold和Font.Italic。標題是標題1 - 標題6樣式的段落。引號是樣式名稱中帶有「引用」的段落。HorizontalRule是具備HorizontalRule形狀的段落。code
使用案例1:如何生成如下Markdown文檔的重點:orm
Markdown treats asterisks (*) and underscores (_) as indicators of emphasis. You can write **bold** or *italic* text. You can also write ***BoldItalic***text.
DocumentBuilder builder =
new
DocumentBuilder(
new
Document());
builder.Writeln(
"Markdown treats asterisks (*) and underscores (_) as indicators of emphasis."
);
builder.Write(
"You can write "
);
builder.Font.Bold =
true
;
builder.Write(
"bold"
);
builder.Font.Bold =
false
;
builder.Write(
" or "
);
builder.Font.Italic =
true
;
builder.Write(
"italic"
);
builder.Font.Italic =
false
;
builder.Writeln(
" text. "
);
builder.Write(
"You can also write "
);
builder.Font.Bold =
true
;
builder.Font.Italic =
true
;
builder.Write(
"BoldItalic"
);
builder.Font.Bold =
false
;
builder.Font.Italic =
false
;
builder.Write(
"text."
);
builder.Document.Save(
"EmphasesExample.md"
);
|
使用案例2:如何使用標題生成如下Markdown文檔:
The following produces headings: # Heading1 ## Heading2 ### Heading3 #### Heading4 ##### Heading5 ###### Heading6 # **Bold Heading1**
Document doc =
new
Document();
DocumentBuilder builder =
new
DocumentBuilder(doc);
// By default Heading styles in Word may have bold and italic formatting.
// If we do not want text to be emphasized, set these properties explicitly to false.
builder.Font.Bold =
false
;
builder.Font.Italic =
false
;
builder.Writeln(
"The following produces headings:"
);
builder.ParagraphFormat.Style = doc.Styles[
"Heading 1"
];
builder.Writeln(
"Heading1"
);
builder.ParagraphFormat.Style = doc.Styles[
"Heading 2"
];
builder.Writeln(
"Heading2"
);
builder.ParagraphFormat.Style = doc.Styles[
"Heading 3"
];
builder.Writeln(
"Heading3"
);
builder.ParagraphFormat.Style = doc.Styles[
"Heading 4"
];
builder.Writeln(
"Heading4"
);
builder.ParagraphFormat.Style = doc.Styles[
"Heading 5"
];
builder.Writeln(
"Heading5"
);
builder.ParagraphFormat.Style = doc.Styles[
"Heading 6"
];
builder.Writeln(
"Heading6"
);
// Note, emphases are also allowed inside Headings:
builder.Font.Bold =
true
;
builder.ParagraphFormat.Style = doc.Styles[
"Heading 1"
];
builder.Writeln(
"Bold Heading1"
);
doc.Save(
"HeadingsExample.md"
);
|
使用案例3:如何使用塊引號生成如下Markdown文檔:
We support blockquotes in Markdown: >*Lorem* >*ipsum* The quotes can be of any level and can be nested: >>>Quote level 3 >>>>Nested quote level 4 > >*Back to first level* > ### Headings are allowed inside Quotes
Document doc =
new
Document();
DocumentBuilder builder =
new
DocumentBuilder(doc);
builder.Writeln(
"We support blockquotes in Markdown:"
);
builder.ParagraphFormat.Style = doc.Styles[
"Quote"
];
builder.Writeln(
"Lorem"
);
builder.Writeln(
"ipsum"
);
builder.ParagraphFormat.Style = doc.Styles[
"Normal"
];
builder.Writeln(
"The quotes can be of any level and can be nested:"
);
Style quoteLevel3 = doc.Styles.Add(StyleType.Paragraph,
"Quote2"
);
builder.ParagraphFormat.Style = quoteLevel3;
builder.Writeln(
"Quote level 3"
);
Style quoteLevel4 = doc.Styles.Add(StyleType.Paragraph,
"Quote3"
);
builder.ParagraphFormat.Style = quoteLevel4;
builder.Writeln(
"Nested quote level 4"
);
builder.ParagraphFormat.Style = doc.Styles[
"Quote"
];
builder.Writeln();
builder.Writeln(
"Back to first level"
);
Style quoteLevel1WithHeading = doc.Styles.Add(StyleType.Paragraph,
"Quote Heading 3"
);
builder.ParagraphFormat.Style = quoteLevel1WithHeading;
builder.Write(
"Headings are allowed inside Quotes"
);
doc.Save(
"QuotesExample.md"
);
|
使用案例4:如何使用水平規則生成如下Markdown文檔:
We support Horizontal rules (Thematic breaks) in Markdown: -----
DocumentBuilder builder =
new
DocumentBuilder(
new
Document());
builder.Writeln(
"We support Horizontal rules (Thematic breaks) in Markdown:"
);
builder.InsertHorizontalRule();
builder.Document.Save(
"HorizontalRuleExample.md"
);
|
使用案例5:如何閱讀Markdown文檔:
// This is Markdown document that was produced in example of UC3.
Document doc =
new
Document(
"QuotesExample.md"
);
// Let's remove Heading formatting from a Quote in the very last paragraph.
Paragraph paragraph = doc.FirstSection.Body.LastParagraph;
paragraph.ParagraphFormat.Style = doc.Styles[
"Quote"
];
doc.Save(
"QuotesModifiedExample.md"
);
|
將如下新屬性添加到Shape類:
////// Returns true if this Shape has a SmartArt object.
///public bool HasSmartArt
|
使用案例:在文檔中使用SmartArt計算多個形狀。
Document doc =
new
Document(@
"input.docx"
);
int
count =
0
;
foreach (Shape shape
in
doc.GetChildNodes(NodeType.Shape,
true
))
{
if
(shape.HasSmartArt)
count++;
}
Console.WriteLine(
"The document has {0} shapes with SmartArt."
, count);
|
新的公共屬性TextShaperFactory已添加到LayoutOptions類。
public ITextShaperFactory TextShaperFactory { get; set; }
應經過單獨的nuget-packages提供ITextShaperFactory的實現。具體實現應建立一個表示字體的文本整形器,並計算文本的整形信息。下面是一個用法示例:
public
void
Test()
{
// Open a document
Document doc =
new
Document(
"OpenType.Document.docx"
);
// When text shaper factory is set, layout starts to use OpenType features.
// An Instance property returns static BasicTextShaperCache object wrapping HarfBuzzTextShaperFactory
doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
// Render the document to PDF format
doc.Save(
"OpenType.Document.pdf"
);
}
|
添加了一個用於從文本框中獲取父形狀的公共屬性,以容許客戶從連接的TextBox中查找連接的Shape。
////// Determines whether this TextBox can be linked to the target Textbox.
///public bool IsValidLinkTarget(TextBox target)
{
}
////// Returns or sets a TextBox that represents the next TextBox in a sequence of shapes.
///public TextBox Next
{
get
;
set
;
}
////// Returns a TextBox that represents the previous TextBox in a sequence of shapes.
///public TextBox Previous
{
get
;
}
////// Breaks the forward link for the specified TextBox, if such a link exists.
///////// BreakForwardLink() doesn't break all other links in the current sequence of shapes.
/// For example: 1-2-3-4 sequence and BreakForwardLink at the 2-nd textbox will create
/// two sequences 1-2, 3-4.
///public void BreakForwardLink()
{
}
////// Gets a parent shape for the TextBox.
///public Shape Parent
{
get
{
return
mParent; }
}
|
使用案例:建立從shape1.TextBox到shape2.TextBox的連接。
TextBox textBox1 = shape1.TextBox;
TextBox textBox2 = shape2.TextBox;
if
(textBox1.IsValidLinkTarget(textBox2))
textBox1.Next = textBox2;
|
使用案例:檢查shape.TextBox是序列的頭部,尾部仍是中間。
TextBox textBox = shape.TextBox;
if
((textBox.Next !=
null
) && (textBox.Previous ==
null
))
{
//The head of the sequence.
}
if
((textBox.Next !=
null
) && (textBox.Previous !=
null
))
{
//The Middle of the sequence.
}
if
((textBox.Next ==
null
) && (textBox.Previous !=
null
))
{
//The Tail of the sequence.
}
|
使用案例:破壞shape.TextBox的連接。
TextBox textBox = shape.TextBox;
// Break a forward link
textBox.BreakForwardLink();
// Break a forward link by setting a null
textBox.Next =
null
;
// Break a link, which leads to this textbox
if
(textBox.Previous !=
null
)
textBox.Previous.BreakForwardLink();
|
ASPOSE技術交流QQ羣(642018183)已開通,各種資源及時分享,歡迎交流討論!