插入图片,回复Word批注内容

By admin in 4858.com on 2019年3月28日

在前边的作品C#
怎样插入、修改、删除Word批注一文中介绍了什么样操作Word批注的有的主意,在本篇小说中一而再介绍操作Word批注的办法。分以下三种情形来介绍:

  1. 安顿图片到Word批注

  2. 读取Word批注

  3. 回复Word批注

概述

OLE,Object Linking and
Embedding,即对象连接与嵌入。我们在安排程序时,OLE能够用来成立复合文书档案,把文字、声音、图像、表格、应用程序等项目标信息整合在一道,在Word中,大家能够由此OLE来落到实处上述因素音信的结合。上面包车型客车示范中将介绍如何通过C#
来操作Word中的OLE,示例内容涵盖以下要点:

  • 插入OLE到Word
  • 编辑Word中的OLE
  • 读取Word中的OLE

 

概要

书签的安装能够协助大家快捷的稳定某段文字,使用起来很便利,也很节省时间。在前一篇小说“C#
怎样添加/删除Word书签”中牵线了插入、删除书签的不二法门,本篇小说将对C#
操作Word书签的效应做进一步介绍。示例内容将包涵以下要点:

  • 在存活书签地点插入图片
  • 在存活书签地方插入表格
  • 读取书签地点的文书档案内容
  • 轮换现有书签位置内容

 

批注内容可以是对某段文字或内容的诠释,也得以是对文段中央思想的席卷提要,大概是对小说内容的评判、疑问,以及在阅读时给本人或旁人起到晋升效用。本篇小说大校介绍怎么着在C#中操作Word批注,首要涵盖以下要点:

所需工具

  • Free Spire.Doc for .NET
    6.3(社区版)
  • Visual Studio

 PS:下载安装Free Spire.Doc
后,注意在你的次第中充裕引用Spire.Doc.dll(dll文件能够在设置路径下的Bin文件夹中赢得)

4858.com 1

 

应用工具

  •  Spire.Doc for
    .NET

:下载安装该类库,在编辑代码时,注目的在于先后中丰盛引用Spire.Doc.dll(dll文件可在装置路径下的Bin文件夹中收获)

4858.com 2

应用工具

  • Spire.Doc for .NET
    6.1
  • 插入图片,回复Word批注内容。Visual Studio

ps:下载安装该类库后,注意添加引用Spire.Doc.dl到品种先后,dll文件可在设置路径的Bin文件夹中获取。

4858.com 3

  • 插入Word批注
  • 修改Word批注
  • 删除Word批注

以身作则代码

示范代码(供参考)

以身作则代码(供参考)

测试文书档案如下:

4858.com 4

 

 

行使工具:Free Spire.Doc for .NET
6.3(最新社区版)

1. 布署图片到Word批注

步骤 1:添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

手续 2:创设文书档案,加载测试文件

Document doc = new Document();
doc.LoadFromFile("testfile.docx");

手续 3 :获取段落

Paragraph paragraph = doc.Sections[0].Paragraphs[2];

手续 4 :添Gavin本、图片到批注

Comment comment = paragraph.AppendComment("探索黑科技,小米为发烧而生!");
comment.Format.Author = "Administor";  

DocPicture docPicture = new DocPicture(doc);
Image img = Image.FromFile("mi.png");
docPicture.LoadImage(img);
//插入图片到批注
comment.Body.AddParagraph().ChildObjects.Add(docPicture);

手续 5 :保存文件

doc.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");

 

测试结果:

4858.com 5

C#漫天代码:

4858.com 64858.com 7

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertImgToComment_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document类,加载文档
            Document doc = new Document();
            doc.LoadFromFile("testfile.docx");

            //获取需要添加批注的段落
            Paragraph paragraph = doc.Sections[0].Paragraphs[2];

            //添加文本批注内容、批注作者
            Comment comment = paragraph.AppendComment("探索黑科技,小米为发烧而生!");
            comment.Format.Author = "Administor";

            //实例化DocPicture类,加载图片
            DocPicture docPicture = new DocPicture(doc);
            Image img = Image.FromFile("mi.png");
            docPicture.LoadImage(img);
            //插入图片到批注
            comment.Body.AddParagraph().ChildObjects.Add(docPicture);

            //保存文件并打开文档
            doc.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

View Code

VB.NET代码:

4858.com 84858.com 9

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing

Namespace InsertImgToComment_Doc
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim doc As Document = New Document()
            doc.LoadFromFile("testfile.docx")
            Dim paragraph As Paragraph = doc.Sections(0).Paragraphs(2)
            Dim comment As Comment = paragraph.AppendComment("探索黑科技,小米为发烧而生!")
            comment.Format.Author = "Administor"
            Dim docPicture As DocPicture = New DocPicture(doc)
            Dim img As Image = Image.FromFile("mi.png")
            docPicture.LoadImage(img)
            comment.Body.AddParagraph().ChildObjects.Add(docPicture)
            doc.SaveToFile("result.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("result.docx")
        End Sub
    End Class
End Namespace

View Code

 

【示例1】插入OLE到Word

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

步骤 2 :创造文书档案

//实例化一个Document类对象
Document doc = new Document();
//向文档中添加一个Section对象,并添加段落
Section sec = doc.AddSection();
Paragraph p = sec.AddParagraph();

手续 3 :加载图片

//实例化一个DocPicture类对象,加载图片
DocPicture picture = new DocPicture(doc);
Image image = Image.FromFile(@"chart1.png");
picture.LoadImage(image);

步骤 4 :插入OLE

//在文档中插入一个工作表, OleLinkType 枚举值控制该OLE是链接还是嵌入
DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Link);
//DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Embed);

手续 5 :保存文书档案

//保存并打开文档
doc.SaveToFile("添加OLE.docx");
System.Diagnostics.Process.Start("添加OLE.docx");

姣好代码后,调试运营程序,生成文书档案。

测试结果,如下图所示:

4858.com 10

全副代码:

4858.com 114858.com 12

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertOLE_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document类对象
            Document doc = new Document();
            //向文档中添加一个Section对象,并添加段落
            Section sec = doc.AddSection();
            Paragraph p = sec.AddParagraph();

            //实例化一个DocPicture类对象,加载图片
            DocPicture picture = new DocPicture(doc);
            Image image = Image.FromFile(@"chart1.png");
            picture.LoadImage(image);

            //在文档中插入一个工作表, OleLinkType 枚举值控制该OLE是链接还是嵌入         
            DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Link);
            //DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Embed);

            //保存并打开文档
            doc.SaveToFile("添加OLE.docx");
            System.Diagnostics.Process.Start("添加OLE.docx");
        }
    }
}

View Code

 

 

【示例1】 在现有书签地方插入图片

步骤 1: 添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

手续 2 :主要代码段

//创建实例,加载文档
Document document = new Document();
document.LoadFromFile("test.docx");

//实例化BookmarksNavigator类,指定需要添加图片的书签“Bookmark1”
BookmarksNavigator bn = new BookmarksNavigator(document);
bn.MoveToBookmark("Bookmark1", true, true);

//添加段落,加载图片并插入到段落
Section section0 = document.AddSection();
Paragraph paragraph = section0.AddParagraph();
Image image = Image.FromFile("img.png");
DocPicture picture = paragraph.AppendPicture(image);
bn.InsertParagraph(paragraph);
document.Sections.Remove(section0);

//保存文档并打开
string output = "result.docx";
document.SaveToFile(output, FileFormat.Docx);
System.Diagnostics.Process.Start(output);

图形插入效果:

 4858.com 13

漫天代码:

4858.com 144858.com 15

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertImgToBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建实例,加载文档
            Document document = new Document();
            document.LoadFromFile("test.docx");

            //实例化BookmarksNavigator类,指定需要添加图片的书签“Bookmark1”
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("Bookmark1", true, true);

            //添加段落,加载图片并插入到段落
            Section section0 = document.AddSection();
            Paragraph paragraph = section0.AddParagraph();
            Image image = Image.FromFile("img.png");
            DocPicture picture = paragraph.AppendPicture(image);
            bn.InsertParagraph(paragraph);
            document.Sections.Remove(section0);

            //保存文档并打开
            string output = "result.docx";
            document.SaveToFile(output, FileFormat.Docx);
            System.Diagnostics.Process.Start(output);
        }
    }
}

View Code

 

注:编写制定代码前注意添加引用Sprie.Doc.dll(dll文件可在安装路径下的Bin文件夹中获得)

2.读取Word批注

步骤 1 :添加using指令

using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

步骤 2 :创设实例,加载文书档案

Document doc = new Document();
doc.LoadFromFile("test.docx");

步骤 3 :将批注内容写入Txt文书档案

//实例化StringBuilder类
StringBuilder SB = new StringBuilder();
//遍历所有word批注,将批注内容写入Txt文档
foreach (Comment comment in doc.Comments)
{
    foreach (Paragraph p in comment.Body.Paragraphs)
    {
        SB.AppendLine(p.Text);
    }
}
File.WriteAllText("CommentExtraction.txt", SB.ToString());
System.Diagnostics.Process.Start("CommentExtraction.txt");

4858.com 16

C# 全体代码:

4858.com 174858.com 18

using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace ExtractComments
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建实例,加载文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //实例化StringBuilder类
            StringBuilder SB = new StringBuilder();
            //遍历所有word批注,将批注内容写入Txt文档
            foreach (Comment comment in doc.Comments)
            {
                foreach (Paragraph p in comment.Body.Paragraphs)
                {
                    SB.AppendLine(p.Text);
                }
            }
            File.WriteAllText("CommentExtraction.txt", SB.ToString());
            System.Diagnostics.Process.Start("CommentExtraction.txt");
        }
    }
}

View Code

VB.NET 代码

4858.com 194858.com 20

Imports System.Text
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace ExtractComments
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim doc As Document = New Document()
            doc.LoadFromFile("test.docx")
            Dim SB As StringBuilder = New StringBuilder()

            For Each comment As Comment In doc.Comments

                For Each p As Paragraph In comment.Body.Paragraphs
                    SB.AppendLine(p.Text)
                Next
            Next

            File.WriteAllText("CommentExtraction.txt", SB.ToString())
            System.Diagnostics.Process.Start("CommentExtraction.txt")
        End Sub
    End Class
End Namespace

View Code

 

【示例2】编辑Word中的OLE

 (那里以上文中生成的文书档案为测试文件)

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.IO;

手续 2 :加载文书档案

//实例化一个Document对象,加载含有OLE的Word文档
Document doc = new Document();
doc.LoadFromFile("test.docx");

步骤 3 :获取section中的全体OLE,依据要求来更改OLE的花色和链接对象

//获取第一个Section
Section sec = doc.Sections[0];

//遍历这个Section中的所有子元素,找到段落下的OLE对象
foreach (DocumentObject obj in sec.Body.ChildObjects)
{
    if (obj is Paragraph)
    {
        Paragraph par = obj as Paragraph;
        foreach (DocumentObject paraObj in par.ChildObjects)
        {
            //找到OLE对象,根据类型来进行更改操作
            if (paraObj.DocumentObjectType == DocumentObjectType.OleObject)
            {
                DocOleObject Ole = paraObj as DocOleObject;
                //如果是链接, 修改对象的链接路径
                if (Ole.LinkType == OleLinkType.Link)
                {
                    //同时还要手动去更改OLE的图片
                    DocPicture pic = Ole.OlePicture;
                    pic.LoadImage(Image.FromFile("Img.png"));
                    Ole.LinkPath = @"sample.docx";
                }
                //如果是嵌入,更改数据即可
                byte[] bys = File.ReadAllBytes(@"sample.docx");
                if (Ole.LinkType == OleLinkType.Embed)
                {
                    DocPicture pic = new DocPicture(doc);
                    pic.LoadImage(Image.FromFile(@"Img.png"));
                    Ole.ObjectType = "Word.Document.12";
                    Ole.SetOlePicture(pic);
                    Ole.SetNativeData(bys);
                }
            }
        }
    }
}

手续 4 :保存文书档案

 //保存修改后的文档,并打开
 doc.SaveToFile("修改OLE.docx", Spire.Doc.FileFormat.Docx2010);
 System.Diagnostics.Process.Start("修改OLE.docx");

调剂运维程序后,生成文书档案。打开文书档案时,生成的文书档案中原来的插入OLE的图片和链接的文档都早已转移了,如下所示:

 4858.com 21

全副代码:

4858.com 224858.com 23

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.IO;

namespace EditOLE_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document对象,加载含有OLE的Word文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //获取第一个Section
            Section sec = doc.Sections[0];

            //遍历这个Section中的所有子元素,找到段落下的OLE对象
            foreach (DocumentObject obj in sec.Body.ChildObjects)
            {
                if (obj is Paragraph)
                {
                    Paragraph par = obj as Paragraph;
                    foreach (DocumentObject paraObj in par.ChildObjects)
                    {
                        //找到OLE对象,根据类型来进行更改操作
                        if (paraObj.DocumentObjectType == DocumentObjectType.OleObject)
                        {
                            DocOleObject Ole = paraObj as DocOleObject;
                            //如果是链接, 修改对象的链接路径
                            if (Ole.LinkType == OleLinkType.Link)
                            {
                                //同时还要手动去更改OLE的图片
                                DocPicture pic = Ole.OlePicture;
                                pic.LoadImage(Image.FromFile("Img.png"));
                                Ole.LinkPath = @"sample.docx";
                            }
                            //如果是嵌入,更改数据即可
                            byte[] bys = File.ReadAllBytes(@"sample.docx");
                            if (Ole.LinkType == OleLinkType.Embed)
                            {
                                DocPicture pic = new DocPicture(doc);
                                pic.LoadImage(Image.FromFile(@"Img.png"));
                                Ole.ObjectType = "Word.Document.12";
                                Ole.SetOlePicture(pic);
                                Ole.SetNativeData(bys);
                            }
                        }
                    }
                }
            }
            //保存修改后的文档,并打开
            doc.SaveToFile("修改OLE.docx", Spire.Doc.FileFormat.Docx2010);
            System.Diagnostics.Process.Start("修改OLE.docx");
        }
    }
}

View Code

 【示例 2】在现有书签地点插入表格

步骤 1:添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using System.Data;

手续 2 :创制文书档案,加载文件

Document doc = new Document();
doc.LoadFromFile("test.docx");

手续 3 :创制表格

//创建Table对象
Table table = new Table(doc, true);

//创建模拟数据
DataTable dt = new DataTable();
dt.Columns.Add("PI", typeof(string));
dt.Columns.Add("Type", typeof(string));
dt.Columns.Add("Nov.", typeof(string));
dt.Columns.Add("YTD", typeof(string));                     
dt.Rows.Add(new string[] { "PI","Type", "Nov.", "YTD" });
dt.Rows.Add(new string[] { "1", "BAIC EC-Series", "15.719", "64.910" });
dt.Rows.Add(new string[] { "2", "Tesla Model S", "3.889", "46.951" });
dt.Rows.Add(new string[] { "3", "Nissan Leaf", "2.817", "46.423" });

//将数据填充至表格
table.ResetCells(dt.Rows.Count, dt.Columns.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
    }
}

手续 4 :插入表格到书签地方

//获取指定书签位置
BookmarksNavigator navigator = new BookmarksNavigator(doc);
navigator.MoveToBookmark("origin2");

//将表格添加至TextBodyPart
TextBodyPart part = navigator.GetBookmarkContent();
part.BodyItems.Add(table);
//替换原有书签内容
navigator.ReplaceBookmarkContent(part);

步骤 5 :保存文件

//保存并打开文件
doc.SaveToFile("output.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("output.docx");

 

 表格插入效果:

4858.com 24

全数代码:

4858.com 254858.com 26

using Spire.Doc;
using Spire.Doc.Documents;
using System.Data;

namespace InsertTableToBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建文档,加载文件
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //创建Table对象
            Table table = new Table(doc, true);

            //创建模拟数据
            DataTable dt = new DataTable();
            dt.Columns.Add("PI", typeof(string));
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("Nov.", typeof(string));
            dt.Columns.Add("YTD", typeof(string));                     
            dt.Rows.Add(new string[] { "PI","Type", "Nov.", "YTD" });
            dt.Rows.Add(new string[] { "1", "BAIC EC-Series", "15.719", "64.910" });
            dt.Rows.Add(new string[] { "2", "Tesla Model S", "3.889", "46.951" });
            dt.Rows.Add(new string[] { "3", "Nissan Leaf", "2.817", "46.423" });

            //将数据填充至表格
            table.ResetCells(dt.Rows.Count, dt.Columns.Count);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
                }
            }

            //获取指定书签位置
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark("origin2");

            //将表格添加至TextBodyPart
            TextBodyPart part = navigator.GetBookmarkContent();
            part.BodyItems.Add(table);

            //替换书签内容
            navigator.ReplaceBookmarkContent(part);

            //保存并打开文件
            doc.SaveToFile("output.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("output.docx");
        }
    }
}

View Code

 

4858.com 27

3. 回复Word批注内容

步骤 1 :添加using指令 

using Spire.Doc;
using Spire.Doc.Fields;

步骤 2 :创制实例

Document doc = new Document();
doc.LoadFromFile("test.docx");

步骤 3 :获取批注

Comment comment = doc.Comments[0];

步骤 4 :回复批注

Comment replyComment = new Comment(doc);
replyComment.Format.Author = "Adam";
replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调");
comment.ReplyToComment(replyComment);

步骤 5 :保存文件

doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("ReplyToComment.docx");

 

测试结果:

4858.com 28

C#4858.com , 全体代码:

4858.com 294858.com 30

using Spire.Doc;
using Spire.Doc.Fields;

namespace ReplyComment_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document类,加载文件
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //获取第一个批注
            Comment comment = doc.Comments[0];

            //实例化Comment类,添加批注回复作者以及回复内容
            Comment replyComment = new Comment(doc);
            replyComment.Format.Author = "Adam";
            replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调");
            comment.ReplyToComment(replyComment);

            //保存文件并打开
            doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("ReplyToComment.docx");
        }
    }
}

View Code

VB.NET 代码:

4858.com 314858.com 32

Imports Spire.Doc
Imports Spire.Doc.Fields

Namespace ReplyComment_Doc
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim doc As Document = New Document()
            doc.LoadFromFile("test.docx")
            Dim comment As Comment = doc.Comments(0)
            Dim replyComment As Comment = New Comment(doc)
            replyComment.Format.Author = "Adam"
            replyComment.Body.AddParagraph().AppendText("这条批注内容请再丰富一下,内容有些单调")
            comment.ReplyToComment(replyComment)
            doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("ReplyToComment.docx")
        End Sub
    End Class
End Namespace

View Code

如上为这一次关于操作Word批注的全体内容。

(本文完)

如需转发,请注脚出处。

【示例3】读取Word中的OLE

 步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;

手续 2 :加载文件

//实例化一个Document对象,加载一个有OLE对象的文档
Document doc = new Document();          
doc.LoadFromFile(@"test.docx");

步骤 3 :遍历section,读取OLE

//遍历文档所有Section          
            foreach (Section sec in doc.Sections)
            {
                //遍历Section下面所有的子元素
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph par = obj as Paragraph;
                        //遍历这个section下面的段落
                        foreach (DocumentObject o in par.ChildObjects)
                        {
                            //找到OLE对象,并根据类型提取
                            if (o.DocumentObjectType == DocumentObjectType.OleObject)
                            {
                                DocOleObject Ole = o as DocOleObject;
                                /*ObjectType属性可以获取ole对象的具体类型。
                                注意,如果是用Spire.Doc添加的ole对象的话,需要在AppendOleObject
                                的时候先声明OleObjectType,不然这里得不到具体的类型,只会得到Package*/
                                string s = Ole.ObjectType;
                                //"AcroExch.Document.11"是指PDF对象对应的ProgID
                                if (s == "AcroExch.Document.11")
                                {
                                    File.WriteAllBytes("Result.pdf", Ole.NativeData);
                                }
                                //"Excel.Sheet.12"是指 Excel03之后的工作表对应的ProgID
                                else if (s == "Excel.Sheet.12")
                                {
                                    File.WriteAllBytes("Result.xlsx", Ole.NativeData);
                                }
                                //"Word.Document.12"是指03之后的Word对应的ProgID
                                else if (s == "Word.Document.12")
                                {
                                    File.WriteAllBytes("Result.docx", Ole.NativeData);
                                }
                            }
                        }
                    }
                }
            }

调节运维程序,生成文书档案,如下:

 4858.com 33

整套代码:

4858.com 344858.com 35

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;

namespace ReadOLE_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document对象,加载一个有OLE对象的文档
            Document doc = new Document();          
            doc.LoadFromFile(@"test.docx");

            //遍历文档所有Section          
            foreach (Section sec in doc.Sections)
            {
                //遍历Section下面所有的子元素
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph par = obj as Paragraph;
                        //遍历这个section下面的段落
                        foreach (DocumentObject o in par.ChildObjects)
                        {
                            //找到OLE对象,并根据类型提取
                            if (o.DocumentObjectType == DocumentObjectType.OleObject)
                            {
                                DocOleObject Ole = o as DocOleObject;
                                /*ObjectType属性可以获取ole对象的具体类型。
                                注意,如果是用Spire.Doc添加的ole对象的话,需要在AppendOleObject
                                的时候先声明OleObjectType,不然这里得不到具体的类型,只会得到Package*/
                                string s = Ole.ObjectType;
                                //"AcroExch.Document.11"是指PDF对象对应的ProgID
                                if (s == "AcroExch.Document.11")
                                {
                                    File.WriteAllBytes("Result.pdf", Ole.NativeData);
                                }
                                //"Excel.Sheet.12"是指 Excel03之后的工作表对应的ProgID
                                else if (s == "Excel.Sheet.12")
                                {
                                    File.WriteAllBytes("Result.xlsx", Ole.NativeData);
                                }
                                //"Word.Document.12"是指03之后的Word对应的ProgID
                                else if (s == "Word.Document.12")
                                {
                                    File.WriteAllBytes("Result.docx", Ole.NativeData);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

View Code

上述是本次关于C# 操作word中的OLE的全体内容。示例方法供参考。

 

本文完。

如需转发,请注解出处!

【示例3】读取书签地点的内容

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

步骤 2 :获取书签地方的始末

//初始化BookmarkNavigator类对象
BookmarksNavigator navigator = new BookmarksNavigator(doc);
//定位到指定书签位置,获取书签位置的文档内容
navigator.MoveToBookmark("bookmark1");
TextBodyPart textBodyPart = navigator.GetBookmarkContent();

步骤 3 :读取书签内容

//遍历书签内容中的子项目,并将文本信息提取至string类型变量中
string text = null;
foreach (var item in textBodyPart.BodyItems)
{
    if (item is Paragraph)
    {
        foreach (var childObject in (item as Paragraph).ChildObjects)
        {
            if (childObject is TextRange)
            {
                text += (childObject as TextRange).Text;
            }
        }
    }
}
//控制台输出文本
Console.WriteLine(text);
Console.ReadLine();

 

 读取结果:

4858.com 36

整整代码:

4858.com 374858.com 38

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

namespace GetTextOfBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document类,加载测试文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //初始化BookmarkNavigator类对象
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            //定位到指定书签位置,获取书签位置的文档内容
            navigator.MoveToBookmark("bookmark1");
            TextBodyPart textBodyPart = navigator.GetBookmarkContent();

            //遍历书签内容中的子项目,并将文本信息提取至string类型变量中
            string text = null;
            foreach (var item in textBodyPart.BodyItems)
            {
                if (item is Paragraph)
                {
                    foreach (var childObject in (item as Paragraph).ChildObjects)
                    {
                        if (childObject is TextRange)
                        {
                            text += (childObject as TextRange).Text;
                        }
                    }
                }
            }
            //控制台输出文本
            Console.WriteLine(text);
            Console.ReadLine();
        }
    }
}

View Code

 

1.插入Word批注

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace InsertComment_Word
{
    class Program
    {
        static void Main(string[] args)
        { 
            //实例化一个Document类对象,并加载Word文档
            Document document = new Document();
            document.LoadFromFile("sample.docx");

            //获取第一段第一节
            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[0];

            //添加文本到批注
            string str = "This paragraph describes the origin and the purpose of WEF";
            Comment comment = paragraph.AppendComment(str);
            //添加批注作者
            comment.Format.Author = "E-iceblue";

            //保存并打开文档
            document.SaveToFile("Comments.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("Comments.docx");
        }
    }
}

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace InsertComment_Word

    Class Program

        Private Shared Sub Main(ByVal args() As String)
            '实例化一个Document类对象,并加载Word文档
            Dim document As Document = New Document
            document.LoadFromFile("sample.docx")
            '获取第一段第一节
            Dim section As Section = document.Sections(0)
            Dim paragraph As Paragraph = section.Paragraphs(0)
            '添加文本到批注
            Dim str As String = "This paragraph describes the origin and the purpose of WEF"
            Dim comment As Comment = paragraph.AppendComment(str)
            '添加批注作者
            comment.Format.Author = "E-iceblue"
            '保存并打开文档
            document.SaveToFile("Comments.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("Comments.docx")
        End Sub
    End Class
End Namespace

 

测试结果:

4858.com 39

【示例4】替换现有书签内容

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

步骤 2 :加载文档

Document document = new Document();
document.LoadFromFile("test.docx");

步骤 3 :添加段落及文字

Section sec = document.AddSection();//添加section
sec.AddParagraph().AppendText("Welcome Back, \n My Friend!"); //添加段落到section,并添加字符串内容

步骤 4 :获取段落

//获取段落内容 
ParagraphBase firstReplacementParagraph = sec.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase lastReplacementParagraph = sec.Paragraphs[sec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
//实例化类TextBodySelection和TextBodyPart
TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);
TextBodyPart part = new TextBodyPart(selection);

步骤 5 :替换书签内容

BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);//实例化BookmarksNavigator类 
bookmarkNavigator.MoveToBookmark("bookmark1", true, true);//定位到书签“bookmark1”所在段落的位置
bookmarkNavigator.DeleteBookmarkContent(true);//删除原有书签位置的内容
bookmarkNavigator.ReplaceBookmarkContent(part, true, true);//用新添加段落的内容替换掉原书签的内容并保留格式
document.Sections.Remove(sec); //移除section

步骤 6 :保存文书档案

//保存文档并打开
document.SaveToFile("替换书签.docx");
System.Diagnostics.Process.Start("替换书签.docx");

 

轮换效果:

4858.com 40

一切代码:

4858.com 414858.com 42

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace EditOrReplaceBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document类实例,加载文档 
            Document document = new Document();
            document.LoadFromFile("test.docx");

            Section sec = document.AddSection();//添加section
            sec.AddParagraph().AppendText("Welcome Back, \n My Friend!"); //添加段落到section,并添加字符串内容

            //获取段落内容 
            ParagraphBase firstReplacementParagraph = sec.Paragraphs[0].Items.FirstItem as ParagraphBase;
            ParagraphBase lastReplacementParagraph = sec.Paragraphs[sec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
            //实例化类TextBodySelection和TextBodyPart
            TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);
            TextBodyPart part = new TextBodyPart(selection);

            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);//实例化BookmarksNavigator类 
            bookmarkNavigator.MoveToBookmark("bookmark1", true, true);//定位到书签“bookmark1”所在段落的位置
            bookmarkNavigator.DeleteBookmarkContent(true);//删除原有书签位置的内容
            bookmarkNavigator.ReplaceBookmarkContent(part, true, true);//用新添加段落的内容替换掉原书签的内容并保留格式            
            document.Sections.Remove(sec); //移除section

            //保存文档并打开
            document.SaveToFile("替换书签.docx");
            System.Diagnostics.Process.Start("替换书签.docx");
        }
    }
}

View Code

 

以上是此次关于C# 操作Word书签作用的补给介绍,如需转发,请申明出处。

(本文完)

 

2.改动、删除批注

测试文书档案:

4858.com 43

C#

using Spire.Doc;

namespace ReplaceAndRemoveComment_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Document类实例,加载带有批注的Word文档
            Document document = new Document();
            document.LoadFromFile("test.docx");

            //修改第一个批注内容
            document.Comments[0].Body.Paragraphs[0].Replace("This paragraph describes the origin and the purpose of WEF", "What is the WEF ?", false, false);

            //移除第二个批注
            document.Comments.RemoveAt(1);

            //保存并打开文档
            document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("RemoveAndReplace.docx");
        }
    }
}

 

VB.NET

Imports Spire.Doc

Namespace ReplaceAndRemoveComment_Word

    Class Program

        Private Shared Sub Main(ByVal args() As String)
            '初始化Document类实例,加载带有批注的Word文档
            Dim document As Document = New Document
            document.LoadFromFile("test.docx")
            '修改第一个批注内容
            document.Comments(0).Body.Paragraphs(0).Replace("This paragraph describes the origin and the purpose of WEF", "What is the WEF ?", false, false)
            '移除第二个批注
            document.Comments.RemoveAt(1)
            '保存并打开文档
            document.SaveToFile("RemoveAndReplace.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("RemoveAndReplace.docx")
        End Sub
    End Class
End Namespace

 

测试结果:

4858.com 44

如上是这一次关于操作Word批注的全体内容。谢谢浏览!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有