//..................................................................... /// <summary> /// https://docs.microsoft.com/zh-cn/dotnet/api/documentformat.openxml.packaging.maindocumentpart?view=openxml-2.8.1 /// /// </summary> /// <param name="docxname"></param> public void ShowComments(string docxname) { //string docxname = @"C:\Users\Public\Documents\MainDocumentPartEx.docx"; string comments = null; //List<string> listing = new List<string>( ); // Open the file read-only. using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(docxname, false)) { MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart; WordprocessingCommentsPart docxprocCommentsPart = mainPart.WordprocessingCommentsPart; // Read the comments using a stream reader. using (StreamReader streamReader = new StreamReader(docxprocCommentsPart.GetStream( ))) { comments = streamReader.ReadToEnd( ); } } FileInfo fileinfo = new FileInfo(docxname); string msgname = fileinfo.FullName + "-comments.txt"; File.WriteAllText(msgname, comments, Encoding.Default); //Console.WriteLine( comments ); //Console.ReadKey( ); return; }
public override string ReadContents() { string documentContent = null; Stream stream = new MemoryStream(File.ReadAllBytes(_FilePath)); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, false)) { using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { documentContent = sr.ReadToEnd(); // Now search any Comments in the document as well. WordprocessingCommentsPart wordProcessingCommentsPart = wordDoc.MainDocumentPart.WordprocessingCommentsPart; if (wordProcessingCommentsPart != null) { using (StreamReader sr1 = new StreamReader(wordProcessingCommentsPart.GetStream())) { documentContent += sr1.ReadToEnd(); } } } } return(documentContent); }
/// <summary> /// 获取文档内容 /// </summary> /// <param name="document"></param> /// <returns></returns> public static string GetCommentsFromDocument(string document) { string comments = null; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; WordprocessingCommentsPart WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart; Stream stream = WordprocessingCommentsPart.GetStream(); using (StreamReader streamReader = new StreamReader(stream)) { comments = streamReader.ReadToEnd(); } } return(comments); }