Last week I was asked to build an application for a blind man to programmatically fill out a PDF document. The problem he is having is that if the fields in the document aren't labeled correctly then he is not able to put his signature and other information into the document in the correct place.
My first approach was to attempt to read the document using iTextSharp and then insert his signature into the field which was most likely to be the signature box:
public string[] MassFieldEdit(IDictionary<string, string> userData, string originalDocument, string edittedDocument, bool flatten)
{
PdfReader reader = new PdfReader(originalDocument);
reader.SelectPages("1-" + reader.NumberOfPages.ToString());
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(edittedDocument, FileMode.Create)))
{
AcroFields form = stamper.AcroFields;
ICollection<string> fieldKeys = form.Fields.Keys;
List<string> leftover = new List<string>(fieldKeys);
foreach (string fieldKey in fieldKeys)
{
foreach (KeyValuePair<string, string> s in user)
{
//Replace Form field with my custom data
if (fieldKey.ToLower().Contains(s.Key.ToLower()))
{
form.SetField(fieldKey, s.Value);
leftover.Remove(fieldKey);
}
}
}
//The below will make sure the fields are not editable in
//the output PDF.
stamper.FormFlattening = flatten;
return leftover.ToArray();
}
}
This works by taking a dictionary set, the key being a word or phrase, checking that against the PDF fields and then inserting the value into the fields if the field matches the word or phrase in the key.
The signature box before my program edits it.
But the problem I have now is that if no field exists then although it may have "sign here" right next to the dotted line, there is no way to insert text onto the dotted line without knowing exactly where the dotted line is, nor can my user select the dotted line because that defeats the point of the program.
I have looked at a number of previous questions and answers, including:
- How do I get a TextField from AcroFields using iText/Sharp?
- How to convert PDF to WORD in c#
- Insert text in existing pdf with itextsharp
- ITextSharp insert text to an existing pdf
I need a way to detect the signature line and then insert his name onto the signature line with more certainty than taking pot shots at field names. Both in situations where a correctly labeled field exists and also in situations where the signature line may be no more than a line of text which says "sign here".