U ovoj ćemo temi objasniti kako umetnuti komentar u word dokument koristeći C#. Bilješka komentara u MS Wordu usidrena je na poziciju u tekstu ili dio teksta. U objektnom modelu dokumenta Aspose.Words komentar je čvor umetnute razine i može biti samo dijete paragrafa. Koristit ćemo ogledni ulazni Word dokument i umetnuti komentar na početku DOCX-a s nekoliko redaka C# koda. Možete koristiti isti pristup za umetanje komentara u bilo koji odlomak u dokumentu.
Koraci za umetanje komentara u Word dokument pomoću C#
- Instalirajte paket Aspose.Words for .NET s NuGet.org
- Dodaj referencu na Aspose.Words
- Postavite licencu pomoću metode License.SetLicense prije uvoza dokumenta
- Uvezite ulazni Word dokument
- Inicijalizirajte instancu DocumentBuilder klase i pomaknite kursor na početak dokumenta
- Inicijalizirajte instancu klase Comment i dodajte tekst komentara koristeći Paragraph
- Dodajte komentar prvom odlomku dokumenta
- Na kraju, spremite dokument u format datoteke Word DOCX
Prethodno smo pogledali Kako pretvoriti PDF u Word u C# bez Interop. Sada ćete naučiti kako umetnuti komentar u DOCX format datoteke.
Kod za umetanje komentara u Word dokument pomoću 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); | |
} | |
} | |
} |
Za umetanje komentara u Word dokument pomoću gornjeg C# koda nije potrebno instalirati MS Office i može se koristiti tamo gdje je instaliran .NET. Ovaj primjer koda umeće komentar na kraj prvog odlomka dokumenta.