2013-03-14 120 views
-2

我在c#中编码,将PDF文档转换为MS Word,并根据需要格式化文档文档。将光标置于Word文档中句号的末尾

任何人都可以建议我如何

  1. 设置光标焦点到每个句子的末尾,打在每个句子的结尾输入。
  2. 选择并格式化每个段落的首字母。

这里是我使用的代码:

for (int i = 0; i < selectedFiles; i++) 
{ 

    inFileName = openFile.SafeFileNames[i]; 
    outFileName = inFileName; 
    pos = inFileName.LastIndexOf('.'); 
    if (pos > 0) 
     outFileName = outFileName.Substring(0, pos); 
    outFileName += ".doc"; 

    //outFileName = savePDFFile.FileName; 

    //Convert(openFile.SafeFileNames[i], outFileName); 
    Convert(cPath + inFileName, cPath + outFileName); 

    Object oMissing = System.Reflection.Missing.Value; 
    //OBJECTS OF FALSE AND TRUE 
    Object oTrue = true; 
    Object oFalse = false; 
    //Create word document 


    Word.Application oWord = new Word.Application(); 
    Word.Document oWordDoc = new Word.Document(); 
    Object oTemplatePath = cPath + outFileName; 

    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); 

    #region Replace Keyword 
    this.FindAndReplace(oWord, " ", " "); 
    this.FindAndReplace(oWord, ". ", "."); 
    this.FindAndReplace(oWord, " . ", "."); 
    this.FindAndReplace(oWord, " .", "."); 
    this.FindAndReplace(oWord, "-", " – "); 
    this.FindAndReplace(oWord, ",", " ,"); 
    this.FindAndReplace(oWord, " , ", " ,"); 
    this.FindAndReplace(oWord, " -", "-"); 
    this.FindAndReplace(oWord, " - ", "-"); 
    this.FindAndReplace(oWord, "- ", "-"); 
    #endregion Replace Keyword 
    Word.Range rng = null; 
    if (oWordDoc.Sentences.Count > 0) 
    { 
     object startLocation = oWordDoc.Sentences[1].Start; 
     object endLocation = oWordDoc.Sentences[1].End; 
     // Supply a Start and End value for the Range. 
     rng = oWordDoc.Range(ref startLocation, ref endLocation); 
     // Select the Range. 
     rng.Select(); 
    } 
    object missing = Missing.Value; 
    object what = Word.WdGoToItem.wdGoToLine; 
    object which = Word.WdGoToDirection.wdGoToLast; 
    oWordDoc.GoTo(ref what, ref which, ref missing, ref missing); 
    for (int j = 0; j < oWordDoc.Sentences.Count; j++) 
    { 
     //oWordDoc.Sentences[j].Text = oWordDoc.Sentences[j].Text.ToString() + "\n"; 
    } 
    // add header 
    foreach (Section section in oWordDoc.Sections) 
    { 
     Range headerRange = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
     headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; 
     headerRange.Font.Size = 12; 
     headerRange.Font.Name = "Ariel"; 

     if (rng != null) 
      headerRange.Text = rng.Text.Trim(); 
    } 
    rng.Text = ""; 
    object findStr = "rr"; //sonething to find 
    while (oWord.Selection.Find.Execute(ref findStr)) //found... 
    { 
     //change font and format of matched words 
     oWord.Selection.Font.Name = "Tahoma"; //change font to Tahoma 
     oWord.Selection.Font.Size = 48; 
     oWord.ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle; 
     //oWord.ActiveWindow.Selection.ParagraphFormat.SpaceAfter = 0.0F; 
     oWord.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed; //change color to red 
    } 
    oWordDoc.Content.Font.Name = "Franklin Gothic Book"; 
    oWordDoc.Content.Font.Size = 11.5F; 
    oWordDoc.Content.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify; 
    //oWordDoc.Content.Start = 0; 
    oWordDoc.ShowGrammaticalErrors = false; 
    oWordDoc.ShowSpellingErrors = false; 
    //oWordDoc.Content.Sections.First 
    oWordDoc.SaveAs(cPath + outFileName); 
    progressBar1.Value = (((i + 1) * 100)/selectedFiles); 
} 
+0

这是一个很大的代码。期望人们阅读太多了。如果将其降至最小的工作示例,您将更有可能获得高质量的答案。所以我会将有关字体大小的东西映像成可以删除。我们不需要阅读。 – 2013-03-14 08:26:01

回答

1

可以使用InsertParagraphAfter功能每个句子后添加新行。

喜欢的东西:

 int count = oWordDoc.Sentences.Count; 
     for(int i=1; i<=count;i++) 
     { 
      object startLocation = oWordDoc.Sentences[i].Start; 
      object endLocation = oWordDoc.Sentences[i].End; 
      // Supply a Start and End value for the Range. 
      rng = oWordDoc.Range(ref startLocation, ref endLocation); 
      // Select the Range. 
      rng.Select(); 
      rng.InsertParagraphAfter(); 
     } 
+0

谢谢,它工作得很好,但我在搜索每个句子的最后一个词后应该没有空格。在每一个句子之后都应该有一个单独的进入....请建议 – user2168778 2013-03-14 08:46:19

+0

@ user2168778,冷静下来 – 2013-03-14 11:03:42

相关问题