2016-08-07 1 views
1

iTextSharp를 사용하면 PDF 양식에있는 양식 필드를 모두 검색 할 수 있습니다. Adobe Acrobat Reader를 사용하여 모든 필드에 위치 속성이있는 PDF를 편집하고 있습니다. PDF 필드가 양식에있는 위치를 나타냅니다.PDF 형식 필드 위치 검색 Itext

제 질문은, 저 값을 읽을 수 있습니까?

예를 들어 PDF 양식에 양식 필드 이름이있는 경우 왼쪽 0.5 인치, 오른쪽 2.5 인치, 위쪽 2 인치, 아래쪽 2 인치와 같은이 필드의 위치 값을 얻을 수 있습니까?

는 지금은 아래의 코드를 사용하여 양식 필드를 검색 해요 :

string pdfTemplate = @"D:\abc.pdf"; 
PdfReader reader = new PdfReader(pdfTemplate); 

var fields = reader.AcroFields; 

int ffRadio = 1 << 15;  //Per spec, 16th bit is radio button 
int ffPushbutton = 1 << 16; //17th bit is push button 
int ff; 

//Loop through each field 
foreach (var f in fields.Fields) 
{ 
    String type = ""; 
    String name = f.Key.ToString(); 
    String value = fields.GetField(f.Key); 

    //Get the widgets for the field (note, this could return more than 1, this should be checked) 
    PdfDictionary w = f.Value.GetWidget(0); 

    //See if it is a button-like object (/Ft == /Btn) 
    if (!w.Contains(PdfName.FT) || !w.Get(PdfName.FT).Equals(PdfName.BTN)) 
    { 
     type = "T"; 
    } 
    else 
    { 
     //Get the optional field flags, if they don't exist then just use zero 
     ff = (w.Contains(PdfName.FF) ? w.GetAsNumber(PdfName.FF).IntValue : 0); 

     if ((ff & ffRadio) == ffRadio) 
     { 
      //Is Radio 
      type = "R"; 
     } 
     else if (((ff & ffRadio) != ffRadio) && ((ff & ffPushbutton) != ffPushbutton)) 
     { 
      //Is Checkbox 
      type = "C"; 
     } 
     else 
     { 
      //Regular button 
      type = "B"; 
     } 
    } 

    //MessageBox.Show(type + "=>" + name + "=>" + value); 
    FormFields fld = new FormFields(name, type, value, "inputfield" +form_fields.Count); 

    form_fields.Add(fld); 

    if (type.Equals("T")) 
     addContent(form_fields.Count); 
} 

답변

2

나는 Find field absolute position and dimension by acrokey의 중복으로이 질문을 닫으려고했지만 그 자바 대답, 대부분의 개발자는 아무런 문제가 없지만 Java를 C#으로 변환하면 일부 개발자는 C# 대답을 얻는 데 도움이 될 수 있습니다.

PDF의 필드는 위젯 주석을 사용하여 시각화됩니다. 하나의 필드는 다른 주석과 대응할 수 있습니다. 예를 들어, 모든 페이지에서 시각화 된 name이라는 필드를 가질 수 있습니다. 이 경우이 필드의 값은 모든 페이지에 표시됩니다.

위젯 주석에 대해 하나씩 여러 위치의 목록을 반환하는 GetFieldPositions() 메소드가 있습니다.

내가 질문에 대한 답변에서 복사하는 코드입니다 iTextSharp GetFieldPositions to SetSimpleColumn

IList<AcroFields.FieldPosition> fieldPositions = fields.GetFieldPositions("fieldNameInThePDF"); 
if (fieldPositions == null || fieldPositions.Count <= 0) throw new ApplicationException("Error locating field"); 
AcroFields.FieldPosition fieldPosition = fieldPositions[0]; 
left = fieldPosition.position.Left; 
right = fieldPosition.position.Right; 
top = fieldPosition.position.Top; 
bottom = fieldPosition.position.Bottom; 

하나 개의 필드는 다음 하나의 위젯 주석에 해당하는 경우 left, right, topbottom 바로 당신에게 왼쪽을 줄 것이다, 필드의 위, 아래 좌표. 필드 너비는 다음과 같이 계산할 수 있습니다. right - left; 높이 : top - bottom. 이 값은 사용자 단위으로 표시됩니다. 기본적으로 1 인치당 72 개의 사용자 단위가 있습니다.

문서에 둘 이상의 페이지가있는 경우 fieldPosition.page은 필드를 찾을 페이지 번호를 제공합니다.