Text Box(文本框)是Word排版的工具之一。在Word文档正文的任何地方插入文本框,可添加补充信息,放在合适的位置,也不会影响正文的连续性。我们可以设置文本框的大小,线型,内部边距,背景填充等效果。文本框内可以图文混排,设置字体,字号,图片大小等。 在日常使用中,我们很容易忽略这些元素,仅仅插入一个黑色单线,仅含文字的文本框。因而,我觉得有必要向大家介绍并制作一个版式精良的文本框,抛砖引玉。

本篇博文主要介绍,如何使用C#在Word文档的特定位置,插入一个有图片填充,内部边距,图文混排,线型精致的文本框。感兴趣的博友请从E-iceblue下载Free Spire.Doc,并添加为Visual Studio引用。

需要用的命名空间:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
using System.Drawing;

步骤详解:

步骤一:加载一个只含有文本的Word文档,如下图。
Document document = new Document();
            document.LoadFromFile("李白生平.docx");

 

步骤二:在加载的Word文档中添加一个文本框,并设定其具体位置。这里需要考虑两点:插入的页和页面的位置。即:在哪一页插入这个文本框,文本框在该页的位置。只有定位好这两点,文本框的位置才能具体确认。此外,还需考虑文本框和文本的位置关系,即设置位置和自动换行(text wrapping)。所以,以下代码,通过设定文本框在哪一段落,相较于页面的位置和自动换行,来确定其位置。
TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300);
            TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
            TB.Format.HorizontalPosition = 370;
            TB.Format.VerticalOrigin = VerticalOrigin.Page;
            TB.Format.VerticalPosition = 155;

TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
            TB.Format.TextWrappingType = TextWrappingType.Both;

步骤三:设置文本框框的颜色,内部边距,图片填充。
TB.Format.LineStyle = TextBoxLineStyle.Double;
            TB.Format.LineColor = Color.LightGoldenrodYellow;
            TB.Format.LineDashing = LineDashing.Solid;
            TB.Format.LineWidth = 3;

TB.Format.InternalMargin.Top = 12;
            TB.Format.InternalMargin.Bottom = 8;
            TB.Format.InternalMargin.Left = 12;
            TB.Format.InternalMargin.Right = 12;

TB.Format.FillEfects.Type = BackgroundType.Picture;
            TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");

步骤四:在文本框内添加段落文本,图片,设置字体,字体颜色,行间距,段后距,对齐方式等。然后保存文档,打开查看效果。
Paragraph para1 = TB.Body.AddParagraph();
            para1.Format.AfterSpacing = 6;
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange TR1 = para1.AppendText("李白");
            TR1.CharacterFormat.FontName = "华文新魏";
            TR1.CharacterFormat.FontSize = 16;
            TR1.CharacterFormat.Bold = true;
           
            Paragraph para2 = TB.Body.AddParagraph();
            Image image = Image.FromFile("李白.jpg");
            DocPicture picture = para2.AppendPicture(image);
            picture.Width = 120;
            picture.Height = 160;
            para2.Format.AfterSpacing = 8;
            para2.Format.HorizontalAlignment = HorizontalAlignment.Center;

Paragraph para3 = TB.Body.AddParagraph();
            TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
            TR2.CharacterFormat.FontName = "华文新魏";
            TR2.CharacterFormat.FontSize = 11;
            para3.Format.LineSpacing = 15;
            para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
            para3.Format.SuppressAutoHyphens = true;

document.SaveToFile("R1.docx");
            System.Diagnostics.Process.Start("R1.docx");

效果图:

完整代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Documents;
using System.Drawing;

namespace textbox
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("李白生平.docx");

TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300);
            TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
            TB.Format.HorizontalPosition = 370;
            TB.Format.VerticalOrigin = VerticalOrigin.Page;
            TB.Format.VerticalPosition = 155;

TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
            TB.Format.TextWrappingType = TextWrappingType.Both;

TB.Format.LineStyle = TextBoxLineStyle.Double;
            TB.Format.LineColor = Color.LightGoldenrodYellow;
            TB.Format.LineDashing = LineDashing.Solid;
            TB.Format.LineWidth = 3;

TB.Format.InternalMargin.Top = 12;
            TB.Format.InternalMargin.Bottom = 8;
            TB.Format.InternalMargin.Left = 12;
            TB.Format.InternalMargin.Right = 12;

TB.Format.FillEfects.Type = BackgroundType.Picture;
            TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");

Paragraph para1 = TB.Body.AddParagraph();
            para1.Format.AfterSpacing = 6;
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange TR1 = para1.AppendText("李白");
            TR1.CharacterFormat.FontName = "华文新魏";
            TR1.CharacterFormat.FontSize = 16;
            TR1.CharacterFormat.Bold = true;
           
            Paragraph para2 = TB.Body.AddParagraph();
            Image image = Image.FromFile("李白.jpg");
            DocPicture picture = para2.AppendPicture(image);
            picture.Width = 120;
            picture.Height = 160;
            para2.Format.AfterSpacing = 8;
            para2.Format.HorizontalAlignment = HorizontalAlignment.Center;

Paragraph para3 = TB.Body.AddParagraph();
            TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
            TR2.CharacterFormat.FontName = "华文新魏";
            TR2.CharacterFormat.FontSize = 11;
            para3.Format.LineSpacing = 15;
            para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
            para3.Format.SuppressAutoHyphens = true;
            
            document.SaveToFile("R1.docx");
            System.Diagnostics.Process.Start("R1.docx");
        
        }
    }
}

 

感谢阅读,欢迎评论交流。

转载于:https://www.cnblogs.com/Yesi/p/4894166.html

C#: 向Word插入排版精良的文本框相关推荐

  1. Word海报随意排版、链接文本框有妙招

    Word海报随意排版.链接文本框有妙招 1.插入文本框 2.文本框内文字状态 3.创建文本框之间的链接 4.插入图片 这篇文章将带我们学习 文本框排版是Word排版中最灵活的方式 文字和图片实现所见即 ...

  2. javascript 获取光标所选中的内容并插入到另一个文本框中(兼容ie和ff)

    项目中正好用到 做下笔记方便以后查找 ie获取光标的位置使用document.selection.createRange() 火狐下使用document.getElementById(id).sele ...

  3. 将word文档中所有文本框中的内容转换成普通段落

    某些pdf转word工具生成的word文档,会将很多内容放在文本框中,以至于编辑word文档不太方便,例如想全选所有段落中的文字再调整某些格式,文本框中的文字有时候不能像普通段落中的文字一样产生同样的 ...

  4. java中添加文本框_Java 添加、删除Word文档中的文本框

    在Word文档中,文本框是指一种可移动.可调大小的文字或图形容器.使用文本框,能够使文档在内容和形式上更为饱满.本文将通过使用Java编程来演示如何添加.删除Word文档中的文本框. Jar文件获取及 ...

  5. PowerPoint PPT 模板插入可修改的文本框

    PowerPoint PPT 母版插入可修改的文本框 请您尝试在幻灯片母板中"插入占位符",而非选择插入文本框.

  6. Word控件Spire.Doc 【文本】教程(5) ;从 Word 文档中的文本框中提取文本

    文本框的目的是允许用户输入程序要使用的文本信息.也可以从文本框中提取现有的文本信息.以下指南重点介绍如何通过Spire.Doc for .NET从 C# 中 Word 文档的文本框中提取文本. Spi ...

  7. html5文本框里插图片文字,word应用教程:在文本框内插入图片

    在使用word编辑电子文档时,可以通过插入功能插入图片,那么怎样才能在wps文字文档从插入可以任意移动的图片呢,那么下面就由学习啦小编给大家分享下word在文本框内插入图片的技巧,希望能帮助到您. w ...

  8. Python读取\修改word文档中的文本框内容

    本文所指的 word文档,都是docx结尾的,如果是doc结尾的,请参考上篇:点我 我们绝大多数的需求都是 利用python-docx 来读取word文档中的内容,进而再对内容进行其他处理,如下代码, ...

  9. Python批量提取docx格式Word文档中所有文本框内的文本

    功能描述: 批量提取指定Word文档(docx格式)中所有文本框中的文本. 测试文件: 参考代码: 执行结果:

最新文章

  1. 二进制搜索树_二进制搜索树数据结构举例说明
  2. 30分钟无坑部署K8S单Master集群
  3. Java题-直接赋值与重新创建内存
  4. What's the QPSK?
  5. 使用Jenkins配置自动化构建
  6. drools 7.x定时器
  7. python基础学习笔记(九)
  8. 连续型切片与离散加减的思路学习
  9. 对sppnet网络的理解
  10. Google 开源最新 NLP 模型,能处理整本《罪与罚》
  11. linux驱动视频采集卡,在linux下使用视频采集卡
  12. 离散数学复习--第二章:一阶逻辑
  13. db4o发布7.2,出现.NET 3.5版本,支持LINQ
  14. allennlp使用
  15. 欧盟gmp中的计算机系统验证,欧盟GMP中的计算机系统验证
  16. Android设备图标显示模糊问题
  17. MOOC单片机原理及应用题库大全
  18. Java实现扫码枪二维码自动跳转网页(基于键盘监听)
  19. 关于module ‘keras.applications’ has no attribute ‘nasnet’/ ‘keras’ has no attribute ‘application’的解决方案
  20. 想要快乐陪伴左右吗?多种提高多巴胺的方法送给你

热门文章

  1. 【python教程入门学习】ASCII编码,将英文存储到计算机
  2. python爬取豆瓣《狂暴巨兽》评分影评,matplotlib和wordcloud制作评分图和词云图
  3. 在线答题考试小程序源码系统 支持在线刷题+考试二合一+安装部署教程
  4. 小程序+spring boot心理测评与活动管理系统毕业设计-附源码191752
  5. 微信小程序根据后台传过来的活动时间xxxx-xx-xx xx:xx:xx格式,实现倒计时功能
  6. 跑在树莓派上智能家居雏形
  7. 什么是前后端分离与前后端不分离
  8. oracle分区名称能不能相同,Oracle分区介绍
  9. 基于DataHub元数据血缘管理实施方案
  10. 矩阵常用归一化方法:z-score,L2,最大最小值归一化