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