2014-11-29 2 views
0

iText를 사용하여 PDF의 기존 AcroForm 필드에 데이터를 채 웁니다.PDF에 새 AcroForm 필드 추가

이제 새로운 AcroForm 필드를 PDF에 추가하는 솔루션을 찾고 있습니다. iText로 가능합니까? 그렇다면 어떻게해야합니까?

답변

2

이 내용은 official documentation, 더 구체적으로 SubmitForm 예제에 설명되어 있습니다. iText와 같은 도구를 사용할 때는 공식 문서를 먼저 읽어야합니다 .-)

어쨌든 AddField이라는 간단한 예제를 작성했습니다. new Rectangle(36, 700, 72, 730)에 의해 정의 된 특정 위치에 버튼 필드를 추가합니다. 당신이 볼 수 있듯이

public void manipulatePdf(String src, String dest) throws DocumentException, IOException { 
    PdfReader reader = new PdfReader(src); 
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); 
    PushbuttonField button = new PushbuttonField(
     stamper.getWriter(), new Rectangle(36, 700, 72, 730), "post"); 
    button.setText("POST"); 
    button.setBackgroundColor(new GrayColor(0.7f)); 
    button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT); 
    PdfFormField submit = button.getField(); 
    submit.setAction(PdfAction.createSubmitForm(
     "http://itextpdf.com:8180/book/request", null, 
     PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES)); 
    stamper.addAnnotation(submit, 1); 
    stamper.close(); 
} 

}

, 당신은 (예 : PushbuttonField, TextField, ... 사용하는 헬퍼 클래스)를 PdfFormField 객체를 만든 다음 필드를 추가 PdfStamperaddAnnotation() 방법을 사용할 필요가 특정 페이지로 이동합니다.