如何使用itextsharp将表单字段添加到现有pdf? [英] How to add a form field to an existing pdf with itextsharp?

查看:83
本文介绍了如何使用itextsharp将表单字段添加到现有pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用itextsharp将表单字段添加到现有pdf中?

How to add a form field to an existing pdf with itextsharp?

我有一个现有的pdf文档,我想在不创建的情况下向其添加表单字段一份副本并写出一份新文件。

I have an existing pdf document, I'd like to add form fields to it without creating a copy and writing out a new document.

推荐答案

经过进一步审查,该领域的裁决被推翻。如果您将压模压平,则字段未在结果文档中显示(因为它们缺少外观设置)。顺便说一句,表单展平会阻止表单字段的进一步编辑。现在我们可以为表单添加外观,但是,更简单的方法是使用TextField类,而不用担心显式设置外观对象。

After further review, the ruling on the field is overturned. Turns out if you form flatten the stamper the fields do not show on the resulting document (because they lack 'appearance' settings). BTW, form flattening prevents further edits of a form field. Now we can add appearance to the form, however, an easier way is to use the TextField class and not worry about explicitly setting up 'appearance' objects.

public void ABetterWayToAddFormFieldToExistingPDF( )
{
    PdfReader reader = new PdfReader(@"c:\existing.pdf");

    FileStream out = new FileStream(@"C:\existingPlusFields.pdf", FileMode.Create, FileAccess.Write);

    PdfStamper stamp = new PdfStamper(reader, out);           

    TextField field = new TextField(stamp.Writer, new iTextSharp.text.Rectangle(40, 500, 360, 530), "some_text");

   // add the field here, the second param is the page you want it on         
    stamp.AddAnnotation(field.GetTextField(), 1);

    stamp.FormFlattening = true; // lock fields and prevent further edits.

    stamp.Close();
}

这篇关于如何使用itextsharp将表单字段添加到现有pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆