微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C#中使用Spire.doc对word的操作总结

                     C#中使用Spire.docword的操作总结

  

在最近的工程中我们要处理一些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(");
Document document = new Document(stream);

 

2、如何创建表格

 

Document document = new Document();
Section section = document.AddSection();
 
Table table = section.AddTable(true);
table.ResetCells(23);
 
TableRow row = table.Rows[0];
row.IsHeader = true;
 
Paragraph para = row.Cells[0].AddParagraph();
TextRange TR = para.AppendText("Item");
 
para = row.Cells[1].AddParagraph();
TR = para.AppendText(Description2].AddParagraph();
TR = para.AppendText(Qty");
 
document.SavetoFile(WordTable.docx");
 
System.Diagnostics.Process.Start(");

 

 

我们还可以设置行高和列宽

 

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",HyperlinkType.WebLink);
 
Insert email address hyperlink
paragraph = section.AddParagraph();
paragraph.AppendText(Contact USmailto:[email protected][email protected]etoFile(Hyperlink.docx");
System.Diagnostics.Process.Start(");

 

 


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(");
comment1.AddItem(textRange);
comment1.Format.Author = Harry Hu";
comment1.Format.Initial = HH";
 
document.SavetoFile(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(Bookmark.docx");

 

 

6、合并邮件

 

Document document = Fax.doc");
 
string[] filednames = new string[] { Contact NameFaxDate" };
 
string[] filedValues = John Smith+1 (69) 123456stem.DateTime.Now.Date.ToString() };
 
document.MailMerge.Execute(filednames, filedValues);
 
document.SavetoFile(MailMerge.docstem.Diagnostics.Process.Start(");
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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 fileusing (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[0].Body.FormFields)
  {
     String path = String.Format({0}/text()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 = 0; 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;
                      }
                  }
                  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.docxnew Document();
DocTwo.LoadFromFile(E:\Work\Document\New Zealand.docxMergeforeach (Section sec in DocTwo.Sections)
{
 DocOne.Sections.Add(sec.Clone());
}
Save and Launch
DocOne.SavetoFile(Merge.docx、保护文档。你可以设置密码或者加入水印来进行保护。文字图片的水印都支持


Protect with password
document.Encrypt(eiceblueAdd Text watermark:
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = Microsoft";
txtWatermark.FontSize = 90;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
 
Add Image watermark:
PictureWatermark picture = new PictureWatermark();
picture.Picture = System.Drawing.Image.FromFile(..\imagess.jpeg");
picture.Scaling = 250;
document.Watermark = picture;

 

10、转换功能是在处理word文档时最常见的操作了。使用免费版的Spire.doc  for .NET 转换变得很简单。只要包含三行类似的代码你就可以把word转换成其他常用格式,像PDF,HTML图片

Word转换成PDF

 

SavetoFile(Target PDF.PDF 

Word转换成图片

 

Image image = document.SavetoImages(0, ImageType.Bitmap);
image.Save(Sample.tiff转换成HTML

 

document.SavetoFile(Target HTML.html""Target HTML.html);

 

结论:

这是一个免费又强大的C# word 组件,它不需要 Word automatio即可运行,并且任何第三方的功能都囊括。

 


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐