2016-08-03 6 views
0

나는 ~ {fieldToBeReplaced}와 같은 평범한 텍스트 위치 마커가있는 전체 문서 묶음을 가지고 있으며 병합 필드로 바꾸길 원합니다.Syncfusion DocIO 병합 필드로 텍스트 바꾸기

다음 코드는 병합 필드를 삽입 한 다음 < < MERGEFIELD_NAME >> 텍스트를 삽입하는 코드입니다.

내가 원하는 것은 단어 삽입 -> 병합 필드 인 것처럼 이름이 표시된 병합 필드가되는 것입니다.

//linqPad Script 
void Main() 
{ 
    Console.WriteLine("::::: Replacing BookMarks ::::: "); 
    //Search And Replace bookmarks 
    try 
    { 
     var replaceDir = new DirectoryInfo(saveFileLocation); 
     var bookmarkFiles = replaceDir.GetFiles().ToList(); 
     foreach (var bkmFile in bookmarkFiles) 
     { 
      if (SearchReplaceBookmarks(bkmFile.FullName)) 
      { 
       Console.WriteLine("Bookmarks Replace:" + bkmFile.Name + " ::: "); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("ERROR: ::: " + ex.Message); 
    } 
} 
static string startDir = Path.GetDirectoryName(Util.CurrentQueryPath); 
string saveFileLocation = startDir + @"\Converted\"; 
    private List<fieldReplace> fieldList = new List<fieldReplace>() { 
    //new fieldReplace("",""), 
    new fieldReplace("~{U-nm}","_Doctor"), 
    new fieldReplace("~{P-nl}","_Patient_Surname","Patient_Firstname"), 
    new fieldReplace("~{DOBN}","_Patient_DOB"), 
    new fieldReplace("~{U-ph","_Doctor_PhoneWork"),//Surgeon Business 
    new fieldReplace("~{U-pm}","_Doctor_PhoneMobile")//Surgeon After Hours 
} 
// Define other methods and classes here 
private bool SearchReplaceBookmarks(string filename) 
{ 
    try 
    { 
     WordDocument wordDocument = new WordDocument(filename); 
     //Adds one section and one paragraph to the document 
     foreach (var fld in fieldList) 
     { 
      var replaceBookmark = wordDocument.Find(fld.TextToReplace, false, true); 
      //if the bookmark is in the list then 
      while (replaceBookmark != null) 
      { 
       //Find and replace text with merge field. 
       var paragraph = new WParagraph(wordDocument); 
       for (int i =0 ; i<= fld.FieldNames.Length-1;i++){ 
        var field = paragraph.AppendField(fld.FieldNames[i], FieldType.FieldMergeField); 
        field.FieldType = FieldType.FieldMergeField; 
        if (i < fld.FieldNames.Length - 1) { paragraph.AppendText(", ");} 
       } 
       var selection = new TextSelection(paragraph, 0, paragraph.Text.Length); 
      wordDocument.Replace(fld.TextToReplace, selection, true, true); //This is where the Merge Field is meant to be inserted 
       replaceBookmark = wordDocument.FindNext(paragraph, fld.TextToReplace, false, true); 
      } 
     } 
     //Debug.Write(wordDocument.MailMerge.MappedFields); 
     wordDocument.Save(filename, FormatType.Docx); 
     wordDocument.Close(); 
     return true; 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("ERROR:" + filename + " ::: " + ex.Message); 
     return false; 
    } 
} 
private class fieldReplace 
{ 
    public fieldReplace(string oldText, params string[] newNewFieldName) 
    { 
     this.TextToReplace = oldText; 
     this.FieldNames = newNewFieldName; 
    } 
    public string TextToReplace { get; set; } 
    public string[] FieldNames { get; set; } 
} 

답변

2
당신의 언급 한 시나리오 분석에

, 1) Word 문서에서 자리 표시 자 텍스트를 찾기. 2) 자리 표시 자 텍스트를 병합 필드로 대체하고 MS Word 문서에서 바꿀 필드를 바꿉니다.

예, DocIO를 사용하면 자리 표시 자 텍스트를 MS Word와 동일한 병합 필드로 바꿀 수 있습니다. 귀하의 요구 사항을 충족시키기 위해 귀하의 코드가 수정되었습니다. Replace() 메서드의 TextBodyPart 오버로드를 사용했습니다. 아래의 코드 스 니펫을 찾으십시오.

수정 된 코드 스 니펫 TextBodyPart bodyPart = new TextBodyPart (wordDocument); bodyPart.BodyItems.Add (단락); wordDocument.Replace (fld.TextToReplace, bodyPart, true, true);

더 궁금한 사항이 있으시면 [email protected]으로 문의하여 지원팀에 문의하십시오.