We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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 { margin: 0 0 0 0; padding:0 0 0 0 } td,div { font-family:Arial;font-size:10pt;vertical-align:top } /* Copyright IBM Corp. 2011 All Rights Reserved. */ body { margin: 0 0 0 0; padding:0 0 0 0; overflow:hidden; } .transcript { background-color:#d2d2d2; } .messageBlock { padding-left:10px; padding-right:10px; margin-bottom:3px } .message { padding-left:20px; margin-left:95px; word-wrap:break-word; white-space:-moz-pre-wrap; _white-space:pre; white-space:pre-wrap;} .messageCont { padding-left:20px; margin-left:95px; word-wrap:break-word; white-space:-moz-pre-wrap; _white-space:pre;white-space:pre-wrap;} .other { font-size:11px;color:#39577a;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .myself { font-size:11px;color:#da8103;font-style:normal;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont { font-size:8px;text-align:right; color:#39577a;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .myselfCont { font-size:8px;text-align:right; color:#da8103;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .system { font-size:11px; word-wrap:break-word;color:#da8103;font-style:normal;font-weight:normal; white-space:-moz-pre-wrap; _white-space:pre;white-space:pre-wrap; } .showTimestamp { padding-left:20px; font-size:11px; float:right; color:#999999;font-style:normal;font-weight:normal; } .other1 { font-size:11px; color:#ac2000;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont1 { font-size:8px;text-align:right; color:#ac2000;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other2 { font-size:11px; color:#3c9fa8;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont2 { font-size:8px;text-align:right; color:#3c9fa8;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other3 { font-size:11px; color:#e25614;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont3 { font-size:8px;text-align:right; color:#e25614;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other4 { font-size:11px; color:#0b6ac8;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont4 { font-size:8px;text-align:right; color:#0b6ac8;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other5 { font-size:11px; color:#b23290;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont5 { font-size:8px;text-align:right; color:#b23290;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other6 { font-size:11px; color:#02e7c7;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont6 { font-size:8px;text-align:right; color:#02e7c7;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other7 { font-size:11px; color:#5b3284;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont7 { font-size:8px;text-align:right; color:#5b3284;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .highlight { background-color:#bed6f8; } .datestamp { padding-right:0px; font-size:11px; cursor:default; margin-bottom:1px; background-color:#c0c0c0; width:100%; float:left; text-align:right; color:#ffffff; font-weight:bold; font-style:italic; } #chatAlert { float:left; border-bottom:1px solid #E8D091; padding:6px; width:100%; color:#A5754C; } #chatAlertImage { float:left; } #chatAlertText { float:left; margin-left:6px; } #chatAlertClose { float:right; margin-right:10px; padding-right:6px; margin-top:0px; } #chatAlertText a { color:#A5754C; } #chatAlertText a:hover { color:#A5754C; text-decoration:none; }

.tsDisplay { display:block }.dsDisplay { display:block }

below link:

http://www.aspose.com/blogs/aspose-blogs/shahzad-latif/archive/2011/05/26/find-text-in-pdf-and-replace-it-with-form-field-combining-dom-api-and-facades-features.html

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");<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

doc.Range.Replace(new
Regex(“ESIG_DEF_SIGNER1_SIGNATURE”),
new MyReplaceEvaluator(),
false); <o:p></o:p>

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");

//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.

http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

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,