C#如何在一个word文档中插入文本框
我想做一个C#程序,在一个word文档页面上方插入一个有一定内容文本框,字体楷体,三号红字,想向各位大神请教主要代码,共同学习,谢谢!...
我想做一个C#程序,在一个word文档页面上方插入一个有一定内容文本框,字体楷体,三号红字,想向各位大神请教主要代码,共同学习,谢谢!
展开
2个回答
展开全部
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();
}
{
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();
}
追问
高手您好,我已经顺利插入了文本框,您好像没有提及怎么在文本框里面加入文字啊,麻烦告诉小弟,谢谢!
而且我想让文本框没有边框,里面文字是红色,字体是楷体,能否好人做到底啊
展开全部
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);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |