Aspose.Words for .NET v19.7最新更新功能示例詳解!| 附下載
Aspose.Words for .NET更新至v19.7,為Markdown格式實現基本的讀寫器,同時實現了檢測SmartArt形狀的功能!接下來我們給大家介紹一下新版中引入的公告API的更改,並用示例實際闡述一下。>>下載Aspose.Words for .NET最新試用版
Aspose.Words 19.7公共API的更改
▲添加了屬性Revision.Group
在Revision類中添加了以下新屬性:
////// 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功能實現了基本的讀寫器
暫時支援以下Markdown功能:
- 標題
- 成批引用
- 橫向規則
- 黑體強調
- 斜體強調
增加了新的公開列舉:
LoadFormat.Markdown
SaveFormat.Markdown
FileFormat.Markdown
|
添加了新的TxtSaveOptionsBase類:
////// 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形狀的段落。
使用案例1:如何生成以下Markdown文件的重點:
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"
);
|
▲實現了檢測SmartArt形狀的功能
將以下新屬性新增到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);
|
▲實現了對OpenType字型的支援和對Kerning功能的支援
新的公共屬性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"
);
}
|
▲WORDSNET-11297 - 實現了用於處理連結文字框的公共API
添加了一個用於從文字框中獲取父形狀的公共屬性,以允許客戶從連結的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)已開通,各類資源及時分享,歡迎交流討