服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#中使用Spire.doc对word的操作方式

C#中使用Spire.doc对word的操作方式

2023-03-02 15:18Eiceblue C#

这篇文章主要介绍了C#中使用Spire.doc对word的操作方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用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(2, 3);
 
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("Description");
 
para = row.Cells[2].AddParagraph();
TR = para.AppendText("Qty");
 
document.SaveToFile("WordTable.docx");
 
System.Diagnostics.Process.Start("WordTable.docx");

C#中使用Spire.doc对word的操作方式

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

3、如何插入超链接?你可以插入两种超链接,Email 链接和webmail 链接。

Document document = new Document();
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");

C#中使用Spire.doc对word的操作方式

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");

C#中使用Spire.doc对word的操作方式

5、如何加入书签

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");

C#中使用Spire.doc对word的操作方式

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");

C#中使用Spire.doc对word的操作方式

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);

C#中使用Spire.doc对word的操作方式

填入表单域

//Fill data from XML file
using (Stream stream = File.OpenRead(@"......DataUser.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()", 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 = 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;
                      }
                  }
                  break;
 
             case FieldType.FieldFormCheckBox:
                  if (Convert.ToBoolean(propertyNode.Value))
                  {
                      CheckBoxFormField checkBox = field as CheckBoxFormField;
                      checkBox.Checked = true;
                  }
                  break;
            }
       }
   }
 }

C#中使用Spire.doc对word的操作方式

8、合并word文档

//Load two documents
//Load Document1 and Document2
Document DocOne = new Document();
DocOne.LoadFromFile(@"E:WorkDocumentwelcome.docx", FileFormat.Docx);
Document DocTwo = new Document();
DocTwo.LoadFromFile(@"E:WorkDocumentNew 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 = 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

document.SaveToFile("Target PDF.PDF", FileFormat.PDF);

Word转换成图片

Image image = document.SaveToImages(0, 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即可运行,并且任何第三方的功能都囊括。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文地址:https://blog.csdn.net/Eiceblue/article/details/43668671

延伸 · 阅读

精彩推荐
  • C#C#用NPOI导出导入Excel帮助类

    C#用NPOI导出导入Excel帮助类

    这篇文章主要为大家详细介绍了C# NPOI导出导入Excel帮助类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    lbx_1588705507311292023-02-09
  • C#C#实现 Server-sent Events的步骤

    C#实现 Server-sent Events的步骤

    这篇文章主要介绍了C#实现 Server-sent Events的步骤,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    yswenli12272022-10-26
  • C#WPF TextBox水印效果制作方法详解

    WPF TextBox水印效果制作方法详解

    这篇文章主要为大家详细介绍了WPF TextBox水印效果的制作方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    assassinx8792022-01-22
  • C#C#抽象类与抽象方法详解

    C#抽象类与抽象方法详解

    这篇文章主要为大家详细介绍了C#抽象类与抽象方法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    在代码的世界里游走4752022-03-02
  • C#区分WCF与WebService的异同、优势

    区分WCF与WebService的异同、优势

    这篇文章主要帮助大家区分WCF与WebService的异同、优势,分为三大方面进行研究学习,感兴趣的小伙伴们可以参考一下...

    丶一二丶8382021-11-15
  • C#C#利用GDI绘制常见图形和文字

    C#利用GDI绘制常见图形和文字

    本文主要介绍了C#中利用GDI来绘制图形和文字的方法,并提供的简单的示例供大家参考学习,希望能够对大家有所帮助。...

    Kimisme4232021-11-17
  • C#关于C#继承的简单应用代码分析

    关于C#继承的简单应用代码分析

    在本篇文章里小编给大家整理了一篇关于C#继承的简单应用代码分析内容,有兴趣的朋友们可以学习下。...

    只吃肉不喝酒6402022-11-20
  • C#C# 中使用隐式和显式操作符的示例

    C# 中使用隐式和显式操作符的示例

    这篇文章主要介绍了C# 中使用隐式和显式操作符的示例,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...

    码农读书8542022-11-09