2013-02-08 58 views
2

我正在ASP.NET網站(Web窗體,.NET 4.0)中工作 我有一個使用Aspose.Words創建的Word文檔。現在我需要突出顯示某些字符串word文檔。我需要一個函數來執行這個動作。 這樣的事情,在Word文檔中突出顯示字符串

private void Highlight(Document doc,String anyString,Color red) 
{ 
    //Highlight all "anyString" in doc by red color 
} 

有人可以幫助我實現這一點?

回答

3

可以使用的Aspose爲你做這個,這樣就可以避免在Web服務器上的字自動化(WH ich很可能沒有安裝辦公室)。這些代碼大部分基於Aspose documentation的示例。

設置下面的類:

private class ReplaceEvaluatorFindAndHighlight : IReplacingCallback 
    { 
     /// <summary> 
     /// This method is called by the Aspose.Words find and replace engine for each match. 
     /// This method highlights the match string, even if it spans multiple runs. 
     /// </summary> 
     ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e) 
     { 
      // This is a Run node that contains either the beginning or the complete match. 
      Node currentNode = e.MatchNode; 

      // The first (and may be the only) run can contain text before the match, 
      // in this case it is necessary to split the run. 
      if (e.MatchOffset > 0) 
       currentNode = SplitRun((Run)currentNode, e.MatchOffset); 

      // This array is used to store all nodes of the match for further highlighting. 
      List<Node> runs = new List<Node>(); 

      // Find all runs that contain parts of the match string. 
      int remainingLength = e.Match.Value.Length; 
      while (
       (remainingLength > 0) && 
       (currentNode != null) && 
       (currentNode.GetText().Length <= remainingLength)) 
      { 
       runs.Add(currentNode); 
       remainingLength = remainingLength - currentNode.GetText().Length; 

       // Select the next Run node. 
       // Have to loop because there could be other nodes such as BookmarkStart etc. 
       do 
       { 
        currentNode = currentNode.NextSibling; 
       } 
       while ((currentNode != null) && (currentNode.NodeType != NodeType.Run)); 
      } 

      // Split the last run that contains the match if there is any text left. 
      if ((currentNode != null) && (remainingLength > 0)) 
      { 
       SplitRun((Run)currentNode, remainingLength); 
       runs.Add(currentNode); 
      } 

      // Now highlight all runs in the sequence. 
      foreach (Run run in runs) 
       run.Font.HighlightColor = Color.Red; 

      // Signal to the replace engine to do nothing because we have already done all what we wanted. 
      return ReplaceAction.Skip; 
     } 

     /// <summary> 
     /// Splits text of the specified run into two runs. 
     /// Inserts the new run just after the specified run. 
     /// </summary> 
     private static Run SplitRun(Run run, int position) 
     { 
      Run afterRun = (Run)run.Clone(true); 
      afterRun.Text = run.Text.Substring(position); 
      run.Text = run.Text.Substring(0, position); 
      run.ParentNode.InsertAfter(afterRun, run); 
      return afterRun; 
     } 
    } 

然後使用它:

Aspose.Words.Document doc = new Aspose.Words.Document(@"Z:\Temp\test.docx"); 

Regex reg = new Regex("anyString", RegexOptions.IgnoreCase); 
doc.Range.Replace(reg, new ReplaceEvaluatorFindAndHighlight(), true); 


doc.Save(@"Z:\Temp\newdoc.docx"); 
+0

這種支持是否將參數作爲參數傳遞? – Bisileesh

+0

這是一個班級,所以你可以通過你想要的任何東西。在構造函數中或通過屬性。 –

+0

我會弄明白的。謝謝! – Bisileesh

0

您可以使用下面的代碼來打開一個Word文檔,並突出顯示搜索文本(S):

private void btnFind_Click(object sender, EventArgs e) 
{ 
object fileName = "xxxxx"; //The filepath goes here 
string textToFind = "xxxxx"; //The text to find goes here 
Word.Application word = new Word.Application(); 
Word.Document doc = new Word.Document(); 
object missing = System.Type.Missing; 
try 
{ 
    doc = word.Documents.Open(ref fileName, ref missing, ref missing, 
    ref missing, ref missing, ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing); 
    doc.Activate(); 
    foreach (Word.Range docRange in doc.Words) 
    { 
     if(docRange.Text.Trim().Equals(textToFind, 
      StringComparison.CurrentCultureIgnoreCase)) 
     { 
      docRange.HighlightColorIndex = 
       Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow; 
      docRange.Font.ColorIndex = 
       Microsoft.Office.Interop.Word.WdColorIndex.wdWhite; 
     } 
    } 
} 
catch (Exception ex) 
{ 
    MessageBox.Show("Error : " + ex.Message); 
} 
} 

你必須使用下面的語句添加到的Microsoft.Office.Interop.Word參考:

using Word = Microsoft.Office.Interop.Word; 

如果你想要做一個函數,然後修改它

+2

字不應在服務器應用程序運行。詞彙自動化並不是爲了在一個無gui的環境中運行而設計的。 –

相關問題