C#/VB.NET 操做Word批註(二)——如何插入圖片、讀取、回覆Word批註內容

在前面的文章C# 如何插入、修改、刪除Word批註一文中介紹瞭如何操做Word批註的一些方法,在本篇文章中繼續介紹操做Word批註的方法。分如下三種狀況來介紹:html

1. 插入圖片到Word批註ide

2. 讀取Word批註工具

3. 回覆Word批註測試

所需工具

 PS:下載安裝Free Spire.Doc 後,注意在你的程序中添加引用Spire.Doc.dll(dll文件能夠在安裝路徑下的Bin文件夾中獲取)ui

 

示例代碼

1. 插入圖片到Word批註

步驟 1:添加using指令spa

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

步驟 2:建立文檔,加載測試文件3d

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

步驟 3 :獲取段落code

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

步驟 4 :添加文本、圖片到批註orm

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 :保存文件htm

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

 

測試結果:

C#所有代碼:

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

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

 

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

C# 所有代碼:

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

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

 

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

 

測試結果:

C# 所有代碼:

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

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批註的所有內容。

(本文完)

如需轉載,請註明出處。

相關文章
相關標籤/搜索