介绍
Aspose.Words FOSS for .NET 包含与商业版 Aspose.Words 引擎相同的文本格式化对象模型:用于运行级属性的 Font、用于段落级属性的 ParagraphFormat、用于编号和项目符号列表的 ListFormat、用于定位文本框的 FrameFormat,以及用于查找替换的 Range.Replace。本指南专注于该层——对文档中已有文本进行格式化和改写——而非从头构建文档结构(请参阅 introduction post 以了解本次发布的概览)。
该库采用 MIT 许可证,无原生依赖,目标为 .NET Standard 2.0,因此可在 .NET Framework 4.6.2+ 和 .NET 6、8、10 上运行。可通过 NuGet 安装,或从源码构建(见下文快速入门)。这里描述的所有内容均适用于使用 .docx、.docm、.dotx、.dotm 以及使用 new Document(fileName) 加载的 Flat OPC 文件,或使用 DocumentBuilder 在内存中构建的文档。
由于 Aspose.Words FOSS for .NET 是原始的 Aspose.Words 代码库的免费核心版,而非重写,因此 Font、ParagraphFormat、ListFormat 和 FrameFormat 对象模型可直接迁移到商业版 Aspose.Words for .NET,以便项目后期需要页面布局、渲染或本版本未包含的额外格式转换器时使用。
关键特性
带字体的运行级格式化
每个 Run 都公开一个 Font 属性,DocumentBuilder.Font 设置将应用于随后写入文本的字体。除了显而易见的 Bold、Italic 和 Underline(一个包含 Single、Double、Dotted、Dash、Wavy 以及它们的 *Heavy 变体的 Underline 枚举),Font 覆盖了 StrikeThrough、DoubleStrikeThrough、Superscript、Subscript、SmallCaps、AllCaps、Hidden、Shadow、Outline、Emboss、Engrave、Color、HighlightColor、Spacing、Position、Kerning、Scaling 和 EmphasisMark。Font.Style、Font.StyleName 和 Font.StyleIdentifier 将运行绑定回字符样式,Font.ClearFormatting() 将运行上的直接格式化重置为其样式默认值。DocumentBuilder 还直接将 Bold、Italic、Underline 公开为快捷方式,并提供 PushFont() 和 PopFont() 用于在临时格式更改前后保存和恢复构建器的当前字体状态。
使用 ParagraphFormat 的段落格式化
ParagraphFormat(可通过 Paragraph.ParagraphFormat 或 DocumentBuilder.ParagraphFormat 访问)通过 ParagraphAlignment 枚举(Left、Center、Right、Justify、Distributed,以及阿拉伯语 kashida 和泰语分布变体)控制对齐,通过 LeftIndent、RightIndent 和 FirstLineIndent(对东亚布局有 CharacterUnit* 等价物)控制缩进,并通过 SpaceBefore、SpaceAfter 与 LineSpacingRule/LineSpacing 控制间距。分页行为由 KeepTogether、KeepWithNext、PageBreakBefore 和 WidowControl 控制,Bidi 将段落标记为从右到左。自定义制表位存放在 ParagraphFormat.TabStops 中,它是一个由 TabStop 对象组成的 TabStopCollection,每个对象都有一个 Position、一个 TabAlignment(Left、Center、Right、Decimal、Bar)以及一个 TabLeader(None、Dots、Dashes、Line、Heavy、MiddleDot)。与 Font 类似,ParagraphFormat.Style 和 StyleName 将段落链接到命名的 Style,ClearFormatting() 清除直接的段落格式。
使用 ListFormat 的列表
ListFormat(在 Paragraph.ListFormat 或 DocumentBuilder.ListFormat 上)对段落应用列表格式:ApplyBulletDefault() 和 ApplyNumberDefault() 将其切换为默认的项目符号或编号列表,RemoveNumbers() 完全移除列表格式,ListIndent()/ListOutdent() 在列表层级之间移动。ListFormat.List 返回底层的 List 定义,ListFormat.ListLevel 返回该段落实际使用的 ListLevel,其中包含每级设置,如 NumberStyle、NumberFormat、Alignment(ListLevelAlignment 为 Left、Center 或 Right 之一)、StartAt、RestartAfterLevel、特定层级的 Font,以及 TrailingCharacter(ListTrailingCharacter 为 Tab、Space 或 Nothing)控制列表标签与段落文本之间的间距。新列表定义来自 ListCollection.Add(listTemplate),使用内置的 ListTemplate 预设之一(BulletDefault、NumberArabicDot、NumberUppercaseRomanDot 等),或使用 AddSingleLevelList 创建单层列表;文档的列表可通过 Document.Lists 枚举。
使用 FrameFormat 的文本框
Paragraph.FrameFormat 是一个只读的报告对象:其上的每个属性都反映段落的 当前的 框架状态,而不是让你直接设置它。 IsFrame 报告段落当前是否为框架; Width, Height, 并且 HeightRule 报告其大小; HorizontalPosition 并且 VerticalPosition, 以及 RelativeHorizontalPosition 并且 RelativeVerticalPosition,报告它相对于页面、页边距、栏或段落的锚定位置;并且 HorizontalDistanceFromText/VerticalDistanceFromText 报告框架与周围正文之间的间距。 HorizontalAlignment 并且 VerticalAlignment 报告内容在框架内的对齐方式。
使用 Range 和 IReplacingCallback 的查找和替换
Range.Replace——可在 Document.Range 上对整个文档进行一次遍历,或在任何节点的 Range 上使用——提供了普通模式/替换对的重载,以及与 FindReplaceOptions 实例结合的模式/替换对,支持基于正则表达式的匹配。FindReplaceOptions.MatchCase 和 FindWholeWordsOnly 缩小匹配范围;Direction(FindReplaceDirection.Forward 或 Backward)设置扫描顺序;ApplyFont 和 ApplyParagraphFormat 允许替换过程将格式传递到替换文本;一组 Ignore* 标志(IgnoreDeleted、IgnoreInserted、IgnoreFields、IgnoreFieldCodes、IgnoreFootnotes、IgnoreStructuredDocumentTags、IgnoreShapes、IgnoreOfficeMath)可从匹配中排除特定内容类别。对于固定字符串无法表达的替换逻辑,实现 IReplacingCallback.Replacing(args) 并将其设为 FindReplaceOptions.ReplacingCallback(或传递给接受回调的 FindReplaceOptions 构造函数之一);args 参数是一个 ReplacingArgs,公开 Match、MatchNode、MatchOffset,以及可设置的 Replacement 字符串,以便回调能够为每个匹配计算值。相关的 ReplaceAction 枚举(Replace、Skip、Stop)描述了操作期间单个匹配的可能结果。
快速入门
Aspose.Words FOSS 用于 .NET 可通过 NuGet 获取:
dotnet add package Aspose.Words.FOSS改为从源码构建:
git clone https://github.com/aspose-words-foss/Aspose.Words-FOSS-for-.NET.git
cd Aspose.Words-FOSS-for-.NET
dotnet build Aspose.Words.sln -c Release
然后向 Aspose.Words.csproj 添加项目引用。 在撰写时进行文本格式化,请在调用 Write()、Writeln() 或 InsertParagraph() 之前,设置 DocumentBuilder.Font 属性(Bold、Italic、Underline)、DocumentBuilder.ParagraphFormat 属性(Alignment、缩进、间距)以及 DocumentBuilder.ListFormat(ApplyBulletDefault()、ApplyNumberDefault())。构建器会将当前的格式状态应用到之后写入的所有内容,直至您再次更改,或将临时更改包裹在 PushFont()/PopFont() 中。 要重新格式化或重写文档中已有的文本,使用 new Document(fileName) 打开文档并对 doc.Range 调用 Range.Replace() ——可以使用普通的模式/替换重载,或使用接受带有 MatchCase、FindWholeWordsOnly 或自定义 IReplacingCallback 的 FindReplaceOptions 的重载 ——随后 Save() 结果。
支持的格式
| 格式 | 扩展名 | 读取 | 写 |
|---|---|---|---|
| DOCX | .docx | ✓ | ✓ |
| DOCM | .docm | ✓ | ✓ |
| DOTX | .dotx | ✓ | ✓ |
| DOTM | .dotm | ✓ | ✓ |
| Flat OPC (所有变体) | 各种 | ✓ | ✓ |
| Markdown | .md | ✓ | ✓ |
| Text | .txt | ✓ | ✓ |
上述格式化类的行为一致,无论文档是从哪些格式加载或保存。此版本不支持读取或写入 DOC、RTF、ODT、HTML、EPUB、MHTML、MOBI、AZW3 或 WordML,也不支持 PDF、XPS 或图像导出,因为页面布局和渲染超出其范围。
开源与许可证
Aspose.Words FOSS for .NET 在 MIT 许可证下发布,可免费用于商业和个人用途,无需版税或重新分发限制。完整源代码,包括上述 Font、ParagraphFormat、ListFormat、FrameFormat 和 Range 实现,已在 GitHub 的 Aspose.Words FOSS for .NET 仓库 中提供。