zoukankan      html  css  js  c++  java
  • C# Word中设置/更改文本方向

    C# Word中设置/更改文本方向

    一般情况下在Word中输入的文字都是横向的,今天给大家分享两种方法来设置/更改一个section内的所有文本的方向及部分文本的方向,有兴趣的朋友可以试下。

    首先,从https://visualstudiogallery.msdn.microsoft.com/d3a38f74-3490-42da-bdb0-37fa5acebc36下载免费版.NET Word类库并安装,然后创建一个C# 控制台应用程序,添加引用及命名空间并参考以下步骤。

    步骤1创建一个新的Document对象并加载Word文档。

    Document document = new Document();
    document.LoadFromFile("示例.docx");

    步骤2为一个section内的所有文本设置文本方向。

    //获取第一个section并为其设置文本方向
    Section section = document.Sections[0];
    section.TextDirection = TextDirection.RightToLeftRotated;

    如果要设置部分文本的文本方向,可以将该文本放在table中然后再设置文本方向,如以下步骤:

    步骤3添加一个新的section和一个table,获取目标单元格并设置文本方向,然后将文本添加到单元格。

    //添加一个新的section到文档
    Section sec = document.AddSection();
    //添加一个table到该section
    Table table = sec.AddTable();
    //添加一行和一列到table
    table.ResetCells(1, 1);
    //获取单元格
    TableCell cell = table.Rows[0].Cells[0];
    table.Rows[0].Height = 50;
    table.Rows[0].Cells[0].Width = 5;
    //设置单元格的文本方向并添加文本到该单元格
    cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;
    cell.AddParagraph().AppendText("你好");

    添加一个新的段落来检测以上方法是否会影响该section内的其他文本的文本方向: 

    sec.AddParagraph().AppendText("新段落");

    步骤4保存文档。

    document.SaveToFile("文本方向.docx", FileFormat.Docx);

    运行结果:

    设置一个section内的所有文本的文本方向:

                            

    设置部分文本的文本方向:

     

    全部代码:

    using Spire.Doc;
    using Spire.Doc.Documents;
    
    namespace Set_text_direction_in_Word
    {
        class Program
        {
            static void Main(string[] args)
            {
                Document document = new Document();
                document.LoadFromFile("示例.docx");
                //设置一个section内的所有文本的文本方向
                Section section = document.Sections[0];
                section.TextDirection = TextDirection.RightToLeftRotated;           
    
                //设置部分文本的文本方向
                Section sec = document.AddSection();
                Table table = sec.AddTable();
                table.ResetCells(1, 1);
                TableCell cell = table.Rows[0].Cells[0];
                table.Rows[0].Height = 50;
                table.Rows[0].Cells[0].Width = 5;
                cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;
    
                cell.AddParagraph().AppendText("你好");
    
                sec.AddParagraph().AppendText("新段落");
     
                //保存文档
                document.SaveToFile("文本方向.docx", FileFormat.Docx);
            }
        }
    }
  • 相关阅读:
    ubuntu 11.10(32位系统)下编译android源码
    12 个基于 Rails 框架开发的 CMS 系统
    36 个 CSS 框架推荐
    再来 10 个新鲜的 HTML5 教程
    汇编程序开发环境搭配(转)
    推荐:介绍一个UndoFramework
    细数 Windows 平台上的 NoSQL 数据库
    使用ShareKit一键分享到Facebook,Twitter等平台
    25个jQuery的编程小抄
    10款iOS高效开发必备的ObjectiveC类库
  • 原文地址:https://www.cnblogs.com/Yesi/p/5807017.html
Copyright © 2011-2022 走看看