讲解内容能够是对某段文字或内容的注脚,也足以是对文段核心境想的归纳提要,也许是对著作内容的评判、疑问,以及在读书时给和睦或旁人起到升迁成效。本篇小说中校介绍怎么样在C#中操作Word注解,首要包含以下要点:

在前头的小说C#
怎么样插入、修改、删除Word疏解一文中牵线了怎么操作Word讲明的一部分方法,在本篇文章中接二连三介绍操作Word解说的艺术。分以下二种景况来介绍:

  1. 插入图片到Word演说

  2. 怎么着插入图片,给Word文档增加。读取Word批注

  3. 回复Word批注

C#/VB.NET 给Word文书档案增加/裁撤书签,

在今世办公情形中,阅读或许编辑较长篇幅的Word文书档案时,想要在文书档案中某一处或许几处留下标志,方便日后找寻、修改时,供给在相对应的文书档案地方插入书签。那对于开拓者来说,在C#抑或VB.NET语言蒙受中,怎么样来飞速、简便的插入书签呢,小编享受一下本人的经历。这里笔者使用了一款E-iceblue集团公布的1款免费的Word组件(Free
Spire.Doc for .NET),方法相当粗略,如下:

步骤1:起头化Document实例并加载Word文档

Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦.docx ");

手续2:于第8段末和第九段间插入书签,命名书签为“C#.bookmark”

Section section = document.Sections[0];
section.Paragraphs[7].AppendBookmarkStart("C#.bookmark");
section.Paragraphs[8].AppendBookmarkEnd("C#.bookmark ");

步骤叁:保存文件

document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");

成功后上述步骤后,文书档案中寻觅定位就能够,文书档案自动定位到当下所设书签地方。

金沙注册送58 1

如上轻巧七个步骤就能够到位对word文书档案书签插入。

总体代码如下,供参考:

C#

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

namespace WordBookmark
{
    class Bookmark
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦.docx ");

            //Insert Bookmark
            Section section = document.Sections[0];
            section.Paragraphs[7].AppendBookmarkStart(".NETFramework");
section.Paragraphs[8].AppendBookmarkEnd(".NETFramework");

            //Save and Launch 
            document.SaveToFile("Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Bookmark.docx");
        }
    }
}

VB.NET:

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordBookmark

    Class Bookmark

        Private Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As Document = New Document
            document.LoadFromFile("C:\Users\Administrator\Desktop\中国梦.docx ")
            'Insert Bookmark
            Dim section As Section = document.Sections(0)
            section.Paragraphs(7).AppendBookmarkStart(".NETFramework")
            section.Paragraphs(8).AppendBookmarkEnd(".NETFramework")
            'Save and Launch 
            document.SaveToFile("Bookmark.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Bookmark.docx")
        End Sub
    End Class
End Namespace

一近年来后的,撤废书签也能够参见实施笔者下边包车型大巴操作

步骤1:加载须求撤消书签的Word文档

Document doc = new Document();
           doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦(书签).docx");

步骤二:撤消已有书签

doc.Bookmarks.RemoveAt(0);

步骤叁:保存文件

doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Remove Bookmark.docx");

打消书签后,获得以下文书档案效果

金沙注册送58 2

如图,原本插入书签的段落已撤销书签

完整代码如下

C#:

using Spire.Doc;

namespace Removing
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦(书签).docx ");

            //Remove Bookmark
            doc.Bookmarks.RemoveAt(0);

            //Save and Launch
            doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Remove Bookmark.docx");
        }
    }
}

VB.NET:

Imports Spire.Doc

Namespace Removing

    Class Program

        Private Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim doc As Document = New Document
            doc.LoadFromFile("C:\Users\Administrator\Desktop\中国梦(书签).docx ")
            'Remove Bookmark
            doc.Bookmarks.RemoveAt(0)
            'Save and Launch
            doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Remove Bookmark.docx")
        End Sub
    End Class
End Namespace

如上内容是自个儿对word插入及吊销书签的操作演示,希望自身的那个分享对你富有启发,谢谢阅读!

 

给Word文书档案增添/裁撤书签,
在现世办公室条件中,阅读也许编辑较长篇幅的Word文书档案时,想要在文书档案中某一处大概几处留下标识,方便…

在现世办公室情状中,阅读可能编辑较长篇幅的Word文书档案时,想要在文书档案中某壹处或然几处留下标识,方便日后索求、修改时,需求在相对应的文书档案地方插入书签。那对于开拓者来讲,在C#依然VB.NET语言情状中,怎么样来快速、简便的插入书签呢,我享受一下自身的经验。这里本身动用了一款E-iceblue集团发表的1款无需付费的Word组件(Free
Spire.Doc for .NET),方法很轻松,如下:

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

所需工具

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

 PS:下载安装Free Spire.Doc
后,注目的在于你的次序中丰裕引用Spire.Doc.dll(dll文件能够在装置路线下的Bin文件夹中赢得)

金沙注册送58 3

 

步骤壹:伊始化Document实例并加载Word文书档案

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

以身作则代码

Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦.docx ");

注:编写代码前注意增添引用Sprie.Doc.dll(dll文件可在设置路线下的Bin文件夹中获得)

一. 插入图片到Word解说

步骤 1:添加using指令

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

步骤 二:成立文书档案,加载测试文件

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

金沙注册送58,手续 三 :获取段落

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

手续 四 :加多文本、图片到解说

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

手续 五 :保存文件

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

 

测试结果:

金沙注册送58 4

C#一体代码:

金沙注册送58 5金沙注册送58 6

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代码:

金沙注册送58 7金沙注册送58 8

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

 

手续二:于第八段末和第10段间插入书签,命名书签为“C#.bookmark”

金沙注册送58 9

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

步骤 三 :将解说内容写入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");

金沙注册送58 10

C# 全体代码:

金沙注册送58 11金沙注册送58 12

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 代码

金沙注册送58 13金沙注册送58 14

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

 

Section section = document.Sections[0];
section.Paragraphs[7].AppendBookmarkStart("C#.bookmark");
section.Paragraphs[8].AppendBookmarkEnd("C#.bookmark ");

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

 

测试结果:

金沙注册送58 15

3. 过来Word批注内容

步骤 1 :添加using指令 

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

手续 2 :创建实例

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

手续 三 :获取解说

Comment comment = doc.Comments[0];

手续 四 :回复讲解

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

 

测试结果:

金沙注册送58 16

C# 全部代码:

金沙注册送58 17金沙注册送58 18

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 代码:

金沙注册送58 19金沙注册送58 20

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讲解的全体内容。

(本文完)

如需转发,请评释出处。

手续三:保存文件

2.改变、删除注解

测试文书档案:

金沙注册送58 21

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

 

测试结果:

金沙注册送58 22

上述是这次关于操作Word解说的全部内容。感激浏览!

document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");

完结后上述步骤后,文书档案中追寻定位就可以,文书档案自动定位到当前所设书签地点。

金沙注册送58 23

如上简单四个步骤就能够到位对word文书档案书签插入。

整体代码如下,供参考:

C#

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

namespace WordBookmark
{
    class Bookmark
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦.docx ");

            //Insert Bookmark
            Section section = document.Sections[0];
            section.Paragraphs[7].AppendBookmarkStart(".NETFramework");
section.Paragraphs[8].AppendBookmarkEnd(".NETFramework");

            //Save and Launch 
            document.SaveToFile("Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Bookmark.docx");
        }
    }
}

VB.NET:

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordBookmark

    Class Bookmark

        Private Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As Document = New Document
            document.LoadFromFile("C:\Users\Administrator\Desktop\中国梦.docx ")
            'Insert Bookmark
            Dim section As Section = document.Sections(0)
            section.Paragraphs(7).AppendBookmarkStart(".NETFramework")
            section.Paragraphs(8).AppendBookmarkEnd(".NETFramework")
            'Save and Launch 
            document.SaveToFile("Bookmark.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Bookmark.docx")
        End Sub
    End Class
End Namespace

无差异于的,撤废书签也得以参照施行小编下边包车型客车操作

手续1:加载必要打消书签的Word文书档案

Document doc = new Document();
           doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦(书签).docx");

步骤二:撤除已有书签

doc.Bookmarks.RemoveAt(0);

步骤三:保存文件

doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Remove Bookmark.docx");

撤消书签后,获得以下文书档案效果

金沙注册送58 24

如图,原本插入书签的段落已撤销书签

完整代码如下

C#:

using Spire.Doc;

namespace Removing
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中国梦(书签).docx ");

            //Remove Bookmark
            doc.Bookmarks.RemoveAt(0);

            //Save and Launch
            doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Remove Bookmark.docx");
        }
    }
}

VB.NET:

Imports Spire.Doc

Namespace Removing

    Class Program

        Private Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim doc As Document = New Document
            doc.LoadFromFile("C:\Users\Administrator\Desktop\中国梦(书签).docx ")
            'Remove Bookmark
            doc.Bookmarks.RemoveAt(0)
            'Save and Launch
            doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Remove Bookmark.docx")
        End Sub
    End Class
End Namespace

如上内容是本人对word插入及吊销书签的操作演示,希望作者的那些分享对您有所启发,感激阅读!

 

相关文章

网站地图xml地图