2010-10-06 53 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); 
} 
相關問題