Syncfusion DocIO使用合并字段替换文本

时间:2016-08-03 06:39:45

标签: c# replace syncfusion mergefield

我有一大堆文档,它有一个纯文本位置标记,如〜{fieldToBeReplaced},我希望它被替换为Merge字段。

我有以下代码插入合并字段然后插入文本<< 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; }
}

//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; } }

1 个答案:

答案 0 :(得分:1)

在分析您提到的场景时, 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);

如有其他问题,请通过support@syncfusion.com与我们的支持团队联系,以获得有关此问题的快速帮助。