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

 更新时间:2016年09月28日 11:10:00   作者:Yesi  
这篇文章主要为大家详细介绍了C#向Word插入排版精良的Text Box的相关方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

相关文章

  • C# 通过 oledb 操作Excel实例代码

    C# 通过 oledb 操作Excel实例代码

    本篇文章主要介绍了C# 通过 oledb 操作Excel实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • C#调用Windows的API实现窗体动画

    C#调用Windows的API实现窗体动画

    在VF、VB、PB的应用中,有些无法通过语言工具本身来完成的或者做得不理想的功能,我们会考虑通过Windows的API来完成。本文就来通过调用Windows的API实现窗体动画,感兴趣的可以尝试一下
    2022-11-11
  • C# Timer控件学习之使用Timer解决按钮幂等性问题

    C# Timer控件学习之使用Timer解决按钮幂等性问题

    Timer控件又称定时器控件或计时器控件,该控件的主要作用是按一定的时间间隔周期性地触发一个名为Tick的事件,因此在该事件的代码中可以放置一些需要每隔一段时间重复执行的程序段,这篇文章主要介绍了关于C#使用Timer解决按钮幂等性问题的相关资料,需要的朋友可以参考下
    2022-10-10
  • winform实现创建最前端窗体的方法

    winform实现创建最前端窗体的方法

    这篇文章主要介绍了winform实现创建最前端窗体的方法,涉及C#窗体属性设置的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-08-08
  • C#正则表达式与HashTable详解

    C#正则表达式与HashTable详解

    这篇文章主要介绍了C#正则表达式与HashTable详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-07-07
  • C#基础概念二十五问 16-20

    C#基础概念二十五问 16-20

    C#基础概念二十五问 16-20...
    2007-04-04
  • C#设计模式实现之迭代器模式

    C#设计模式实现之迭代器模式

    迭代器模式把对象的职责分离,职责分离可以最大限度减少彼此之间的耦合程度,从而建立一个松耦合的对象,这篇文章主要给大家介绍了关于C#设计模式实现之迭代器模式的相关资料,需要的朋友可以参考下
    2021-08-08
  • C#从文件或标准输入设备读取指定行的方法

    C#从文件或标准输入设备读取指定行的方法

    这篇文章主要介绍了C#从文件或标准输入设备读取指定行的方法,涉及C#文件及IO操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • Unity3D实现旋钮控制灯光效果

    Unity3D实现旋钮控制灯光效果

    这篇文章主要为大家详细介绍了Unity3D实现旋钮控制灯光效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • C#预定义的基础类型转换

    C#预定义的基础类型转换

    这篇文章介绍了C#预定义的基础类型转换,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论