将 HTML 字符串代码附加到 .doc 时设置文本对齐方式
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
我们可以为 .doc 文件的段落设置亚洲排版,有四种文本对齐方式:顶部、中心、基线、底部和自动。在本文中,让我们看看如何在 C# 中将 HTML 字符串代码附加到 .doc 时设置对齐方式。
下载Spire.Doc 5.3.83或更高版本,然后添加对您项目的引用。
以下是步骤:
第 1 步:创建一个包含以下代码的 HTML 文件。
<html> <body> <i>f</i>(<i>x</i>)=<img align="middle" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACcAAAAlCAYAAADBa/A+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADvSURBVFhH7ZNbCsUwCES7/03nIkSwRh1rL00+PCCJVaeTPq5xMG2uSpuLuC7fwlZzZOxYc0TZnDwZ76WYzjVRjQnn57oQmdC5x9ses6IHUO7xd3NW8xNzVPPCwrtOLBXdbAlHgpJMX9SzVGQz7fUw55Eo87ZnqVAzh8x5z8jrHpl6pBNPb6bNVWlzVW7m5N+zKyT9Wqu0OYn3fVl8am754IHBz5+cpGxOP3qda9CNLNAMVESmmG3mMjw1lzrwXF0iEap5gUj1zNUlI0Jk+4i05lxNWCR1ysIh0Ixb1SJQCNQJ1pERgRU30uaqHGxujB+eJddCfZBWMwAAAABJRU5ErkJggg==" />
第 2 步:创建一个新文档并添加新部分。
Document doc2 = new Document(); doc2.AddSection();
第 3 步:创建新段落 p 并将其 TextAlign 属性设置为“Center”。
p.AppendText("Alignment:Center ");
p.AppendHTML(File.ReadAllText(@"test.html"));
p.Format.TextAlignment = TextAlignment.Center;
第 4 步 : 设置其他选项进行对比,Auto默认为Baseline。
Paragraph p1 = doc2.Sections[0].AddParagraph();
p1.AppendText("Alignment:Baseline ");
p1.AppendHTML(File.ReadAllText(@"test.html"));
p1.Format.TextAlignment = TextAlignment.Baseline;
Paragraph p2 = doc2.Sections[0].AddParagraph();
p2.AppendText("Alignment:Bottom ");
p2.AppendHTML(File.ReadAllText(@"test.html"));
p2.Format.TextAlignment = TextAlignment.Bottom;
Paragraph p3 = doc2.Sections[0].AddParagraph();
p3.AppendText("Alignment:Top ");
p3.AppendHTML(File.ReadAllText(@"test.html"));
p3.Format.TextAlignment = TextAlignment.Top;
Paragraph p4 = doc2.Sections[0].AddParagraph();
p4.AppendText("Alignment:Auto ");
p4.AppendHTML(File.ReadAllText(@"test.html"));
p4.Format.TextAlignment = TextAlignment.Auto;
第 5 步:保存并查看。
doc2.SaveToFile(@"test.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("test.doc");
屏幕截图:
完整代码:
using Spire.Doc;
using Spire.Doc.Documents;
using System.IO;
namespace SetTextAlignment
{
class Program
{
static void Main(string[] args)
{
Document doc2 = new Document();
doc2.AddSection();
Paragraph p = doc2.Sections[0].AddParagraph();
p.AppendText("Alignment:Center ");
p.AppendHTML(File.ReadAllText(@"test.html"));
p.Format.TextAlignment = TextAlignment.Center;
Paragraph p1 = doc2.Sections[0].AddParagraph();
p1.AppendText("Alignment:Baseline ");
p1.AppendHTML(File.ReadAllText(@"test.html"));
p1.Format.TextAlignment = TextAlignment.Baseline;
Paragraph p2 = doc2.Sections[0].AddParagraph();
p2.AppendText("Alignment:Bottom ");
p2.AppendHTML(File.ReadAllText(@"test.html"));
p2.Format.TextAlignment = TextAlignment.Bottom;
Paragraph p3 = doc2.Sections[0].AddParagraph();
p3.AppendText("Alignment:Top ");
p3.AppendHTML(File.ReadAllText(@"test.html"));
p3.Format.TextAlignment = TextAlignment.Top;
Paragraph p4 = doc2.Sections[0].AddParagraph();
p4.AppendText("Alignment:Auto ");
p4.AppendHTML(File.ReadAllText(@"test.html"));
p4.Format.TextAlignment = TextAlignment.Auto;
doc2.SaveToFile(@"test.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("test.doc");
}
}
}
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询 ;技术交流Q群(767755948)


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线