2017-01-27 5 views
0

iText 7에서 생성 된 양식이있는 PDF가 있습니다. 그러나 양식을 채우고 인쇄하려고하면 양식 값이 표시되지 않습니다. 양식 개요 만 보여줍니다. 인쇄 할 때 값을 표시하는 양식 필드는 어떻게 생성합니까?왜 iText 7 양식 필드 값이 인쇄되지 않습니까?

샘플 양식 생성기 :

import java.io.FileNotFoundException; 
import java.io.IOException; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import com.itextpdf.forms.PdfAcroForm; 
import com.itextpdf.forms.fields.PdfFormField; 
import com.itextpdf.forms.fields.PdfTextFormField; 
import com.itextpdf.kernel.font.PdfFont; 
import com.itextpdf.kernel.font.PdfFontFactory; 
import com.itextpdf.kernel.geom.PageSize; 
import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 
import com.itextpdf.layout.Document; 
import com.itextpdf.layout.border.Border; 
import com.itextpdf.layout.element.Cell; 
import com.itextpdf.layout.element.Table; 
import com.itextpdf.layout.renderer.CellRenderer; 
import com.itextpdf.layout.renderer.DrawContext; 

/** 
* @author Lucas Vander Wal 
* 
*/ 
public final class TestForm { 

    private static final Logger log = LoggerFactory.getLogger(TestForm.class); 

    private static final PageSize LETTER_SIZE = new PageSize(612, 792), 
      LETTER_SIZE_LANDSCAPE = LETTER_SIZE.rotate(); 

    private static final PdfFont FONT; 
    static { 
     try { 
      FONT = PdfFontFactory.createFont(); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    private static final float FONT_SIZE_12 = 12; 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     log.info("Running pdf generation..."); 
     new TestForm().generate("formTest.pdf"); 
     log.info("Done with pdf generation."); 
    } 

    private Document doc; 
    private PdfDocument pdfDoc; 
    private PdfAcroForm form; 

    public TestForm() { 
    } 

    /** 
    * Generates the timesheet pdf and saves it to the given file location 
    * 
    * @param outputFile 
    */ 
    public void generate(String outputFile) { 
     try { 
      pdfDoc = new PdfDocument(new PdfWriter(outputFile)); 
      doc = new Document(pdfDoc, LETTER_SIZE_LANDSCAPE); 
      // set document properties 
      doc.setFontSize(10); 
      float marginSize = doc.getTopMargin(); 
      doc.setMargins(marginSize/2, marginSize, marginSize/2, marginSize); 

      form = PdfAcroForm.getAcroForm(pdfDoc, true); 

      // build the form 
      buildUserInfo(); 
      // close the document 
      doc.close(); 

     } catch (FileNotFoundException e) { 
      log.warn("Unable to save to file: " + outputFile, e); 
     } 

    } 

    private void buildUserInfo() { 
     // build the user info table 
     Table userTable = new Table(4).setBorder(Border.NO_BORDER).setWidthPercent(100).setMargin(0).setPadding(0); 
     // add 4 text entry fields 
     this.addFieldToTable(userTable, "nameEmployee", "Name of Employee:"); 

     // add the table to the document 
     this.doc.add(userTable); 

    } 

    private void addFieldToTable(Table t, String fieldName, String fieldLabel) { 
     t.addCell(new Cell().add(fieldLabel).setPadding(5)); 
     Cell cell = new Cell().setPadding(5); 
     cell.setNextRenderer(new CellRenderer(cell) { 

      @Override 
      public void draw(DrawContext drawContext) { 
       super.draw(drawContext); 
       PdfTextFormField field = PdfFormField.createText(
         drawContext.getDocument(), getOccupiedAreaBBox().decreaseHeight(5), fieldName, "", 
         FONT, FONT_SIZE_12); 
       form.addField(field); 
      } 

     }); 
     t.addCell(cell); 
    } 
} 

답변

0

PDF 양식 필드는 인쇄 할 수있는 기능에 영향을 미치는 가시성 설정이 있습니다. 자세한 내용은 this Adobe page을 참조하십시오.

PdfFormField 개체에서 setVisibility() 메서드를 사용하여 양식 필드를 '가시성'으로 설정해야합니다. This iText 7 documentation은 양식을 'HIDDEN', 'VISIBLE_BUT_DOES_NOT_PRINT'및 'HIDDEN_BUT_PRINTABLE'로 설정하는 방법을 설명하지만 표시 설정은 언급하지 않으며 실제로 PdfFormField의 정적 옵션 중 하나가 표시되지 않습니다.

위에 나열된 세 개의 값은 정수 1, 2 및 3에 해당하며 직감에 대해서는 가시성을 0으로 설정하고 인쇄 할 때 양식 필드가 표시됩니다!

요약하면 인쇄 할 수있는 양식 필드를 만들려면 PdfFormField.setVisibility(0)을 호출하십시오.

+1

PdfFormField.VISIBLE은 7.0.2 이상에서 옵션이 될 수 있습니다 (이미 7.0.1에있을 수 있습니다. 확인하지 않았습니다). 귀하의 솔루션과 직감에 관해서는 0은 실제로 VISIBLE을 따르지 않으며, VISIBLE은 기본 옵션이며 4 이상은 역시 작동해야합니다. –

+0

@SamuelHuylebroeck 의견에 감사드립니다. '0이 VISIBLE에 맞지 않는다'는 것은 무엇을 의미합니까? 양식 필드 값이 기본적으로 인쇄되지 않으므로 기본값은 VISIBLE이 아니지만 가시성을 0으로 설정하면됩니다. 4 이상을 시도하지는 않았지만 기회를 얻었을 때 결과를 게시 할 것입니다. – lucasvw