• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#向Word插入排版精良的TextBox

C#向Word插入排版精良的TextBox

作者:Yesi 字体:[增加 减小] 来源:互联网 时间:2017-05-28

Yesi 通过本文主要向大家介绍了c#textbox,c#中textbox控件,c#textbox换行,c#textbox多行显示,c#textbox滚动条等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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;
 </div>

步骤详解:

步骤一:加载一个只含有文本的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; 

</div>

步骤三:设置文本框框的颜色,内部边距,图片填充。

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");

 </div>

步骤四:在文本框内添加段落文本,图片,设置字体,字体颜色,行间距,段后距,对齐方式等。然后保存文档,打开查看效果。

      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");
 </div>

效果图:

 

完整代码示例: 

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");
    
    }
  }
}

</div>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#实现手机拍照并且保存水印照片
  • C#向Word插入排版精良的TextBox
  • C#如何给word文档添加水印
  • C#如何给PDF文件添加水印
  • C#中给Excel添加水印的具体方法
  • C#向图片添加水印的两种不同场景与解决方法
  • c#格式化数字的方法
  • C#给图片加水印的简单实现方法
  • C#中设置textbox限制条件的方法
  • C#递归遍历窗体所有textbox控件并设置textbox事件的方法

相关文章

  • 2017-05-28C#使用自定义算法对数组进行反转操作的方法
  • 2017-05-28.Net WInform开发笔记(三)谈谈自制控件(自定义控件)
  • 2017-05-28C#使用读写锁三行代码简单解决多线程并发的问题
  • 2017-05-28C#在RichTextBox中显示不同颜色文字的方法
  • 2017-05-28C#简单实现文件上传功能
  • 2017-05-28在Framework 4.0中:找出新增的方法与新增的类(二)
  • 2017-05-28C#简单多线程同步和优先权用法实例
  • 2017-05-28基于C#的电视台节目表接口调用代码
  • 2017-05-28C# 中的var关键字详细介绍
  • 2017-05-28使用SmtpClient发送邮件的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#实现左截取和右截取字符串实例
    • C#编程之事务用法
    • 用序列化实现List<T> 实例的深复制(推荐)
    • 举例说明Java多线程编程中读写锁的使用
    • c#获取gridview的值代码分享
    • C#中面向对象编程机制之多态学习笔记
    • C#函数式编程中的缓存技术详解
    • C#调用CMD命令实例
    • .NET程序页面中,操作并输入cmd命令的小例子
    • winfrom 在业务层实现事务控制的小例子

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有