ITextSharp:从表单字段中删除字符限制

我正在尝试从表单字段中删除字符限制,然后使用iTextSharp进行设置 . 我可以使用Adobe Acrobat手动执行此操作,但我们正在处理大量PDF文档,我们使用iTextSharp动态标记表单字段 . 我们没有构建这些PDF文档,我们只是将它们用作我们填充数据的模板 . 我想知道是否有办法使用api删除表单字段的字符限制,所以我们不必手动检查每个字段的字符限制 .

我猜这可以用SetFieldProperty方法完成,但我不确定如何 .

提前致谢

回答(2)

2 years ago

如果您了解PDF格式的内部结构,这很容易!下面的代码与code I wrote for a different type of PDF annotation here几乎相同 . 该页面将更详细地解释一些内容,并为您提供一些参考 . 但基本上诀窍是遍历每个页面,然后遍历每个页面的注释(表单字段属于此类别等),然后查找设置了最大长度的文本字段,并删除该限制 . 所有这些工作都是在一个只读的 PdfReader 对象上完成的,所以一旦我们完成,我们需要再次循环并使用某种 PdfWriter 将其写回 .

下面是一个完整的C#2010 WinForms应用程序,目标是iTextSharp 5.2.1,它显示了所有这些 . 有关更多详细信息,请参阅代码中的注释 .

using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            var inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
            var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.pdf");

            //Setup some variables to be used later
            PdfDictionary PageDictionary = default(PdfDictionary);
            PdfArray Annots = default(PdfArray);

            //Bind a reader to our input PDF
            PdfReader R = new PdfReader(inputFile);
            //Store the number of pages
            int PageCount = R.NumberOfPages;
            //Loop through each page remember that page numbers start at 1
            for (int i = 1; i <= PageCount; i++) {
                //Get the current page
                PageDictionary = R.GetPageN(i);

                //Get all of the annotations for the current page
                Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);

                //Make sure we have something
                if ((Annots == null) || (Annots.Length == 0)) { continue; }

                //Loop through each annotation
                foreach (PdfObject A in Annots.ArrayList) {
                    //Convert the itext-specific object as a generic PDF object
                    PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A);

                    //See if this annotation has a WIDGET which would be the UI implementation of a form field
                    if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.WIDGET)) { continue; }

                    //See if this annotation is a text field (TX)
                    if (!AnnotationDictionary.Get(PdfName.FT).Equals(PdfName.TX)) { continue; }

                    //See if it has a maximum length specified (MAXLEN)
                    if (AnnotationDictionary.Contains(PdfName.MAXLEN)) {
                        //If so, remove it
                        AnnotationDictionary.Remove(PdfName.MAXLEN);
                    }
                }
            }
            //Next we create a new document add import each page from the reader above
            using (FileStream FS = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document Doc = new Document()) {
                    using (PdfCopy writer = new PdfCopy(Doc, FS)) {
                        Doc.Open();
                        for (int i = 1; i <= R.NumberOfPages; i++) {
                            writer.AddPage(writer.GetImportedPage(R, i));
                        }
                        Doc.Close();
                    }
                }
            }
        }
    }
}

2 years ago

我能够使用Chris提供的信息来解决这个问题 . 我试图在设置值之前删除任何字段上的任何最大长度限制 .

//TargetObject is an AcroFields object
            AcroFields.Item item = TargetObject.GetFieldItem({FieldName});
            if (item != null)
            {
                foreach (var i in item.merged)
                {
                    if (i.GetType() == typeof(PdfDictionary))
                    {
                        PdfDictionary dictionary = (PdfDictionary)i;
                        if (dictionary.Contains(PdfName.MAXLEN))
                        {
                            dictionary.Remove(PdfName.MAXLEN);
                        }
                        break; //no need to continue once we find the dictionary and remove max length
                    }
                }
            }