Find Text in WORD and Replace it with Form Field(DocumentBuilder.InsertTextInput)

Hi,

How can I find particular text in word document and replace it with a text field form(DocumentBuilder.InsertTextInput) similar to the pdf explained in body
below link:
https://docs.aspose.com/words/net/find-and-replace/

Basically I want to replace some of the texts with formFields and save this doc as a pdf by preserving these formfields.

My sample code is given below. The input file is attached

public static void ProcessWordToPdfDocument()
{
    // Open the document
    Document doc = new Document("..//..//SIGN PAGE.doc");
    //Get list of eSignature tags
    List<string> lstESignTags = GetESignTagList();
    // Insert a text input field. The unique name of this field is "TextInput1", the other parameters define
    // what type of FormField it is, the format of the text, the field result and the maximum text length (0 = no limit)
    //DocumentBuilder builder = new DocumentBuilder(doc);
    //builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Hello", 0);
    //Search the each of the text present in lstESignTags list and replace it with a formTextField at the same location using builder.InsertTextInput --> How to implement this 
    foreach (string eSignTagName in lstESignTags)
    {
        //find text
        //remove text
        //use the location obtained from the text, set field attributes and Insert text form field
    }
    //Save document to the PDF format with specified options
    // Render the first page only and preserve form fields as usable controls and not as plain text
    PdfSaveOptions pdfOptions = new PdfSaveOptions();
    pdfOptions.PageIndex = 0;
    pdfOptions.PageCount = 1;
    pdfOptions.PreserveFormFields = true;
    doc.Save("../../SIGN_PAGE_FormFields.doc", pdfOptions);
}

private static List<string> GetESignTagList()
{
    List<string> lstESignTags = new List<string>();
    lstESignTags.Add("ESIG_DEF_SIGNER1_SIGNATURE");
    lstESignTags.Add("ESIG_DEF_SIGNER1_DATE");
    lstESignTags.Add("ESIG_DEF_SIGNER1_TITLE");
    lstESignTags.Add("ESIG_DEF_SIGNER2_SIGNATURE");
    lstESignTags.Add("ESIG_DEF_SIGNER2_TITLE");
    lstESignTags.Add("ESIG_ABC_SIGNER1_SIGNATURE");
    lstESignTags.Add("ESIG_ABC_SIGNER1_DATE");
    lstESignTags.Add("ESIG_ABC_SIGNER1_TITLE");
    return lstESignTags;
}

Hi Komal,

Thanks for your inquiry. I believe, you can achieve this by using the following code:

Document doc = new Document(MyDir + @"SIGN+PAGE.doc");
doc.Range.Replace(new Regex("ESIG_DEF_SIGNER1_SIGNATURE"), new 
 MyReplaceEvaluator(), false); 
doc.Save(MyDir + @"out.doc");
private class MyReplaceEvaluator : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo(e.MatchNode);
        builder.InsertTextInput("name of form field", TextFormFieldType.Regular, "", "123456789", 10);
        e.Replacement = "";
        return ReplaceAction.Replace;
    }
}

private class MyReplaceEvaluator : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo(e.MatchNode);
        builder.InsertTextInput("name of form field", TextFormFieldType.Regular, "", "123456789", 10);
        e.Replacement = "";
        return ReplaceAction.Replace;
    }
}

Best regards,

Hi Hafeez,

Thanks for your feedback. I modified the suggested solution a little bit to have the proper formatting for the Text form fileds when I converted them to Word/PDF. I am seeing following issues whenever I create a text form filed and save the document as either doc or pdf.

My output: SIGN_PAGE_FormFields_Output.pdf

1. The background color or highlighted color of the form field is not coming in Grey as expected.

2. The border color of the form field is not coming in Red as expected.

Please see the attached file SIGN_PAGE_Expected background and formatting.pdf which is created using the aspose.pdf

//open pdf document
Document pdfDocument = new Document("…//…//SIGN_PAGE.pdf");
//add form field on text position
FormEditor formEditor = new FormEditor(pdfDocument);
foreach (string eSignTagName in lstESignTags)
{
    //find text
    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(eSignTagName);
    pdfDocument.Pages[1].Accept(textFragmentAbsorber);
    //determine text position
    Rectangle textRect = textFragmentAbsorber.TextFragments[1].Rectangle;
    //remove text
    textFragmentAbsorber.TextFragments[1].Text = string.Empty;
    //save document without text
    // pdfDocument.Save("…//…[//TextRemoved.pdf](//TextRemoved.pdf)");
    //use the location obtained from the text
    formEditor.AddField(FieldType.Text, eSignTagName, 1,
    (float)textRect.LLX,
    (float)textRect.LLY,
    eSignTagName.Contains("DATE") ? (float)textRect.URX - 10 : (float)textRect.URX,
    eSignTagName.Contains("SIGNATURE") ? (float)textRect.URY + 5 : (float)textRect.URY);
    //set field attributes
    FormFieldFacade fieldFacade = new FormFieldFacade();
    formEditor.Facade = fieldFacade;
    fieldFacade.BackgroundColor = System.Drawing.Color.Gray;
    fieldFacade.BorderColor = System.Drawing.Color.Red;
    formEditor.DecorateField(eSignTagName);
}
//save document with form field
formEditor.Save("…//…//SIGN_PAGE_eSignFieldAdded.pdf");

3. The height of all the text form filed is not coming correctly. Looks like some paragraph spacing is added.

4. The length of ESIG_DEF_SIGNER1_DATE form field is not coming correctly. I specified 10 and it’s coming around 1 or 2.

4. How can I set the Text formatting for the Text Form field. Eg: Title Case.

My code for creating a form field using aspose.words is given below:

public static void ProcessWordToPdfDocument()
{
    // Open the document
    Document doc = new Document("…//…//SIGN PAGE.doc");
    //Get list of eSignature tags
    List lstESignTags = GetESignTagList();
    //Search the each of the text present in lstESignTags list and replace it with a formTextField at the same location using builder.InsertTextInput --> How to implement this 
    foreach (string eSignTagName in lstESignTags)
    {
        //find eSignTagging text and repalce it with InsertText form field
        doc.Range.Replace(new Regex(eSignTagName), new MyReplaceEvaluator(eSignTagName), false);
    }
    //Save document to the PDF format with specified options
    // Render the first page only and preserve form fields as usable controls and not as plain text
    PdfSaveOptions pdfOptions = new PdfSaveOptions();
    pdfOptions.PageIndex = 0;
    pdfOptions.PageCount = 1;
    pdfOptions.PreserveFormFields = true;
    doc.Save("…/…/SIGN_PAGE_FormFields.pdf", pdfOptions);
}

private static List GetESignTagList()
{
    List lstESignTags = new List();
    lstESignTags.Add("ESIG_DEF_SIGNER1_SIGNATURE");
    lstESignTags.Add("ESIG_DEF_SIGNER1_DATE");
    lstESignTags.Add("ESIG_DEF_SIGNER1_TITLE");
    lstESignTags.Add("ESIG_DEF_SIGNER2_SIGNATURE");
    lstESignTags.Add("ESIG_DEF_SIGNER2_TITLE");
    lstESignTags.Add("ESIG_ABC_SIGNER1_SIGNATURE");
    lstESignTags.Add("ESIG_ABC_SIGNER1_DATE");
    lstESignTags.Add("ESIG_ABC_SIGNER1_TITLE");
    return lstESignTags;
}

private class MyReplaceEvaluator : IReplacingCallback
{
    string eSignTagName = string.Empty;
    public MyReplaceEvaluator(string tagName)
    {
        eSignTagName = tagName;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        int maxLength = 50;
        TextFormFieldType formatType = TextFormFieldType.Regular;
        if (this.eSignTagName.Contains("DATE"))
        {
            maxLength = 10;
            formatType = TextFormFieldType.Date;
        }
        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo(e.MatchNode);
        FormField textFormField = builder.InsertTextInput(this.eSignTagName, formatType, "", "", maxLength);
        /*Set the formatting */
        textFormField.Font.Border.Color = System.Drawing.Color.Red;
        textFormField.Font.HighlightColor = System.Drawing.Color.Gray;
        textFormField.Font.Shading.ForegroundPatternColor = System.Drawing.Color.Gray;
        textFormField.HelpText = "Sample Help Text";
        textFormField.Font.Italic = true;
        textFormField.Font.Bold = false;
        textFormField.Font.Italic = true;
        textFormField.Font.Name = "Arial";
        textFormField.Font.Size = 12;
        textFormField.Font.Spacing = 5;
        textFormField.Font.Underline = Underline.Single;
        e.Replacement = "";
        return ReplaceAction.Replace;
    }
}

Hi Komal,

Thanks for your inquiry. We are checking with these scenarios and will get back to you soon.

Best regards,

Hi Komal,

Thanks for being patient.

Regarding problem#2 (The border color of the form field is not coming in Red as expected), for the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10188. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Regarding your other problems, I would suggest you please upgrade to thelatest version 14.4.0 from the following link. I hope, this helps.

https://downloads.aspose.com/words/net

In case the problems still remain, please generate final 1) output Word and 2) output PDF documents using Aspose.Words 14.4.0 and attach them here for further testing. Thanks for your cooperation.

Best regards,