Spire.Doc 14.3 现已发布。本次更新新增支持“定义网格时自动调整为右缩进”与“为字体调整字间距”功能,同时提供多个新接口用于添加与操作 SmartArt 图形。此外,三个在操作 Word 文档时出现问题也已成功被修复。详情如下。
新功能:
- 支持设置“定义网格时自动调整为右缩进”功能。
- 支持设置“为字体调整字间距”功能。
- 支持添加和操作 SmartArt 图形。
- 支持获取 SmartArt 中所有节点的文本。
paragraph.Format.AdjustRightIndent =true;//默认值是true
textRange.CharacterFormat.Kerning = 2.5f;
Document document = new Document();
Section section = document.AddSection();
Spire.Doc.Documents.Paragraph paragraph = section.AddParagraph(); paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
Spire.Doc.Fields.TextRange textRange = paragraph.AppendText("RepeatingBendingProcess");
textRange.CharacterFormat.FontSize = 28f;
textRange.CharacterFormat.FontName = "Times New Roman";
paragraph = section.AddParagraph();
paragraph = section.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
// 添加采用“分段流程”版式的SmartArt图形
Spire.Doc.Fields.Shapes.Shape shape = paragraph.AppendSmartArt(SmartArtType.RepeatingBendingProcess, 432, 252);
SmartArt repeatingBendingSmartArt = shape.SmartArt;
// 添加节点文本
SmartArtNode process1 = repeatingBendingSmartArt.Nodes[0];
process1.Text = "1";
((Spire.Doc.Fields.TextRange)process1.Paragraphs[0].ChildObjects[0]).CharacterFormat.FontName = "Calibri";
((Spire.Doc.Fields.TextRange)process1.Paragraphs[0].ChildObjects[0]).CharacterFormat.FontSize = 20f;
((Spire.Doc.Fields.TextRange)process1.Paragraphs[0].ChildObjects[0]).CharacterFormat.TextColor = Color.Crimson;
// 添加节点文本
SmartArtNode process2 = repeatingBendingSmartArt.Nodes[1];
process2.Text = "2";
((Spire.Doc.Fields.TextRange)process2.Paragraphs[0].ChildObjects[0]).CharacterFormat.FontSize = 15f;
// 添加节点文本
SmartArtNode process3 = repeatingBendingSmartArt.Nodes[2];
process3.Text = "3";
((Spire.Doc.Fields.TextRange)process3.Paragraphs[0].ChildObjects[0]).CharacterFormat.FontSize = 10f;
// 添加节点文本
SmartArtNode process4 = repeatingBendingSmartArt.Nodes[3];
process4.Text = "4";
((Spire.Doc.Fields.TextRange)process4.Paragraphs[0].ChildObjects[0]).CharacterFormat.FontSize = 10f;
// 添加节点文本
SmartArtNode process5 = repeatingBendingSmartArt.Nodes[4];
process5.Text = "5";
((Spire.Doc.Fields.TextRange)process5.Paragraphs[0].ChildObjects[0]).CharacterFormat.FontSize = 10f;
document.SaveToFile(outputFile, FileFormat.Docx);
document.Close();
using (Document document = new Document(inputFile))
{
// 遍历所有节
foreach (Section section in document.Sections)
{
if (section?.Paragraphs == null) continue;
// 遍历节内所有段落
foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs)
{
foreach (var childObj in paragraph.ChildObjects)
{
if (childObj is Spire.Doc.Fields.Shapes.Shape shape && shape.HasSmartArt)
{
SmartArt smartArt = shape.SmartArt;
if (smartArt == null) continue;
TraverseSmartArtNodes(smartArt.Nodes, builder, 0);
}
}
}
}
}
public static void TraverseSmartArtNodes(SmartArtNodeCollection nodes, StringBuilder builder, int level)
{
if (nodes == null || nodes.Count == 0) return;
for (int nodeIdx = 0; nodeIdx < nodes.Count; nodeIdx++)
{
SmartArtNode node = nodes[nodeIdx];
if (node == null) continue;
// 清理节点文本
string nodeText = node.Text != null ? node.Text.Trim() : "空文本";
if (nodeText == "\r" || string.IsNullOrEmpty(nodeText)) continue;
// 拼接节点层级标识
string nodePrefix;
switch (level)
{
case 0:
nodePrefix = "smartArt.Nodes";
break;
case 1:
nodePrefix = "smartArt.Nodes.ChildNodes";
break;
case 2:
nodePrefix = "smartArt.Nodes.ChildNodes.ChildNodes";
break;
default:
nodePrefix = $"smartArt.Nodes.Level{level}";
break;
}
// 基础文本输出
builder.AppendLine($"{nodePrefix}_{nodeIdx}:{nodeText}");
// 递归处理子节点
TraverseSmartArtNodes(node.ChildNodes, builder, level + 1);
}
}
问题修复:
- 修复了获取段落文本不正确的问题。
- 修复了转换 Word 到 PDF,阿拉伯文本字体被改变的问题。
- 修复了提取页面出现多余空白页的问题
获取Spire.Doc 14.3,请点击:







