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[].Paragraphs[].AppendTextBox(, );
            TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
            TB.Format.HorizontalPosition = ;
            TB.Format.VerticalOrigin = VerticalOrigin.Page;
            TB.Format.VerticalPosition = ;             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 = ;             TB.Format.InternalMargin.Top = ;
            TB.Format.InternalMargin.Bottom = ;
            TB.Format.InternalMargin.Left = ;
            TB.Format.InternalMargin.Right = ;             TB.Format.FillEfects.Type = BackgroundType.Picture;
            TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");
步骤四:在文本框内添加段落文本,图片,设置字体,字体颜色,行间距,段后距,对齐方式等。然后保存文档,打开查看效果。
            Paragraph para1 = TB.Body.AddParagraph();
            para1.Format.AfterSpacing = ;
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange TR1 = para1.AppendText("李白");
            TR1.CharacterFormat.FontName = "华文新魏";
            TR1.CharacterFormat.FontSize = ;
            TR1.CharacterFormat.Bold = true;
           
            Paragraph para2 = TB.Body.AddParagraph();
            Image image = Image.FromFile("李白.jpg");
            DocPicture picture = para2.AppendPicture(image);
            picture.Width = ;
            picture.Height = ;
            para2.Format.AfterSpacing = ;
            para2.Format.HorizontalAlignment = HorizontalAlignment.Center;             Paragraph para3 = TB.Body.AddParagraph();
            TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
            TR2.CharacterFormat.FontName = "华文新魏";
            TR2.CharacterFormat.FontSize = ;
            para3.Format.LineSpacing = ;
            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[].Paragraphs[].AppendTextBox(, );
            TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
            TB.Format.HorizontalPosition = ;
            TB.Format.VerticalOrigin = VerticalOrigin.Page;
            TB.Format.VerticalPosition = ;             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 = ;             TB.Format.InternalMargin.Top = ;
            TB.Format.InternalMargin.Bottom = ;
            TB.Format.InternalMargin.Left = ;
            TB.Format.InternalMargin.Right = ;             TB.Format.FillEfects.Type = BackgroundType.Picture;
            TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");             Paragraph para1 = TB.Body.AddParagraph();
            para1.Format.AfterSpacing = ;
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange TR1 = para1.AppendText("李白");
            TR1.CharacterFormat.FontName = "华文新魏";
            TR1.CharacterFormat.FontSize = ;
            TR1.CharacterFormat.Bold = true;
           
            Paragraph para2 = TB.Body.AddParagraph();
            Image image = Image.FromFile("李白.jpg");
            DocPicture picture = para2.AppendPicture(image);
            picture.Width = ;
            picture.Height = ;
            para2.Format.AfterSpacing = ;
            para2.Format.HorizontalAlignment = HorizontalAlignment.Center;             Paragraph para3 = TB.Body.AddParagraph();
            TextRange TR2 = para3.AppendText("盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”");
            TR2.CharacterFormat.FontName = "华文新魏";
            TR2.CharacterFormat.FontSize = ;
            para3.Format.LineSpacing = ;
            para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
            para3.Format.SuppressAutoHyphens = true;
            
            document.SaveToFile("R1.docx");
            System.Diagnostics.Process.Start("R1.docx");
        
        }
    }
}

 

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

C#: 向Word插入排版精良的文本框的更多相关文章

  1. C#: 向Word插入排版精良的Text Box

    Text Box(文本框)是Word排版的工具之一.在Word文档正文的任何地方插入文本框,可添加补充信息,放在合适的位置,也不会影响正文的连续性.我们可以设置文本框的大小,线型,内部边距,背景填充等 ...

  2. Word插入htm文件导致文本域动态增加的一个问题

    受html标签影响,超链接也会被变成文本域(HYPERLINK).当遍历文本域进行替换之前如果预存了文本域的数量(Count/Length/etc.)将导致遗漏.

  3. C# 操作Word文本框——插入表格/读取表格/删除表格

    在文本框中,我们可以操作很多元素,如文本.图片.表格等,在本篇文章中将着重介绍如何插入表格到文本框,插入的表格我们可以对表格进行格式化操作来丰富表格内容.此外,对于文本框中的表格内容,我们也可以根据需 ...

  4. class3_Entry & Text 输入和文本框

    程序总体运行效果图如下;   #!/usr/bin/env python # -*- coding:utf-8 -*- # -------------------------------------- ...

  5. (9)Microsoft office Word 2013版本操作入门_文本框_word排版

    1.插入文本框 :[插入]---[文本框]可以自己画,也可以选择已有的模板 2.文本框设置 :点中文本框选择[格式]---[对齐文本]可以让文字上下居中,[形状填充]填充不同的颜色.[形状轮廓]可以改 ...

  6. C# 设置Word文本框中的文字旋转方向

    在Word中可插入文本框,默认情况下插入的文本框中的文字方向为横向排列,对于一些特殊文档的设计要求,需要改变文字方向,如本次测试中的文档排版为考生试卷类型,考生信息栏的内容为下图中的这种, 本文将以C ...

  7. *像word一样编辑复杂的文本:SpannableString 样式详介

    简介: 使用android.text.Spanned; android.text.SpannableString; android.text.SpannableStringBuilder; 和 and ...

  8. word中创建文本框

    word中创建文本框         在插入中点击"文本框"选项卡,例如以下图所看到的:        手工加入自己想要的文本框格式,然后选择所创建的文本框,在工具栏处会发现多了一 ...

  9. Java 添加Word文本框

    在Word中,文本框是指一种可移动.可调节大小的文字或图形容器.我们可以向文本框中添加文字.图片.表格等对象,下面,将通过Java编程来实现添加以上对象到Word文本框. 使用工具:Free Spir ...

随机推荐

  1. DOS 命令 os系统(windows)

    一.cd 相关操作 1."cd .. "or "cd ..\" --返回上一级 2.cd E:\Python -- 进入目录 二.dir --drectory ...

  2. 【.NET Core项目实战-统一认证平台】第五章 网关篇-自定义缓存Redis

    [.NET Core项目实战-统一认证平台]开篇及目录索引 上篇文章我们介绍了2种网关配置信息更新的方法和扩展Mysql存储,本篇我们将介绍如何使用Redis来实现网关的所有缓存功能,用到的文档及源码 ...

  3. 《响应式Web设计实践》学习笔记

    原书: 响应式Web设计实践 目录: 第2章 流动布局 1. 布局选项 2. 字体大小 3. 网格布局 4. 混合固定宽度和流动宽度 第3章 媒介查询 1. 视口 2. 媒介查询结构 3. 内嵌样式与 ...

  4. 感悟优化——Netty对JDK缓冲区的内存池零拷贝改造

    NIO中缓冲区是数据传输的基础,JDK通过ByteBuffer实现,Netty框架中并未采用JDK原生的ByteBuffer,而是构造了ByteBuf. ByteBuf对ByteBuffer做了大量的 ...

  5. dcloud 近期遇到的小知识一览

    1.form-data时,请求头改变为application/x-www-form-urlencoded. 2.下拉刷新,首先在page.json里面的style将enablePullDownRefr ...

  6. Spark机器学习(上)

    1.机器学习概念 1.1 机器学习的定义 在维基百科上对机器学习提出以下几种定义: l“机器学习是一门人工智能的科学,该领域的主要研究对象是人工智能,特别是如何在经验学习中改善具体算法的性能”. l“ ...

  7. 脑残式网络编程入门(六):什么是公网IP和内网IP?NAT转换又是什么鬼?

    本文引用了“帅地”发表于公众号苦逼的码农的技术分享. 1.引言 搞网络通信应用开发的程序员,可能会经常听到外网IP(即互联网IP地址)和内网IP(即局域网IP地址),但他们的区别是什么?又有什么关系呢 ...

  8. [Oracle]使用InstantClient访问Oracle数据库

    环境 操作系统: Win8.1 Enterprise Oracle开发工具: PL/SQL Developer 7.0.1.1066 (MBCS) 步骤 下载InstantClient Oracle官 ...

  9. PHP拿到接口数据返回的json以及传参-----ajax 跨域请求 ---

    以下测试------ <php $ch = curl_init(); $str = '';//此处为接口地址以及传参------- curl_setopt($ch, CURLOPT_URL, $ ...

  10. 利用 DynamicLinq 实现简单的动态表达式构建查询

    平时使用 LINQ 进行一些简单的条件拼接查询一般都会这样操作: public class SearchInputDto { public string ConditionA { get; set; ...