广告位联系
返回顶部
分享到

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

C#教程 来源:互联网 作者:佚名 发布时间:2023-10-12 22:40:56 人浏览
摘要

使用Spire.doc对word的操作 在最近的工程中我们要处理一些word文档。通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复。极少数可以完全实现的word组件又要收费。 功

使用Spire.doc对word的操作

在最近的工程中我们要处理一些word文档。通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复。极少数可以完全实现的word组件又要收费。

功夫不负有心人,我们找到了可以满足我们需要的免费的C# word程序库。为了和其他的作比较,我在这里先做以下汇总。希望对大家有帮助。

如何得到?

这个免费版的word 组件可以在Codeplex下载到,你也可以从本文里直接下载msi文件。它还提供了一些源代码。

Word操作汇总

1、打开已有word文件,这是要处理word文件的最基本也是必须的步骤。它提供了三种方法。

方法1:从已知特定的文档中初始化一个新的Document 类的实例

1

Document document = new Document(@"..\..\Sample.docx");

方法2、从文件中加载一个word文件

1

2

Document document = new Document();

document.LoadFromFile(@"..\..\Sample.docx");

方法3、从流文件中加载word文件

1

2

Stream stream = File.OpenRead(@"..\..\Sample.docx");

Document document = new Document(stream);

2、如何创建表格 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

4、如何加入注解

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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、如何加入书签

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

6、合并邮件

1

2

3

4

5

6

7

8

9

10

11

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、加入表单,这部分包含创建以及填入表单域。

创建表单

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

填入表单域

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

//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[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;

            }

       }

   }

 }

8、合并word文档

1

2

3

4

5

6

7

8

9

10

11

12

13

14

//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、保护文档。你可以设置密码或者加入水印来进行保护。文字和图片的水印都支持。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

//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

1

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

Word转换成图片

1

2

Image image = document.SaveToImages(0, ImageType.Bitmap);

image.Save("Sample.tiff", ImageFormat.Tiff);

word转换成HTML

1

2

document.SaveToFile("Target HTML.html", FileFormat.Html);

WordDocViewer(""Target HTML.html");


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • C#中的时间显示格式(12小时制VS24小时制)介绍
    C#时间显示格式 一起看下: 24小时制 1 this.toolStripStatusLabel1.Text = 您好,欢迎来到XXXX控制系统! + 当前时间: + DateTime.Now.ToString(yyyy-MM-dd HH
  • C#中使用Spire.doc对word的操作方式介绍

    C#中使用Spire.doc对word的操作方式介绍
    使用Spire.doc对word的操作 在最近的工程中我们要处理一些word文档。通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复。
  • C#纯技术之Class写入Json介绍

    C#纯技术之Class写入Json介绍
    C# Class写入Json 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 /// summary /// 写入json文件 /// /summary /// param name=obj/param /// param name=save
  • C#实体类转换的两种方式介绍
    C#实体类转换方式 将一个实体类的数据赋值到另一个实体类中(亦或者实现深拷贝)。 以下提供两种方式 一种是序列化 一种是泛型+反射
  • 在C#中构造自定义属性的详细介绍
    属性用于向程序添加元数据,例如编译器指令和其他信息,例如注释、描述、方法和类。 .Net Framework 允许创建可用于存储声明性信息并可在
  • 在C#中获取路径内的所有目录和子目录的教程
    要获取目录,C#提供了一个方法Directory.GetDirectories。Directory.GetDirectories方法返回与指定搜索模式在指定目录中匹配的子目录的名称(包括其路
  • C#实现窗体换肤的教程
    效果 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  • 【译】.NET 7 中的性能改进(二)
    原文 | Stephen Toub翻译 | 郑子铭堆栈替换 (On-Stack Replacement)堆栈替换 (OSR) 是 .NET 7 中最酷的 JIT 功能之一。但要真正了解 OSR,我们首先需要了解
  • C#将Excel转换为PDF的教程

    C#将Excel转换为PDF的教程
    转换场景 将Excel转换为PDF是一个很常用的功能 常见的转换场景有以下三种: 转换整个Excel文档到PDF 转换Excel文档的某一个工作表到PDF 转换
  • C#中的时间显示格式(12小时制VS24小时制)
    C#时间显示格式 一起看下: 24小时制 1 this.toolStripStatusLabel1.Text = 您好,欢迎来到XXXX控制系统! + 当前时间: + DateTime.Now.ToString(yyyy-MM-dd HH
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计