当前位置:网站首页>C#/VB.NET:在 Word 中设置文本对齐方式
C#/VB.NET:在 Word 中设置文本对齐方式
2022-08-04 10:05:00 【InfoQ】
文本对齐是一种段落格式属性,通常用于确定整个段落中的文本外观。在日常工作中,我们所常见的四种文本对齐方式分别为:左对齐、居中对齐、右对齐和两端对齐。接下来,我将展示如何通过
Spire.Doc for .NET
在C#和VB.NET程序中
为Word文档设置文本对齐方式。
安装 Spire.Doc for .NET
首先,我们需要将 Spire.Doc for.NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。可以从
此链接
下载 DLL 文件,也可以通过
NuGet
安装 DLL 文件。
PM> Install-Package Spire.Doc
在Word中设置文本对齐方式
详细步骤如下:
l 创建
Document
类的
实例。
l 使用
Document.LoadFromFile
()
方法加载示例 Word 文档。
l 利用
Document.Sections[]
属性获取指定部分。
l 利用
Section.Paragraphs[]
属性获取指定的段落。
l 利用
Paragraph.Format
属性
获取段落格式。
l 利用
ParagraphFormat.HorizontalAlignment
属性为指定段落设置文本对齐方式。
l 使用
Document.SaveToFile
()
方法将文档保存到另一个文件。
C#:
using Spire.Doc;
using Spire.Doc.Documents;
namespace AlignText
{
class Program
{
static void Main(string[] args)
{
//创建Document类的实例
Document doc = new Document();
//加载示例文档
doc.LoadFromFile(@"sample.docx");
//获取第一部分
Section section = doc.Sections[0];
//获取第一段并使之居中对齐
Paragraph p = section.Paragraphs[0];
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//获取第二段并使之左对齐
Paragraph p1 = section.Paragraphs[1];
p1.Format.HorizontalAlignment = HorizontalAlignment.Left;
//获取第三段并使之右对齐
Paragraph p2 = section.Paragraphs[2];
p2.Format.HorizontalAlignment = HorizontalAlignment.Right;
//获取第四段并使之两端对齐
Paragraph p3 = section.Paragraphs[3];
p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;
//保存结果文档
doc.SaveToFile("result.docx", FileFormat.Docx);
}
}
}
VB.NET:
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace AlignText
Friend Class Program
Shared Sub Main(ByVal args() As String)
'创建Document类的实例
Dim doc As New Document()
'加载示例文档
doc.LoadFromFile("sample.docx")
'获得第一部分
Dim section As Section = doc.Sections(0)
'获取第一段并使之居中对齐
Dim p As Paragraph = section.Paragraphs(0)
p.Format.HorizontalAlignment = HorizontalAlignment.Center
'获取第二段并使之左对齐
Dim p1 As Paragraph = section.Paragraphs(1)
p1.Format.HorizontalAlignment = HorizontalAlignment.Left
'获取第三段并使之右对齐
Dim p2 As Paragraph = section.Paragraphs(2)
p2.Format.HorizontalAlignment = HorizontalAlignment.Right
'获取第四段并使之两端对齐
Dim p3 As Paragraph = section.Paragraphs(3)
p3.Format.HorizontalAlignment = HorizontalAlignment.Justify
'保存结果文档
doc.SaveToFile("result.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

边栏推荐
- JDBC知识点
- Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
- HCIP 交换实验
- HCIP 第十八天
- 学习在微信小程序中判断url的文件后缀格式
- MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时
- DOM简述
- MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
- MindSpore:mirrorpad算子速度过慢的问题
- 请问下Flink SQL如何写hologres分区表?我想要每天一个分区
猜你喜欢
随机推荐
Acwing 3208. Z字形扫描 偏移量+扩展图
浅聊偏函数
学习使用php把stdClass Object转array的方法整理
云计算适合什么企业_当前全球云计算处于发展
物体颜色的来源
MindSpore:损失函数问题
常用的输入对象
函数防抖与函数节流
暴力破解-破解 Apache BASIC 认证
Producer and Consumer Problems in Concurrent Programming
pyvista 的介绍与使用
How to restore the Youxuan database with only data files
Win11不识别蓝牙适配器的解决方法
学习在php中将特大数字转成带有千/万/亿为单位的字符串
Inheritance and the static keyword
rk3399-339 usb设备复合 总体流程
MindSpore:MindSpore GPU版本安装问题
微信小程序自定义组件-城市选择「建议收藏」
HCIP 交换实验
LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串









