• Word控件Spire.Doc 【段落处理】教程(九):在 C# 中将 HTML 字符串代码附加到 .doc 时设置文本对齐方式


    我们可以为 .doc 文件的段落设置亚洲排版,有四种文本对齐方式:顶部、中心、基线、底部和自动。在本文中,让我们看看如何在 C# 中将 HTML 字符串代码附加到 .doc 时设置对齐方式。

    Spire.Doc for.NET 最新下载(资源qun:767755948)https://www.evget.com/product/3368/download

    下载最新版spire.doc,然后添加对您项目的引用。

    以下是步骤:

    第 1 步:创建一个包含以下代码的 HTML 文件。

    
    
    f(x)=

    第 2 步:创建一个新文档并添加新部分。

    Document doc2 = new Document();
    doc2.AddSection();

    第 3 步:创建新段落 p 并将其 TextAlignment 属性设置为“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");
    
    }
    }
    }

  • 相关阅读:
    90%的程序员不适合做独立开发
    《Java并发编程的艺术》总结
    多线程【thread】创建【1】
    leetcode 387. First Unique Character in a String(字符串中第一个独一无二的字母)
    springboot面向园区管理的物联网云平台设计与实现毕业设计源码150916
    多链路自检与灵活组网:新能源充电桩物联网5G工业路由器
    力扣刷题day33|509斐波那契数、70爬楼梯、746使用最小花费爬楼梯
    亮相“外滩金融峰会” 百望云实力入选“融城杯金融科技创新十佳案例”
    基于Druid的HiveSQL血缘解析
    Unity | API鉴权用到的函数汇总
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126597682