How to get the textbox in mailmerge field

Hi Aspose Team,

As intro, I have able to generated report with mail merge fields, using .dot file as input and output as .pdf

And for now I have requirement to have mail merge field to show textbox in an output file. So in output file it is PDF form when user can enter the information (text) on the textbox.

You already have the documentation at this link
https://docs.aspose.com/pdf/net/acroforms/
but problem is I need to define the location x and y hardcoded.

My goal is to have the texbox near or replace the position where the mail merge field exist on my .dot file (input file), so when output file (.pdf) generated, my user can fill in text in that textbox.

Please provide a sample codes, I have provided the input file and and a manipulation screenshot as expected goal.

Thanks,

Dicky

Hi Dicky,

Thanks for your inquiry. In your case, I suggest you following solution.

  1. Implement IFieldMergingCallback interface and insert FormField
  2. Use PdfSaveOptions.PreserveFormFields to preserve Microsoft Word form fields as form fields in PDF

Please use following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "Input.dot");
// Add a handler for the MergeField event.
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldTest01();
// Fill the fields in the document with user data.
doc.MailMerge.Execute(
new string[] { "SignerName", "SignerTitle" },
new object[] { "Enter the signer name", "Title" });
PdfSaveOptions options = new PdfSaveOptions();
options.PreserveFormFields = true;
doc.Save(MyDir + "Out.pdf", options);
private class HandleMergeFieldTest01 : IFieldMergingCallback
{
    /// 
    /// This is called when merge field is actually merged with data in the document.
    /// 
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldName.Trim() == "SignerName")
        {
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.FieldName.Trim());
            // Insert a text form field for input a name.
            builder.InsertTextInput("", TextFormFieldType.Regular, "", (string)e.FieldValue, 0);
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}

Hi Tahir,

Thanks it works, I will try more.

Hi Dicky,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.