2017-01-27 10 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()メソッドを使用して、フォームフィールドを 'VISIBLE'に設定する必要があります。 This iText 7 documentationはフォームを 'HIDDEN'、 'VISIBLE_BUT_DOES_NOT_PRINT'、および 'HIDDEN_BUT_PRINTABLE'に設定する方法を説明していますが、設定は表示されていません。実際、PdfFormFieldの静的オプションの1つではありません。

上記の3つの値は、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

関連する問題