如何使用iTextSharp确定文本字段允许的最大字符数?

时间:2010-10-06 18:14:32

标签: asp.net itextsharp

我有一个PDF文档,其中包含许多文本字段,其中有几个字段具有最大长度 - 即最大允许字符数。

有没有办法使用iTextSharp来确定这个设置?这是我到目前为止的代码:

Dim reader As New iTextSharp.text.pdf.PdfReader("Foobar.pdf")
Dim inputFields As IDictionary(Of String, iTextSharp.text.pdf.AcroFields.Item) = reader.AcroFields.Fields

For Each key As String In inputFields.Keys
    Dim PDFFieldName As String = key
    Dim MaxFieldLength As Integer = ???

    ...
Next

我需要将MaxFieldLength设置为迭代的当前表单字段的允许字符数。

由于

2 个答案:

答案 0 :(得分:2)

我认为你正在寻找这样的东西:

    Dim reader As New PdfReader("YourPdf.pdf")
    Dim fields As IDictionary(Of String, iTextSharp.text.pdf.AcroFields.Item) = reader.AcroFields.Fields
    For Each key As String In fields.Keys
        Dim fieldItem = reader.AcroFields.GetFieldItem(key)
        Dim pdfDictionary As PdfDictionary = fieldItem.GetWidget(0)

        Dim pdfFieldName As String = key
        Dim maxFieldLength As Integer = Int32.Parse(pdfDictionary.GetAsNumber(PdfName.MAXLEN).ToString())

        Console.WriteLine("Field={0}, MaxLen={1}", pdfFieldName, maxFieldLength.ToString())
    Next

我想在PdfName类上找到详细的文档。

答案 1 :(得分:1)

试试这个:

byte[] Password;

//generates Byte array to unlock PDF
ASCIIEncoding encoding = new ASCIIEncoding();
Password = encoding.GetBytes("xxxxxxxx");
//PdfReader myReader = new PdfReader();

PdfReader myReader = new PdfReader(file, Password);
PdfStamper myStamp = new PdfStamper(myReader, new FileStream(file + "_TMP", FileMode.Create));
//PdfStamper myStamp = new PdfStamper(myReader, new FileStream(file, FileMode.Create));
AcroFields myFields = myStamp.AcroFields;

string tmpString;

foreach (KeyValuePair<string, AcroFields.Item> de in myFields.Fields)
{
  Console.WriteLine("Processing... " + de.Key + " : " + de.Value.GetWidget(0).Get(PdfName.MAXLEN));
  tmpString = de.Key + " : " + de.Value.GetWidget(0).Get(PdfName.MAXLEN);
}