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插入排版精良的Text Box的更多相关文章

  1. C#: 向Word插入排版精良的文本框

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

  2. 2、word插入目录、图/表

    一.word插入目录 依次对每个标题在“段落”中进行大纲级别选择. 光标定位于目录生成的页面,再“引用”->“目录”->选择“自动目录1/2”,则可自动生成目录.若目录有所更改,则可选择“ ...

  3. Word 插入目录详细教程 -- 视频教程(6)

    >> 视频教程链接:B站,速度快,清晰 更多关于插入目录的方法,参看:Word插入目录系列 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)

  4. [UE4]Text Box

    Text Box:文本输入控件. 一.新建一个名为testTextBox的UserWidget,添加一个名为“EditableTextBox_0”的TextBox到默认容器Canvas Panel 二 ...

  5. c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打

    由于NPOI暂时没找到书签内容替换功能,所以换用Apose.Word组件. using System; using System.Collections.Generic; using System.C ...

  6. openXML向Word插入表

    表是 Word 中的另一类型的块级内容,它是以行和列排列的一组段落(以及其他块级内容). Word 中的表格通过 tbl 元素定义,该元素类似于 HTML <表格>标记. 表元素指定文档中 ...

  7. 怎么用MathType解决Word公式排版很乱的问题

    现在办公室起草文件,期刊论文投稿.学校试着编辑都要先在Word中编辑好后再打印出来.在Word中编辑这些文本内容时,如果遇到公式就要使用专门的MathType公式编辑器.而有很多人在用MathType ...

  8. word插入图片显示不完整的解决的方法

    有时在编写word文档,插入图片时,会出现图不完整的情况. 解决方法是:选中图片,段落格式设置为单位行距(不是22磅),图片格式为嵌入式.问题解决.

  9. word插入行

    如何在Word中添加多行或多列 在弹出的列表中选择[插入],再选择[在下方插入行]即可. 选择多少行就可添加多少行. 按F4重复上一操作可快速添加. 添加列也同样如此,选中一个单元格,右键单击,在弹出 ...

随机推荐

  1. Codeforces Round #361 (Div. 2) D

    D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...

  2. (学)解决诡异的 Exception type: SocketException 127.0.0.1:80

    许久不发博了,老杨听完故事让我持续写一下“十万个为什么” 一.背景:  昨天我们亲密的战友HH刘老板亲临现场,指出我们协用的一个项目,客户方面反馈手持终端系统不定期“卡死”,要我们安排人飞到广州驻场解 ...

  3. 学习JavaScript闭包

    作者: 阮一峰 日期: 2009年8月30日 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于Javascript初 ...

  4. ubuntu 安装与开始学习

    下载地址 http://cn.ubuntu.com/download/ 经验: 1.遇到安装问题,首先尝试解读错误,再使用  ./configure --help  不行再上Stack overflo ...

  5. 谢欣伦 - 原创软件 - 游戏专题 - 操蛋的小鸟Fucking Bird

    前段时间朋友介绍了一个最近很火的游戏<Flappy Bird>.在工作之余,我用了三天时间做了一个类似的游戏<Fucking Bird>.一开始分享给了两个女同事,发现她们玩嗨 ...

  6. Unity - Apk包的代码与资源提取

    最近在研究如何给Unity游戏进行加密,让别人不能轻易破解你的apk包,不过网上的加密方法都是有对应的破解方法~_~!!结果加密方法没找到好的,逆向工程倒会了不少.今天就来讲解如何提取一个没做任何保护 ...

  7. 魅族M8时期写过几个app,纪念一下曾经的自己

    找工作的过程中也看了不少资料和文章,也学着别人弄弄博客,但发现自己临时的行为有点那啥吧..曾经我也写过不少东西,有过自己的一个技术论坛,为当时的魅族M8手机写过一个系列的技术帖子,但因为论坛被我关了, ...

  8. 广州PostgreSQL用户会技术交流会小记 2015-9-19

    广州PostgreSQL用户会技术交流会小记 2015-9-19 今天去了广州PostgreSQL用户会组织的技术交流会 分别有两个session 第一个讲师介绍了他公司使用PostgreSQL-X2 ...

  9. JVM 备注

    一.堆内存分布: JAVA 分为堆内存和栈内存,GC主要针对堆内存 1)Young: 存放新生内存对象 1.1)Eden JVM 刚开始分配的对象 1.2)Survivor1(from) 1.3)Su ...

  10. ASP.Net请求处理机制初步探索之旅 - Part 2 核心

    开篇:上一篇我们了解了一个请求从客户端发出到服务端接收并转到ASP.Net处理入口的过程,这篇我们开始探索ASP.Net的核心处理部分,借助强大的反编译工具,我们会看到几个熟悉又陌生的名词(类):Ht ...