您好,登錄后才能下訂單哦!
在前面的文章C# 如何插入、修改、刪除Word批注一文中介紹了如何操作Word批注的一些方法,在本篇文章中將繼續(xù)介紹操作Word批注的方法。分以下三種情況來介紹:
【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)
{
//實(shí)例化Document類,加載文檔
Document doc = new Document();
doc.LoadFromFile("testfile.docx");
//獲取需要添加批注的段落
Paragraph paragraph = doc.Sections[0].Paragraphs[2];
//添加文本批注內(nèi)容、批注作者
Comment comment = paragraph.AppendComment("探索黑科技,小米為發(fā)燒而生!");
comment.Format.Author = "Administor";
//實(shí)例化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");
}
}
}
【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("探索黑科技,小米為發(fā)燒而生!")
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
測(cè)試下效果:
【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)
{
//創(chuàng)建實(shí)例,加載文檔
Document doc = new Document();
doc.LoadFromFile("test.docx");
//實(shí)例化StringBuilder類
StringBuilder SB = new StringBuilder();
//遍歷所有word批注,將批注內(nèi)容寫入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");
}
}
}
【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
讀取結(jié)果:
【C#】
using Spire.Doc;
using Spire.Doc.Fields;
namespace ReplyComment_Doc
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化Document類,加載文件
Document doc = new Document();
doc.LoadFromFile("test.docx");
//獲取第一個(gè)批注
Comment comment = doc.Comments[0];
//實(shí)例化Comment類,添加批注回復(fù)作者以及回復(fù)內(nèi)容
Comment replyComment = new Comment(doc);
replyComment.Format.Author = "Adam";
replyComment.Body.AddParagraph().AppendText("這條批注內(nèi)容請(qǐng)?jiān)儇S富一下,內(nèi)容有些單調(diào)");
comment.ReplyToComment(replyComment);
//保存文件并打開
doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("ReplyToComment.docx");
}
}
}
【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("這條批注內(nèi)容請(qǐng)?jiān)儇S富一下,內(nèi)容有些單調(diào)")
comment.ReplyToComment(replyComment)
doc.SaveToFile("ReplyToComment.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("ReplyToComment.docx")
End Sub
End Class
End Namespace
測(cè)試結(jié)果:
需要注意的是,在回復(fù)Word批注這個(gè)功能上,免費(fèi)版的Spire.Doc暫不支持,須使用商業(yè)版。
以上為本次關(guān)于操作Word批注的全部內(nèi)容。
(本文完)
如需轉(zhuǎn)載,請(qǐng)注明出處。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。