亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2016年09月28日 11:10:00   作者:Yesi  
這篇文章主要為大家詳細介紹了C#向Word插入排版精良的Text Box的相關方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Text Box(文本框)是Word排版的工具之一。在Word文檔正文的任何地方插入文本框,可添加補充信息,放在合適的位置,也不會影響正文的連續(xù)性。我們可以設置文本框的大小,線型,內(nèi)部邊距,背景填充等效果。文本框內(nèi)可以圖文混排,設置字體,字號,圖片大小等。 在日常使用中,我們很容易忽略這些元素,僅僅插入一個黑色單線,僅含文字的文本框。因而,我覺得有必要向大家介紹并制作一個版式精良的文本框,拋磚引玉。

本篇博文主要介紹,如何使用C#在Word文檔的特定位置,插入一個有圖片填充,內(nèi)部邊距,圖文混排,線型精致的文本框。感興趣的博友請從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; 

步驟三:設置文本框框的顏色,內(nèi)部邊距,圖片填充。

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

 

步驟四:在文本框內(nèi)添加段落文本,圖片,設置字體,字體顏色,行間距,段后距,對齊方式等。然后保存文檔,打開查看效果。

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C# 通過 oledb 操作Excel實例代碼

    C# 通過 oledb 操作Excel實例代碼

    本篇文章主要介紹了C# 通過 oledb 操作Excel實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • C#調(diào)用Windows的API實現(xiàn)窗體動畫

    C#調(diào)用Windows的API實現(xiàn)窗體動畫

    在VF、VB、PB的應用中,有些無法通過語言工具本身來完成的或者做得不理想的功能,我們會考慮通過Windows的API來完成。本文就來通過調(diào)用Windows的API實現(xiàn)窗體動畫,感興趣的可以嘗試一下
    2022-11-11
  • C#?Timer控件學習之使用Timer解決按鈕冪等性問題

    C#?Timer控件學習之使用Timer解決按鈕冪等性問題

    Timer控件又稱定時器控件或計時器控件,該控件的主要作用是按一定的時間間隔周期性地觸發(fā)一個名為Tick的事件,因此在該事件的代碼中可以放置一些需要每隔一段時間重復執(zhí)行的程序段,這篇文章主要介紹了關于C#使用Timer解決按鈕冪等性問題的相關資料,需要的朋友可以參考下
    2022-10-10
  • winform實現(xiàn)創(chuàng)建最前端窗體的方法

    winform實現(xiàn)創(chuàng)建最前端窗體的方法

    這篇文章主要介紹了winform實現(xiàn)創(chuàng)建最前端窗體的方法,涉及C#窗體屬性設置的相關技巧,非常具有實用價值,需要的朋友可以參考下
    2015-08-08
  • C#正則表達式與HashTable詳解

    C#正則表達式與HashTable詳解

    這篇文章主要介紹了C#正則表達式與HashTable詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • C#基礎概念二十五問 16-20

    C#基礎概念二十五問 16-20

    C#基礎概念二十五問 16-20...
    2007-04-04
  • C#設計模式實現(xiàn)之迭代器模式

    C#設計模式實現(xiàn)之迭代器模式

    迭代器模式把對象的職責分離,職責分離可以最大限度減少彼此之間的耦合程度,從而建立一個松耦合的對象,這篇文章主要給大家介紹了關于C#設計模式實現(xiàn)之迭代器模式的相關資料,需要的朋友可以參考下
    2021-08-08
  • C#從文件或標準輸入設備讀取指定行的方法

    C#從文件或標準輸入設備讀取指定行的方法

    這篇文章主要介紹了C#從文件或標準輸入設備讀取指定行的方法,涉及C#文件及IO操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • Unity3D實現(xiàn)旋鈕控制燈光效果

    Unity3D實現(xiàn)旋鈕控制燈光效果

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)旋鈕控制燈光效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • C#預定義的基礎類型轉(zhuǎn)換

    C#預定義的基礎類型轉(zhuǎn)換

    這篇文章介紹了C#預定義的基礎類型轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05

最新評論