C#操作Word文本框——插入圖片、表格、文字、超鏈接等-創(chuàng)新互聯(lián)

概述

Text Box(文本框)是Word排版的工具之一。在Word文檔中的任何地方插入文本框,可添加補充信息,放在合適的位置,也不會影響正文的連續(xù)性。我們可以設(shè)置文本框的大小,線型,內(nèi)部邊距,背景填充等效果。文本框內(nèi)可以圖文混排,設(shè)置字體,字號,圖片大小、文字鏈接,繪入表格等。
在下面的示例中,將分為兩部分來介紹在Word中插入文本框,分別是:
第一部分:插入圖文混排的文本框,包含圖片填充,內(nèi)部邊距,圖文混排、文字超鏈接等元素
第二部分:關(guān)于在文本框中插入表格、讀取表格、刪除表格等操作

堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都服務(wù)器托管小微創(chuàng)業(yè)公司專業(yè)提供企業(yè)網(wǎng)站設(shè)計營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。

使用工具

  • * Free Spire.Doc for .NET 6.3 (免費版)
  • Visual Stuido

示例操作

示例一】添加圖文混排的文本框
C#

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;

namespace AddTextBox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化Document類,并加載Word文檔
            Document document = new Document();
            document.LoadFromFile("Sample.docx");

            //獲取首個section中的第一個Paragraph,并添加指定大小的文本框
            TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(180, 340);
            //指定文本框在頁面中的位置
            TB.Format.HorizontalOrigin = HorizontalOrigin.Page;
            TB.Format.HorizontalPosition = 330;
            TB.Format.VerticalOrigin = VerticalOrigin.Page;
            TB.Format.VerticalPosition = 110;

            //設(shè)置文本環(huán)繞方式
            TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
            TB.Format.TextWrappingType = TextWrappingType.Both;

            //格式化文本框
            TB.Format.LineStyle = TextBoxLineStyle.Double;
            TB.Format.LineColor = Color.Black;
            TB.Format.LineDashing = LineDashing.Solid;
            TB.Format.LineWidth = 3;
            TB.Format.InternalMargin.Top = 15;
            TB.Format.InternalMargin.Bottom = 10;
            TB.Format.InternalMargin.Left = 12;
            TB.Format.InternalMargin.Right = 10;

            //加載圖片并填充圖片作為文本框背景
            TB.Format.FillEfects.Type = BackgroundType.Picture;
            TB.Format.FillEfects.Picture = Image.FromFile(@"C:\Users\Administrator\Desktop\1.jpg");

            //添加段落1到文本框,并添加文本,設(shè)置文本格式
            Paragraph para1 = TB.Body.AddParagraph();
            para1.Format.AfterSpacing = 6;
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange TR1 = para1.AppendText("The TIMES");
            TR1.CharacterFormat.FontName = "Andalus";
            TR1.CharacterFormat.FontSize = 12;
            TR1.CharacterFormat.TextColor = Color.Black;
            //添加段落2,加載圖片并設(shè)置圖片大小、位置
            Paragraph para2 = TB.Body.AddParagraph();
            Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\The times.jpg");
            DocPicture picture = para2.AppendPicture(image);
            picture.Width = 120;
            picture.Height = 160;
            para2.Format.AfterSpacing = 8;
            para2.Format.HorizontalAlignment = HorizontalAlignment.Center;
            //添加段落3,插入文本并設(shè)置格式
            Paragraph para3 = TB.Body.AddParagraph();
            TextRange TR2 = para3.AppendText("The Times is the first newspaper to have borne that name, lending it to numerous other papers around the world, such as The Times of India and The New York Times. ");
            TR2.CharacterFormat.FontName = "Cambria";
            TR2.CharacterFormat.FontSize = 10;
            para3.Format.LineSpacing = 15;
            para3.Format.HorizontalAlignment = HorizontalAlignment.Left;
            //插入超鏈接到指定字符串
            para3.Format.SuppressAutoHyphens = true;
            para3.AppendHyperlink("https://en.wikipedia.org/wiki/The_Times", "See more", HyperlinkType.WebLink);

            //保存并打開文檔
            document.SaveToFile("Result.docx");
            System.Diagnostics.Process.Start("Result.docx"); 
        }
    }
}

文本框添加效果:
C# 操作Word文本框——插入圖片、表格、文字、超鏈接等

示例2】Word文本框中插入表格、讀取文本框中的表格、刪除表格
1.插入表格
C#

using Spire.Doc;
using Spire.Doc.Documents; 
using Spire.Doc.Fields; 

namespace InsertTableToTextbox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {

            //創(chuàng)建一個Document類對象
            Document document = new Document();

            //添加section到文檔
            Section section = document.AddSection();
            //添加段落section
            Paragraph paragraph = section.AddParagraph();

            //添加指定大小的文本框到段落
            TextBox textbox = paragraph.AppendTextBox(300, 100);

            //添加文本到文本,設(shè)置文本格式
            Paragraph textboxParagraph = textbox.Body.AddParagraph();
            TextRange textboxRange = textboxParagraph.AppendText("Sample Report 1");
            textboxRange.CharacterFormat.FontName = "Arial";

            //插入表格到文本框
            Table table = textbox.Body.AddTable(true);
            //指定表格行數(shù)、列數(shù)
            table.ResetCells(4, 4);
            //實例化數(shù)組內(nèi)容
            string[,] data = new string[,]  
            {  
               {"Name","Age","Gender","ID" },  
               {"John","28","Male","0023" },  
               {"Steve","30","Male","0024" },  
               {"Lucy","26","female","0025" }  
            };

            //將數(shù)組內(nèi)容添加到表格 
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]);
                    tableRange.CharacterFormat.FontName = "Arial";
                }
            }

            //應(yīng)用表格樣式
            table.ApplyStyle(DefaultTableStyle.MediumGrid3Accent1);

            //保存并打開文檔
            document.SaveToFile("Output.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("Output.docx");
        }
    }
}

這里應(yīng)用表格格式,Spire.Doc 支持多種不同的表格類型,可根據(jù)需要自行選擇。
C# 操作Word文本框——插入圖片、表格、文字、超鏈接等

表格插入效果:

C# 操作Word文本框——插入圖片、表格、文字、超鏈接等

2. 讀取表格
C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;
using System.Text;

namespace GetTableFromTextbox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //載入Word文檔
            Document document = new Document("Output.docx");

            //獲取第一個文本框
            TextBox textbox = document.TextBoxes[0];

            //獲取文本框中第一個表格
            Table table = textbox.Body.Tables[0] as Table;
            //實例化StringBuilder類
            StringBuilder sb = new StringBuilder();

            //遍歷表格中的段落并提取文本
            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Paragraph paragraph in cell.Paragraphs)
                    {
                        sb.AppendLine(paragraph.Text);
                    }
                }
            }
            File.WriteAllText("text.txt", sb.ToString());
        }
    }
}

讀取結(jié)果:
C# 操作Word文本框——插入圖片、表格、文字、超鏈接等

3.刪除表格

C#

using Spire.Doc;
using Spire.Doc.Fields;

namespace RemoveTableFormTextbox_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //創(chuàng)建Document實例
            Document document = new Document("Output.docx");

            //獲取第一個文本框
            TextBox textbox = document.TextBoxes[0];

            //刪除文本框中第一個表格
            textbox.Body.Tables.RemoveAt(0);

            //保存文檔
            document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("RemoveTable.docx");
        }
    }
}

刪除結(jié)果:
C# 操作Word文本框——插入圖片、表格、文字、超鏈接等

以上全部內(nèi)容為本次關(guān)于“C#操作Word文本框”的全部內(nèi)容。如需轉(zhuǎn)載,請注明出處。
感謝閱讀!

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。

新聞名稱:C#操作Word文本框——插入圖片、表格、文字、超鏈接等-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://muchs.cn/article8/ejiop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、電子商務(wù)App開發(fā)、小程序開發(fā)外貿(mào)網(wǎng)站建設(shè)、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司