iText无内边框表格

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);
//新建Table1列
PdfPTable table = new PdfPTable(1);
//定义每列的宽度
table.setWidths(new int[]{240});

//新建table t1 5 列
PdfPTable t1 = new PdfPTable(5);
t1.setWidths(new int[]{60,30,60,30,60});
t1.getDefaultCell().setBorder(PdfPCell.NO_BORDER);

PdfPCell head = new PdfPCell(new Phrase("状态:",fNormal));
head.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
head.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
head.setBorder(PdfPCell.NO_BORDER);
head.setMinimumHeight(30);
t1.addCell(head);
/* head.setBorderWidthRight(0f);
head.setBorderColorRight(BaseColor.WHITE);
head.setBorder(Rectangle.BOTTOM | Rectangle.LEFT | Rectangle.TOP );*/

// head.setCellEvent(new BorderCell(3));
// table.addCell(head);
PdfPCell c0 = new PdfPCell();
c0.setCellEvent(new CheckboxCellEvent("1", true));
c0.setFixedHeight(10);
c0.setBorder(PdfPCell.NO_BORDER);
c0.setPadding(10);


PdfPCell c1 = new PdfPCell(c0);
// c1.setIndent(15);
// c1.setPadding(10);
c1.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
c1.setBorder(PdfPCell.NO_BORDER);
t1.addCell(c1);

PdfPCell c11 = new PdfPCell(new Phrase("是",fNormal));
c11.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
c11.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
c11.setMinimumHeight(30);
c11.setBorder(PdfPCell.NO_BORDER);
t1.addCell(c11);

PdfPCell c2 = new PdfPCell();
c2.setCellEvent(new CheckboxCellEvent("0", false));
c2.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
c2.setBorder(PdfPCell.NO_BORDER);
c2.setFixedHeight(10);
c2.setMinimumHeight(30);
t1.addCell(c2);

PdfPCell c21 = new PdfPCell(new Phrase("否",fNormal));
c21.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
c21.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
c21.setBorder(PdfPCell.NO_BORDER);
c21.setMinimumHeight(30);
t1.addCell(c21);
//将t1嵌入到 列中
PdfPCell checkbox = new PdfPCell(t1);
table.addCell(checkbox);
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);
}
}
}
class BorderCell implements PdfPCellEvent {
private int border = 0;
public BorderCell(int border) {
this.border = border;
}
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.saveState();
canvas.setLineDash(0, 4, 2);
if ((border & PdfPCell.TOP) == PdfPCell.TOP) {
canvas.moveTo(position.getRight(), position.getTop());
canvas.lineTo(position.getLeft(), position.getTop());
}
if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) {
canvas.moveTo(position.getRight(), position.getBottom());
canvas.lineTo(position.getLeft(), position.getBottom());
}
if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) {
canvas.moveTo(position.getRight(), position.getTop());
canvas.lineTo(position.getRight(), position.getBottom());
}
if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) {
canvas.moveTo(position.getLeft(), position.getTop());
canvas.lineTo(position.getLeft(), position.getBottom());
}
canvas.stroke();
canvas.restoreState();
}
}
}


结果如下:

[img]http://dl2.iteye.com/upload/attachment/0118/1040/4cdfa589-e0a7-3640-abfb-0a5844d34d01.png[/img]

你可能感兴趣的:(iText,java)