Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose.Words for .NET更新至v19.7,为Markdown格式实现基本的读写器,同时实现了检测SmartArt形状的功能!接下来我们给大家介绍一下新版中引入的公告API的更改,并用示例实际阐述一下。
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.Words最新试用下载可点击下方“了解更多”哦~
本文暂时没有评论,来添加一个吧(●'◡'●)