في هذا الموضوع ، سنشرح كيفية إدراج التعليق في مستند Word باستخدام C#. تعليق التعليق التوضيحي في برنامج MS Word مثبت على موضع في النص أو منطقة من النص. في نموذج كائن مستند Aspose.Words ، يكون التعليق عقدة على مستوى السطر ولا يمكن أن يكون إلا عنصرًا فرعيًا من الفقرة. سنستخدم نموذج مستند Word للإدخال وإدخال تعليق في بداية DOCX مع بضعة أسطر من كود C#. يمكنك استخدام نفس الأسلوب لإدراج التعليق على أي فقرة في المستند.
خطوات إدراج تعليق في مستند Word باستخدام C#
- قم بتثبيت حزمة Aspose.Words for .NET من NuGet.org
- أضف مرجعًا إلى Aspose.Words
- حدد الترخيص باستخدام طريقة License.SetLicense قبل استيراد المستند
- قم باستيراد مستند Word المدخل
- تهيئة مثيل فئة DocumentBuilder وتحريك المؤشر إلى بداية المستند
- تهيئة مثيل فئة التعليق وإضافة نص التعليق باستخدام الفقرة
- أضف تعليقًا إلى الفقرة الأولى من المستند
- أخيرًا ، احفظ المستند بتنسيق ملف Word DOCX
في السابق ، بحثنا في كيفية تحويل PDF إلى Word في C# بدون Interop. الآن ، سوف تتعلم كيفية إدراج تعليق في تنسيق الملف DOCX.
رمز لإدراج تعليق في مستند Word باستخدام C#
using Aspose.Words; | |
using System; | |
namespace InsertCommentinWord | |
{ | |
class how_to_insert_comment_into_word_document_using_c_sharp | |
{ | |
public static void InsertComment(String directorypath) | |
{ | |
//Set Aspose license before importing document. | |
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License(); | |
AsposeWordsLicense.SetLicense(directorypath + @"Aspose.Words.lic"); | |
//Import the Document into Aspose.Words DOM. | |
Document doc = new Document(directorypath + "input.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
//Move the cursor to the beginning of the document. | |
builder.MoveToDocumentStart(); | |
//Insert comment to first paragraph of document. | |
Comment comment = new Comment(doc, "Aspose.Words", "AW", DateTime.Today); | |
builder.CurrentParagraph.AppendChild(comment); | |
comment.Paragraphs.Add(new Paragraph(doc)); | |
comment.FirstParagraph.Runs.Add(new Run(doc, "Comment text.")); | |
//Save the Document | |
doc.Save(directorypath + @"output.docx", SaveFormat.Docx); | |
} | |
} | |
} |
إدخال تعليق في مستند Word باستخدام رمز C# أعلاه لا يحتاج إلى تثبيت MS Office ويمكن استخدامه حيث تم تثبيت .NET. يقوم مثال التعليمات البرمجية هذا بإدراج التعليق في نهاية الفقرة الأولى من المستند.