2010-10-06 54 views
1

我有一个带有多个文本字段的PDF文档,其中有几个文本字段具有最大长度 - 即最大允许字符数。如何使用iTextSharp确定文本字段允许的最大字符数?

有没有使用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

我认为你在寻找这样的事情:

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类的详细文档。

+0

谢谢!在下周之前,我不会回到客户的网站上进行测试,但是一旦我做到了,我一定会告诉你它是否有效。 – 2010-10-07 18:45:42

+0

昨天对客户的网站做出了明确的规定,并有机会实施此代码 - 如广告中所述,谢谢! – 2010-10-14 18:31:44

+0

@斯科特 - 太好了! – 2010-10-14 19:41:14

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); 
} 
相关问题