iText制作PDFCheckbox实例

package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import test.CheckboxCell2.CheckboxCellEvent;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfAppearance;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;

public class TestCheckBox {
	public static final String DEST = "E:\\testCheck.pdf";
	 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TestCheckBox().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        //定义字体
        BaseFont bfChinese = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false );  
		Font fNormal = new Font(bfChinese, 11, Font.NORMAL);
		//新建Table两列
        PdfPTable table = new PdfPTable(2);
        //定义每列的宽度
        table.setWidths(new int[]{100,200});
        //第一列start
        PdfPCell head = new PdfPCell(new Phrase("状态:",fNormal));
        head.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        head.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        head.setMinimumHeight(30);
        table.addCell(head);
        //第一列end
        //第二列start
        //新建table t1
        PdfPTable t1 = new PdfPTable(4);
        t1.setWidths(new int[]{30,60,30,60});
        
        PdfPCell c0 = new PdfPCell();
        c0.setCellEvent(new CheckboxCellEvent("1", true));
        c0.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c0.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c0.setFixedHeight(10);
        c0.setPadding(10);
       
        
        PdfPCell c1 = new PdfPCell(c0);
//        c1.setIndent(15);
//        c1.setPadding(10);
        c1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        t1.addCell(c1);
        
        PdfPCell c11 = new PdfPCell(new Phrase("是",fNormal));
        c11.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c11.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c11.setMinimumHeight(30);
        t1.addCell(c11);
        
        PdfPCell c2 = new PdfPCell();
        c2.setCellEvent(new CheckboxCellEvent("0", false));
        c2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c2.setFixedHeight(10);
        c2.setMinimumHeight(30);
        t1.addCell(c2);
        
        PdfPCell c21 = new PdfPCell(new Phrase("否",fNormal));
        c21.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        c21.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c21.setMinimumHeight(30);
        t1.addCell(c21);
        //将t1嵌入到 第二列中
        PdfPCell checkbox = new PdfPCell(t1);
        table.addCell(checkbox);
      //第二列end
        document.add(table);
        document.close();
        System.out.println("===========:end");
    }
 
    class CheckboxCellEvent implements PdfPCellEvent {
        // The name of the check box field
        protected String name;
        protected boolean flag ;
        // We create a cell event
        public CheckboxCellEvent(String name, boolean flag) {
            this.name = name;
            this.flag = flag;
        }
        // We create and add the check box field
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfWriter writer = canvases[0].getPdfWriter(); 
            // define the coordinates of the middle
            float x = (position.getLeft() + position.getRight()) / 2;
            float y = (position.getTop() + position.getBottom()) / 2;
            // define the position of a check box that measures 20 by 20
            //画勾
            Rectangle rect = new Rectangle(x - 5, y - 5, x + 5, y + 5);
            RadioCheckField checkbox = new RadioCheckField(writer, rect, name, "On");
            checkbox.setCheckType(RadioCheckField.TYPE_CHECK);
            
            if(flag){
            	//设置为选中状态
            	checkbox.setChecked(true);
            }
            else{
            	checkbox.setChecked(false);
            }
            //画框
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.setColorStroke(BaseColor.BLACK);
            canvas.setLineDash(1f);
            canvas.rectangle(x - 10, y - 10, 20,20);
            canvas.stroke();
           
            try {
                writer.addAnnotation(checkbox.getCheckField());
            } catch (Exception e) {
                throw new ExceptionConverter(e);
            }
        }
    }
	}

结果如下:


你可能感兴趣的:(iText制作PDFCheckbox实例)