2008-12-23 17 views
2

Comment rechercher un texte spécifique dans un texte (dans Docx à l'aide d'OpenXML SDK 2.0) et une fois que vous l'avez trouvé comment insérez-vous un commentaire le "texte de recherche". Le "texte de recherche" peut être une sous-chaîne d'une exécution existante. Tous les exemples dans les exemples insèrent des commentaires autour du premier paragraphe ou quelque chose de simple comme ça ... pas ce que je cherche.Insertion d'un commentaire dans un fichier Docx * autour * d'un texte à l'intérieur d'une série

Merci

Répondre

2

Vous devez le diviser en plusieurs parties. Essayez d'utiliser DocumentReflector - il génère même du code C# - pour regarder un document créé avec Word. La structure devrait ressembler à ceci (simplifié):

<paragraph> 
    <run>...</run> 
    <commentRangeStart /> 
    <run>search text</run> 
    <commentRangeEnd /> 
    <run>...</run> 
</paragraph> 
0

Pour quelqu'un qui pourrait être à la recherche toujours la réponse:

Here est le code pour que:

private void AddComment(Paragraph paragraph, string text) 
     { 
      string commentId = GetNextCommentId(); 
      Comment comment = new Comment() { Id= commentId, Date = DateTime.Now }; 
      Paragraph commentPara = new Paragraph(new Run(new Text(GetCommentsString(text))) { RunProperties = new RunProperties(new RunStyle() { Val = "CommentReference" }) }); 
      commentPara.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId() { Val = "CommentText" }); 
      comment.AppendChild(commentPara); 
      _comments.AppendChild(comment);//Comments object 
      _comments.Save(); 

      paragraph.InsertBefore(new CommentRangeStart() { Id = commentId }, paragraph.GetFirstChild<Run>()); 
      var commentEnd = paragraph.InsertAfter(new CommentRangeEnd() { Id = commentId }, paragraph.Elements<Run>().Last()); 
      paragraph.InsertAfter(new Run(new CommentReference() { Id = commentId }), commentEnd); 
     }