【发布时间】:2018-01-14 06:05:57
【问题描述】:
上周我被要求为盲人构建一个应用程序,以编程方式填写 PDF 文档。他遇到的问题是,如果文档中的字段没有正确标记,那么他就无法将他的签名和其他信息放入文档中的正确位置。
我的第一个方法是尝试使用 iTextSharp 读取文档,然后将他的签名插入最有可能是签名框的字段中:
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();
}
}
这通过获取一个字典集,键是一个单词或短语,根据 PDF 字段检查它,然后如果该字段与键中的单词或短语匹配,则将值插入到字段中。
The signature box before my program edits it.
但我现在遇到的问题是,如果不存在任何字段,那么虽然它可能在虚线旁边有“在此处签名”,但如果不知道虚线的确切位置,就无法将文本插入到虚线上,我的用户也不能选择虚线,因为这违背了程序的要点。
我查看了一些以前的问题和答案,包括:
- 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
老实说,我被困住了,这是我第一次处理 PDF 文档。我需要一种方法来检测签名行,然后将他的名字插入到签名行中,这比在字段名称上进行拍摄更有把握。无论是在存在正确标记的字段的情况下,还是在签名行可能不超过一行显示“在此处签名”的文本的情况下。
我们将不胜感激任何帮助,即使是部分解决方案和正确方向的推动。
【问题讨论】:
-
如果您获得任何实际字段而不仅仅是扫描图像,那么您很幸运。但是表格的来源是什么?提交 ADA 投诉并修复表格。
-
他发给我的一些示例包括 TWC 替代 W-9 和直接存款表格以及其他官方文件,我正在寻找一种适用于任何类型 PDF 的程序化解决方案,其中包含虽然是签名行。我已经考虑过一种光学字符识别方法,但我想知道在我走这条路之前是否已经有可用的解决方案。
标签: c# pdf itext electronic-signature blind