文檔首頁(yè)>>Aspose中文文檔>>將注釋插入文字處理文檔
將注釋插入文字處理文檔
Aspose.Words是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類(lèi)文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
使用Aspose.Words
在 Aspose.Words 中,您可以使用Comment類(lèi)來(lái)處理注釋。
以下代碼示例演示如何將注釋插入到 Word 文檔中:
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Comment comment = new Comment(doc); // Insert some text into the comment. Paragraph commentParagraph = new Paragraph(doc); commentParagraph.AppendChild(new Run(doc, "This is comment!!!")); comment.AppendChild(commentParagraph); // Create a "CommentRangeStart" and "CommentRangeEnd". int commentId = 0; CommentRangeStart start = new CommentRangeStart(doc, commentId); CommentRangeEnd end = new CommentRangeEnd(doc, commentId); builder.Write("This text is before the comment. "); // Insert comment and comment range start. builder.InsertNode(comment); builder.InsertNode(start); // Insert some more text. builder.Write("This text is commented. "); // Insert end of comment range. builder.InsertNode(end); builder.Write("This text is after the comment."); doc.Save(ArtifactsDir + "Insert a comment - Aspose.Words.docx");
點(diǎn)擊復(fù)制
使用 Open XML SDK
需要添加的命名空間:
using System; using System.Linq; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
點(diǎn)擊復(fù)制
以下代碼示例演示如何從文檔中刪除分頁(yè)符:
public void InsertACommentFeature() { using (WordprocessingDocument document = WordprocessingDocument.Create(ArtifactsDir + "Insert a comment - OpenXML.docx", WordprocessingDocumentType.Document)) { // Locate the first paragraph in the document. Paragraph firstParagraph = document.MainDocumentPart.Document.Descendants<Paragraph>().First(); Comments comments; string id = "0"; // Verify that the document contains a // WordProcessingCommentsPart part; if not, add a new one. if (document.MainDocumentPart.GetPartsOfType<WordprocessingCommentsPart>().Any()) { comments = document.MainDocumentPart.WordprocessingCommentsPart.Comments; if (comments.HasChildren) // Obtain an unused ID. id = comments.Descendants<Comment>().Select(e => e.Id.Value).Max(); } else { // No "WordprocessingCommentsPart" part exists, so add one to the package. WordprocessingCommentsPart commentPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>(); commentPart.Comments = new Comments(); comments = commentPart.Comments; } // Compose a new Comment and add it to the Comments part. Paragraph p = new Paragraph(new Run(new Text("This is my comment."))); Comment cmt = new Comment { Id = id, Author = "author", Initials = "initials", Date = DateTime.Now }; cmt.AppendChild(p); comments.AppendChild(cmt); comments.Save(); // Specify the text range for the Comment. // Insert the new CommentRangeStart before the first run of paragraph. firstParagraph.InsertBefore(new CommentRangeStart {Id = id}, firstParagraph.GetFirstChild<Run>()); // Insert the new CommentRangeEnd after last run of paragraph. var cmtEnd = firstParagraph.InsertAfter(new CommentRangeEnd {Id = id}, firstParagraph.Elements<Run>().Last()); // Compose a run with CommentReference and insert it. firstParagraph.InsertAfter(new Run(new CommentReference {Id = id}), cmtEnd); } }
點(diǎn)擊復(fù)制