资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#操作Word文本框——插入图片、表格、文字、超链接等-创新互联

概述

Text Box(文本框)是Word排版的工具之一。在Word文档中的任何地方插入文本框,可添加补充信息,放在合适的位置,也不会影响正文的连续性。我们可以设置文本框的大小,线型,内部边距,背景填充等效果。文本框内可以图文混排,设置字体,字号,图片大小、文字链接,绘入表格等。
在下面的示例中,将分为两部分来介绍在Word中插入文本框,分别是:
第一部分:插入图文混排的文本框,包含图片填充,内部边距,图文混排、文字超链接等元素
第二部分:关于在文本框中插入表格、读取表格、删除表格等操作

坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都服务器托管小微创业公司专业提供企业网站设计营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。

使用工具

  • * Free Spire.Doc for .NET 6.3 (免费版)
  • Visual Stuido

示例操作

示例一】添加图文混排的文本框
C#

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;

namespace AddTextBox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document类,并加载Word文档
            Document document = new Document();
            document.LoadFromFile("Sample.docx");

            //获取首个section中的第一个Paragraph,并添加指定大小的文本框
            TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(180, 340);
            //指定文本框在页面中的位置
            TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
            TB.Format.HorizontalPosition = 330;
            TB.Format.VerticalOrigin = VerticalOrigin.Page;
            TB.Format.VerticalPosition = 110;

            //设置文本环绕方式
            TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
            TB.Format.TextWrappingType = TextWrappingType.Both;

            //格式化文本框
            TB.Format.LineStyle = TextBoxLineStyle.Double;
            TB.Format.LineColor = Color.Black;
            TB.Format.LineDashing = LineDashing.Solid;
            TB.Format.LineWidth = 3;
            TB.Format.InternalMargin.Top = 15;
            TB.Format.InternalMargin.Bottom = 10;
            TB.Format.InternalMargin.Left = 12;
            TB.Format.InternalMargin.Right = 10;

            //加载图片并填充图片作为文本框背景
            TB.Format.FillEfects.Type = BackgroundType.Picture;
            TB.Format.FillEfects.Picture = Image.FromFile(@"C:\Users\Administrator\Desktop\1.jpg");

            //添加段落1到文本框,并添加文本,设置文本格式
            Paragraph para1 = TB.Body.AddParagraph();
            para1.Format.AfterSpacing = 6;
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange TR1 = para1.AppendText("The TIMES");
            TR1.CharacterFormat.FontName = "Andalus";
            TR1.CharacterFormat.FontSize = 12;
            TR1.CharacterFormat.TextColor = Color.Black;
            //添加段落2,加载图片并设置图片大小、位置
            Paragraph para2 = TB.Body.AddParagraph();
            Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\The times.jpg");
            DocPicture picture = para2.AppendPicture(image);
            picture.Width = 120;
            picture.Height = 160;
            para2.Format.AfterSpacing = 8;
            para2.Format.HorizontalAlignment = HorizontalAlignment.Center;
            //添加段落3,插入文本并设置格式
            Paragraph para3 = TB.Body.AddParagraph();
            TextRange TR2 = para3.AppendText("The Times is the first newspaper to have borne that name, lending it to numerous other papers around the world, such as The Times of India and The New York Times. ");
            TR2.CharacterFormat.FontName = "Cambria";
            TR2.CharacterFormat.FontSize = 10;
            para3.Format.LineSpacing = 15;
            para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
            //插入超链接到指定字符串
            para3.Format.SuppressAutoHyphens = true;
            para3.AppendHyperlink("https://en.wikipedia.org/wiki/The_Times", "See more", HyperlinkType.WebLink);

            //保存并打开文档
            document.SaveToFile("Result.docx");
            System.Diagnostics.Process.Start("Result.docx"); 
        }
    }
}

文本框添加效果:
C# 操作Word文本框——插入图片、表格、文字、超链接等

示例2】Word文本框中插入表格、读取文本框中的表格、删除表格
1.插入表格
C#

using Spire.Doc;
using Spire.Doc.Documents; 
using Spire.Doc.Fields; 

namespace InsertTableToTextbox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {

            //创建一个Document类对象
            Document document = new Document();

            //添加section到文档
            Section section = document.AddSection();
            //添加段落section
            Paragraph paragraph = section.AddParagraph();

            //添加指定大小的文本框到段落
            TextBox textbox = paragraph.AppendTextBox(300, 100);

            //添加文本到文本,设置文本格式
            Paragraph textboxParagraph = textbox.Body.AddParagraph();
            TextRange textboxRange = textboxParagraph.AppendText("Sample Report 1");
            textboxRange.CharacterFormat.FontName = "Arial";

            //插入表格到文本框
            Table table = textbox.Body.AddTable(true);
            //指定表格行数、列数
            table.ResetCells(4, 4);
            //实例化数组内容
            string[,] data = new string[,]  
            {  
               {"Name","Age","Gender","ID" },  
               {"John","28","Male","0023" },  
               {"Steve","30","Male","0024" },  
               {"Lucy","26","female","0025" }  
            };

            //将数组内容添加到表格 
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]);
                    tableRange.CharacterFormat.FontName = "Arial";
                }
            }

            //应用表格样式
            table.ApplyStyle(DefaultTableStyle.MediumGrid3Accent1);

            //保存并打开文档
            document.SaveToFile("Output.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("Output.docx");
        }
    }
}

这里应用表格格式,Spire.Doc 支持多种不同的表格类型,可根据需要自行选择。
C# 操作Word文本框——插入图片、表格、文字、超链接等

表格插入效果:

C# 操作Word文本框——插入图片、表格、文字、超链接等

2. 读取表格
C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;
using System.Text;

namespace GetTableFromTextbox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //载入Word文档
            Document document = new Document("Output.docx");

            //获取第一个文本框
            TextBox textbox = document.TextBoxes[0];

            //获取文本框中第一个表格
            Table table = textbox.Body.Tables[0] as Table;
            //实例化StringBuilder类
            StringBuilder sb = new StringBuilder();

            //遍历表格中的段落并提取文本
            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Paragraph paragraph in cell.Paragraphs)
                    {
                        sb.AppendLine(paragraph.Text);
                    }
                }
            }
            File.WriteAllText("text.txt", sb.ToString());
        }
    }
}

读取结果:
C# 操作Word文本框——插入图片、表格、文字、超链接等

3.删除表格

C#

using Spire.Doc;
using Spire.Doc.Fields;

namespace RemoveTableFormTextbox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document实例
            Document document = new Document("Output.docx");

            //获取第一个文本框
            TextBox textbox = document.TextBoxes[0];

            //删除文本框中第一个表格
            textbox.Body.Tables.RemoveAt(0);

            //保存文档
            document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("RemoveTable.docx");
        }
    }
}

删除结果:
C# 操作Word文本框——插入图片、表格、文字、超链接等

以上全部内容为本次关于“C#操作Word文本框”的全部内容。如需转载,请注明出处。
感谢阅读!

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前文章:C#操作Word文本框——插入图片、表格、文字、超链接等-创新互联
本文网址:http://cdkjz.cn/article/ejiop.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220