在C#中使用Spire.doc对word的操作总结
在C#中使用Spire.doc对word的操作总结
在最近的工程中我们要处理一些word文档。通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复。极少数可以完全实现的word组件又要收费。功夫不负有心人,终于找到了可以满足我们需要的免费的C# word程序库。为了和其他的作比较,我在这里先做以下汇总。希望对大家有帮助。
如何得到?
这个免费版的word 组件可以在Codeplex下载到,你也可以从本文里直接下载msi文件。它还提供了一些源代码。
Word操作汇总
1、 打开已有word文件,这是要处理word文件的最基本也是必须的步骤。他提供了三种方法。
方法1:从已知特定的文档中初始化一个新的Document 类的实例
Document document = new Document(@"..\..\Sample.docx");
方法2、从文件中加载一个word文件
Document document = new Document();
document.LoadFromFile(@"..\..\Sample.docx");
方法3、从流文件中加载word文件
Stream stream = File.OpenRead(@"..\..\Sample.docx");
Document document = new Document(stream);
2、如何创建表格
Document document = new Document();
Section section = document.AddSection();
Table table = section.AddTable(true);
table.ResetCells(, );
TableRow row = table.Rows[];
row.IsHeader = true;
Paragraph para = row.Cells[].AddParagraph();
TextRange TR = para.AppendText("Item");
para = row.Cells[].AddParagraph();
TR = para.AppendText("Description");
para = row.Cells[].AddParagraph();
TR = para.AppendText("Qty");
document.SaveToFile("WordTable.docx");
System.Diagnostics.Process.Start("WordTable.docx");
我们还可以设置行高和列宽
3、如何插入超链接?你可以插入两种超链接,Email 链接和webmail 链接。
Documentdocument =newDocument();
Section section = document.AddSection();
//Insert URL hyperlink
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Home page");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com",HyperlinkType.WebLink);
//Insert email address hyperlink
paragraph = section.AddParagraph();
paragraph.AppendText("Contact US");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com",HyperlinkType.EMailLink);
document.SaveToFile("Hyperlink.docx");
System.Diagnostics.Process.Start("Hyperlink.docx");
4、如何加入注解
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Home Page of ");
TextRange textRange = paragraph.AppendText("e-iceblue");
Comment comment1 = paragraph.AppendComment("www.e-iceblue.com");
comment1.AddItem(textRange);
comment1.Format.Author = "Harry Hu";
comment1.Format.Initial = "HH";
document.SaveToFile("Comment.docx");
System.Diagnostics.Process.Start("Comment.docx");
5、如何加入书签
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendBookmarkStart("SimpleBookMark");
paragraph.AppendText("This is a simple book mark.");
paragraph.AppendBookmarkEnd("SimpleBookMark");
document.SaveToFile("Bookmark.docx");
System.Diagnostics.Process.Start("Bookmark.docx");
6、合并邮件
Document document = new Document();
document.LoadFromFile("Fax.doc");
string[] filedNames = new string[] { "Contact Name", "Fax", "Date" };
string[] filedValues = new string[] { "John Smith", "+1 (69) 123456", System.DateTime.Now.Date.ToString() };
document.MailMerge.Execute(filedNames, filedValues);
document.SaveToFile("MailMerge.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("MailMerge.doc");
7、加入表单,这部分包含创建以及填入表单域。
创建表单
//Add new section to document
Section section = document.AddSection();
//Add Form to section
private void AddForm(Section section)
//add text input field
TextFormField field
= fieldParagraph.AppendField(fieldId, FieldType.FieldFormTextInput) as TextFormField;
//add dropdown field
DropDownFormField list
= fieldParagraph.AppendField(fieldId, FieldType.FieldFormDropDown) as DropDownFormField;
//add checkbox field
fieldParagraph.AppendField(fieldId, FieldType.FieldFormCheckBox);
填入表单域
//Fill data from XML file
using (Stream stream = File.OpenRead(@"..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
Fill data:
foreach (FormField field in document.Sections[].Body.FormFields)
{
String path = String.Format("{0}/text()", field.Name);
XPathNavigator propertyNode = user.SelectSingleNode(path);
if (propertyNode != null)
{
switch (field.Type)
{
case FieldType.FieldFormTextInput:
field.Text = propertyNode.Value;
break;
case FieldType.FieldFormDropDown:
DropDownFormField combox = field as DropDownFormField;
for(int i = ; i < combox.DropDownItems.Count; i++)
{
if (combox.DropDownItems[i].Text == propertyNode.Value)
{
combox.DropDownSelectedIndex = i;
break;
}
if (field.Name == "country" && combox.DropDownItems[i].Text =="Others")
{
combox.DropDownSelectedIndex = i;
}
}
break;
case FieldType.FieldFormCheckBox:
if (Convert.ToBoolean(propertyNode.Value))
{
CheckBoxFormField checkBox = field as CheckBoxFormField;
checkBox.Checked = true;
}
break;
}
}
}
}
8、合并word文档
//Load two documents
//Load Document1 and Document2
Document DocOne = new Document();
DocOne.LoadFromFile(@"E:\Work\Document\welcome.docx", FileFormat.Docx);
Document DocTwo = new Document();
DocTwo.LoadFromFile(@"E:\Work\Document\New Zealand.docx", FileFormat.Docx);
//Merge
foreach (Section sec in DocTwo.Sections)
{
DocOne.Sections.Add(sec.Clone());
}
//Save and Launch
DocOne.SaveToFile("Merge.docx", FileFormat.Docx);
9、保护文档。你可以设置密码或者加入水印来进行保护。文字和图片的水印都支持。
//Protect with password
document.Encrypt("eiceblue");
//Add Text watermark:
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "Microsoft";
txtWatermark.FontSize = ;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
//Add Image watermark:
PictureWatermark picture = new PictureWatermark();
picture.Picture = System.Drawing.Image.FromFile(@"..\imagess.jpeg");
picture.Scaling = ;
document.Watermark = picture;
10、转换功能是在处理word文档时最常见的操作了。使用免费版的Spire.doc for .NET, 转换变得很简单。只要包含三行类似的代码你就可以把word转换成其他常用格式,像PDF,HTML和图片。
Word转换成PDF
SaveToFile("Target PDF.PDF", FileFormat.PDF);
Word转换成图片
Image image = document.SaveToImages(, ImageType.Bitmap);
image.Save("Sample.tiff", ImageFormat.Tiff);
Word转换成HTML
document.SaveToFile("Target HTML.html", FileFormat.Html);
WordDocViewer(""Target HTML.html");
结论:
这是一个免费又强大的C# word 组件,它不需要 Word automatio即可运行,并且任何第三方的功能都囊括。
在C#中使用Spire.doc对word的操作总结的更多相关文章
- 【好文翻译】一步一步教你使用Spire.Doc转换Word文档格式
背景: 年11月,微软宣布作为ECMA国际主要合作伙伴,将其开发的基于XML的文件格式标准化,称之为"Office Open XML" .Open XML的引进使office文档结 ...
- Spire.Doc组件读取与写入Word
之前写了一篇开源组件DocX读写word的文章,当时时间比较匆忙选了这个组件,使用过程中还是有些不便,不能提前定义好模版,插入Form表单域进行替换.最近无意中发现Spire.Doc组件功能很强大,目 ...
- [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc
开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...
- 使用Spire.Doc组件利用模板导出Word文档
以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作, ...
- 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc (转)
[原文地址]http://www.cnblogs.com/liqingwen/p/5898368.html 序 本打算过几天简单介绍下组件 Spire.XLS,突然发现园友率先发布了一篇,既然 x ...
- C#使用Spire.Doc Word for .Net读写Word
以前对Excel或Word文档操作都使用微软的COM组件Microsoft Word 15.0 object library. 但是这种方式必须要求服务器上安装Office,而且会出现读写操作完成后未 ...
- 【产品对比】Word开发工具Aspose.Words和Spire.Doc性能和优劣对比一览
转:evget.com/article/2018/4/3/27885.html 概述:Microsoft Office Word是微软公司的一个文字处理器应用程序,作为办公软件必不可少的神器之一,Wo ...
- 使用spire.doc导出支持编辑Latex公式的标准格式word
背景 之前有的教辅标注需求,在导出题库的时候希望顺便导出可以查看word,方便线下预览成品效果,因为只是用来预览并且为了沿用前端的样式,当时方案就是直接生成html,写个word的文件头,这样就可以用 ...
- Word转图片(使用Spire.doc)
Spire.Doc for .NET是一款由E-iceblue公司开发的专业的Word .NET类库.支持.net,WPF,Silverlight, 下载地址:http://www.e-iceblue ...
随机推荐
- 日常关键字:定时关机、该任务映像已损坏或已篡改.(0x80041321)、ChaZD生词同步扇贝
我在床上用chinanet网络慢得简直令人发指,12B/S.是的你没有看错,这是我最常看到的网速.但是我最近发现电脑联网开出一个WiFi,在床上用手机上网时,网速会一点提升,可达到1KB/S(⊙﹏⊙) ...
- ESLint 规则
ESLint由 JavaScript 红宝书 作者 Nicholas C.Zakas 编写, 2013 年发布第一个版本. ESLint是一个以可扩展.每条规则独立的,被设计为完全可配置的lint工具 ...
- java学习中的一些疑惑解答
一.java中的枚举类型: 在实际编程中,往往存在着这样的"数据集",它们的数值在程序中是稳定的,而且"数据集"中的元素是有限的.例如星期一到星期日七个数据元素 ...
- Metaio获取当前追踪的对象的方法
重写 onTrackingEvent获取TrackingValues集合,然后通过TrackingValues的state属性的isTrackingState()方法判断是否为追踪状态,或者直接使用s ...
- PHP的输出缓冲区(转)
什么是缓冲区?简单而言,缓冲区的作用就是,把输入或者输出的内容先放进内存,而不显示或者读取.至于为什么要有缓冲区,这是一个很广泛的问题,如果有兴趣,可以在网山找下资料.其实缓冲区最本质的作用就是,协调 ...
- Delphi自己隐藏自定义弹出列表
先上代码 procedure TForm3.Timer1Timer(Sender: TObject); var Point: TPoint; Name: array[0..255] of Char; ...
- CYQ.Data.Orm.DBFast 新增类介绍(含类的源码及新版本配置工具源码)
前言: 以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网. 也不是全不能上,房间里有三台能上 ...
- ENode框架Conference案例分析系列之 - 上下文划分和领域建模
前面一片文章,我介绍了Conference案例的核心业务,为了方便后面的分析,我这里再列一下: 业务描述 Conference是这样一个系统,它提供了一个在线创建会议以及预订会议座位的平台.这个系统的 ...
- Unicode编码解码在线转换工具
// Unicode编码解码在线转换工具 Unicode 是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standar ...
- Step by Step 创建一个新的Dynamics CRM Organization
原创地址:http://www.cnblogs.com/jfzhu/p/4012833.html 转载请注明出处 前面演示过如何安装Dynamics CRM 2013,参见<Step by st ...