C#如何在一个word文档中插入文本框

我想做一个C#程序,在一个word文档页面上方插入一个有一定内容文本框,字体楷体,三号红字,想向各位大神请教主要代码,共同学习,谢谢!... 我想做一个C#程序,在一个word文档页面上方插入一个有一定内容文本框,字体楷体,三号红字,想向各位大神请教主要代码,共同学习,谢谢! 展开
 我来答
ljt502032774
推荐于2016-01-06 · 超过35用户采纳过TA的回答
知道答主
回答量:70
采纳率:0%
帮助的人:59.4万
展开全部
if (OpenDialog1.ShowDialog() == DialogResult.OK)
{
object filename = OpenDialog1.FileName.Trim();
//Insert a paragraph at the beginning of the document.
Object Nothing = Type.Missing;
MSWord.Application WordApp = new MSWord.ApplicationClass();

//创建一个名为WordDoc的文档对象
MSWord.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

object oEndOfDoc = "\\endofdoc";
MSWord.Shape shape;
MSWord.Shape shape1;
object type = 1;
object type1 = 3;
object unit = 3;
object count = 1;
object extend = 0;
object oClassType = "WMPlayer.OCX.7";
object oClassType1 = "ShockwaveFlash.ShockwaveFlash.8";
object lefts = 150;
object tops = 90;
object widths = 100;
object heights = 100;
shape = WordDoc.Shapes.AddCanvas(1, 1, 100, 100, ref Nothing); //插入一个画布对象
shape.CanvasItems.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 20, 20, 50, 50);//画布上插入一个文本框

shape.Select(ref Nothing); //选中
WordApp.Application.Selection.TypeParagraph(); //插入段落
WordApp.Application.Selection.InsertBreak(ref type);插入分页符
shape = WordDoc.Shapes.AddCanvas(10, 20, 200, 200, ref Nothing);//在另一页插入一个画布对象
shape.CanvasItems.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 20, 20, 150, 150);
shape.Select(ref Nothing);
shape = WordDoc.Shapes.AddOLEControl(ref oClassType, ref lefts, ref tops, ref widths, ref heights, ref Nothing);

shape.Select(ref Nothing);
WordApp.Application.Selection.TypeParagraph();
WordApp.Application.Selection.InsertBreak(ref type);
shape = WordDoc.Shapes.AddCanvas(0, 50, 300, 300, ref Nothing);
shape.Select(ref Nothing);
shape = WordDoc.Shapes.AddOLEControl(ref oClassType1, ref lefts, ref tops, ref widths, ref heights, ref Nothing);

shape.Select(ref Nothing);
WordApp.Application.Selection.TypeParagraph();
WordApp.Application.Selection.InsertBreak(ref type);
shape = WordDoc.Shapes.AddCanvas(0, 70, 400, 400, ref Nothing);
shape.CanvasItems.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 20, 20, 50, 50);
shape.Select(ref Nothing);
shape = WordDoc.Shapes.AddOLEControl(ref oClassType1, ref lefts, ref tops, ref widths, ref heights, ref Nothing);
shape.Select(ref Nothing);
WordApp.Application.Selection.TypeParagraph();
WordApp.Application.Selection.InsertBreak(ref type);
shape = WordDoc.Shapes.AddCanvas(0, 0, 500, 500, ref Nothing);
shape.Select(ref Nothing);
shape = WordDoc.Shapes.AddOLEControl(ref oClassType, ref lefts, ref tops, ref widths, ref heights, ref Nothing);
shape.Select(ref Nothing);
WordApp.Application.Selection.TypeParagraph();
WordApp.Application.Selection.InsertBreak(ref type);
shape = WordDoc.Shapes.AddCanvas(0, 0, 500, 500, ref Nothing);
shape.CanvasItems.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 10, 20, 50, 50);
shape.CanvasItems.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 35, 20, 50, 50);
shape.Select(ref Nothing);
shape = WordDoc.Shapes.AddOLEControl(ref oClassType1, ref lefts, ref tops, ref widths, ref heights, ref Nothing);
shape.Select(ref Nothing);
shape = WordDoc.Shapes.AddOLEControl(ref oClassType1, ref lefts, ref tops, ref widths, ref heights, ref Nothing);

shape.Select(ref Nothing);
WordApp.Application.Selection.TypeParagraph();
WordApp.Application.Selection.InsertBreak(ref type);
shape = WordDoc.Shapes.AddCanvas(0, 0, 500, 500, ref Nothing);
shape.Select(ref Nothing);
shape = WordDoc.Shapes.AddOLEControl(ref oClassType, ref lefts, ref tops, ref widths, ref heights, ref Nothing);

WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
追问
高手您好,我已经顺利插入了文本框,您好像没有提及怎么在文本框里面加入文字啊,麻烦告诉小弟,谢谢!
而且我想让文本框没有边框,里面文字是红色,字体是楷体,能否好人做到底啊
博思aippt
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT... 点击进入详情页
本回答由博思aippt提供
Jack4DotNet
2017-11-20 · TA获得超过128个赞
知道小有建树答主
回答量:218
采纳率:91%
帮助的人:31.1万
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace WordTextbox
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document对象
            Document doc = new Document();

            //添加section和段落
            Section section = doc.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //在段落上添加文本框
            TextBox tb = paragraph.AppendTextBox(140, 250);

            //设置文本框相对页边距的位置
            tb.Format.HorizontalOrigin = HorizontalOrigin.Margin;
            tb.Format.HorizontalPosition = 0;
            tb.Format.VerticalOrigin = VerticalOrigin.Margin;
            tb.Format.VerticalPosition = 20;

            //在文本框中添加段落一,并在段落一插入图片
            Paragraph para1 = tb.Body.AddParagraph();
            Image image = Image.FromFile("hualuogeng.png");
            DocPicture picture = para1.AppendPicture(image);

            //设置段落格式     
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
            para1.Format.AfterSpacing = 8;

            //在文本框中添加段落二,添加文本到段落二
            Paragraph para2 = tb.Body.AddParagraph();
            TextRange textRange = para2.AppendText("华罗庚(1910.11.12—1985.6.12),出生于江苏常州金坛区,祖籍江苏丹阳。数学家,中国科学院院士,美国国家科学院外籍院士,第三世界科学院院士,联邦德国巴伐利亚科学院院士。");
            textRange.CharacterFormat.FontName = "黑体";
            textRange.CharacterFormat.FontSize = 9;

            //设置段落格式    
            para2.Format.HorizontalAlignment = HorizontalAlignment.Left;
            para2.Format.LineSpacing = 15;

            //保存文档
            doc.SaveToFile("文本框.docx", FileFormat.Docx2013);
        }
    }
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式