Syncfusion DocIO用合并字段替换文本

本文关键字:替换 文本 字段 合并 DocIO Syncfusion | 更新日期: 2023-09-27 18:00:08

我有一大堆文档,它们有一个像~{fieldToBeReplaced}这样的纯文本位置标记,我希望用Merge字段替换它。

我有以下代码,它插入合并字段,然后插入文本<< MERGEFIELD_NAME >>

我想要的是它是显示名称的合并字段,就像我插入时一样->合并单词中的字段。


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"),
    new fieldReplace("~{U-pm}","_Doctor_PhoneMobile")
}
private bool SearchReplaceBookmarks(string filename)
{
    try
    {
        WordDocument wordDocument = new WordDocument(filename);
        
        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);
                
                //This is where the Merge Field is meant to be inserted
                wordDocument.Replace(fld.TextToReplace, selection, true, true);  
                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; }
}

Syncfusion DocIO用合并字段替换文本

在分析您提到的场景时,1) 从Word文档中查找占位符文本。2) 将占位符文本替换为合并字段和MS Word文档中要替换的字段。

是的,使用DocIO,占位符文本可以替换为合并字段,相当于MS Word。我们已经修改了您的代码以满足您的要求。我们使用了Replace()方法的TextBodyPart重载。请找到下面的代码片段。

修改的代码段TextBodyPart bodyPart=新的TextBodyPPart(wordDocument);bodyPart.BodyItems.Add(段落);wordDocument.Replace(fld.TextToReplace,bodyPart,true,true);

如有更多问题,请联系我们的支持团队support@syncfusion.com以便在这方面得到及时的帮助。