利用itext创建pdf表格文件(java)
2009-07-24 15:44
495 查看
public class GeneralPdf extends PdfPageEventHelper {
public final boolean printPdf() {
Document doc = null;
try {
Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();
doc = new Document(rectPageSize, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.getInstance(doc, "c://pdffile.pdf");
writer.setPageEvent(new generalContent());
doc.open();
generalContent();
doc.add(table);
doc.close();
writer.close();
} catch (Exception e) {
e.getMessage());
}
}
public final void onCloseDocument(final PdfWriter writer, final Document arg1) {
tpl.beginText();
tpl.setFontAndSize(helv, 12);
tpl.setTextMatrix(0, 0);
tpl.showText("" + (writer.getPageNumber() - 1));
tpl.endText();
}
public final void onEndPage(final PdfWriter writer, final Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
String text = writer.getPageNumber() + "/";
float textSize = helv.getWidthPoint(text, 12);
float textBase = document.bottom();
cb.beginText();
cb.setFontAndSize(helv, 12);
cb.setTextMatrix(document.getPageSize().getWidth() / 2, textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.getPageSize().getWidth() / 2 + textSize, textBase);
cb.saveState();
}
public final void onOpenDocument(final PdfWriter writer, final Document arg1) {
try {
// 初期化template
tpl = writer.getDirectContent().createTemplate(100, 100);
// 创建日文字体
helv = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-HW-H", false);
} catch (Exception e) {
e.getMessage();
}
}
// PDF文件内容
protected static void generalContent() {
PdfPCell cell = null;
PdfPTable bodyTable = null;
PdfPCell bodyCell = null;
PdfPCellEventExtBottomDash cellBottomDash = new PdfPCellEventExtBottomDash();
PdfPCellEventExtRightDash cellRightDash = new PdfPCellEventExtRightDash();
...
bodyTable = new PdfPTable(IWIDTHS);
bodyCell = new PdfPCell(new Paragraph("text", fontContent));
bodyCell.setHorizontalAlignment(Element.ALIGN_CENTER);
bodyCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
bodyCell.setBorder(PdfPCell.NO_BORDER);
bodyCell.setCellEvent(cellBottomDash);
bodyCell.setBorder(PdfPCell.RIGHT);
bodyCell.setFixedHeight(CELLHEIGHT);
bodyTable.addCell(bodyCell);
...
}
}
// 画表格虚线1
public class PdfPCellEventExtBottomDash implements PdfPCellEvent {
public final void cellLayout(final PdfPCell cell, final Rectangle position,
final PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
cb.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
cb.setLineDash(2, 0);
cb.setLineWidth(0.5f);
cb.moveTo(position.getLeft(), position.getBottom());
cb.lineTo(position.getRight(), position.getBottom());
cb.stroke();
}
}
// 画表格虚线2
public class PdfPCellEventExtRightDash implements PdfPCellEvent {
public final void cellLayout(final PdfPCell cell, final Rectangle position,
final PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
cb.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
cb.setLineDash(2, 0);
cb.setLineWidth(0.5f);
cb.moveTo(position.getRight(), position.getTop());
cb.lineTo(position.getRight(), position.getBottom());
cb.stroke();
}
}
public final boolean printPdf() {
Document doc = null;
try {
Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();
doc = new Document(rectPageSize, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.getInstance(doc, "c://pdffile.pdf");
writer.setPageEvent(new generalContent());
doc.open();
generalContent();
doc.add(table);
doc.close();
writer.close();
} catch (Exception e) {
e.getMessage());
}
}
public final void onCloseDocument(final PdfWriter writer, final Document arg1) {
tpl.beginText();
tpl.setFontAndSize(helv, 12);
tpl.setTextMatrix(0, 0);
tpl.showText("" + (writer.getPageNumber() - 1));
tpl.endText();
}
public final void onEndPage(final PdfWriter writer, final Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
String text = writer.getPageNumber() + "/";
float textSize = helv.getWidthPoint(text, 12);
float textBase = document.bottom();
cb.beginText();
cb.setFontAndSize(helv, 12);
cb.setTextMatrix(document.getPageSize().getWidth() / 2, textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.getPageSize().getWidth() / 2 + textSize, textBase);
cb.saveState();
}
public final void onOpenDocument(final PdfWriter writer, final Document arg1) {
try {
// 初期化template
tpl = writer.getDirectContent().createTemplate(100, 100);
// 创建日文字体
helv = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-HW-H", false);
} catch (Exception e) {
e.getMessage();
}
}
// PDF文件内容
protected static void generalContent() {
PdfPCell cell = null;
PdfPTable bodyTable = null;
PdfPCell bodyCell = null;
PdfPCellEventExtBottomDash cellBottomDash = new PdfPCellEventExtBottomDash();
PdfPCellEventExtRightDash cellRightDash = new PdfPCellEventExtRightDash();
...
bodyTable = new PdfPTable(IWIDTHS);
bodyCell = new PdfPCell(new Paragraph("text", fontContent));
bodyCell.setHorizontalAlignment(Element.ALIGN_CENTER);
bodyCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
bodyCell.setBorder(PdfPCell.NO_BORDER);
bodyCell.setCellEvent(cellBottomDash);
bodyCell.setBorder(PdfPCell.RIGHT);
bodyCell.setFixedHeight(CELLHEIGHT);
bodyTable.addCell(bodyCell);
...
}
}
// 画表格虚线1
public class PdfPCellEventExtBottomDash implements PdfPCellEvent {
public final void cellLayout(final PdfPCell cell, final Rectangle position,
final PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
cb.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
cb.setLineDash(2, 0);
cb.setLineWidth(0.5f);
cb.moveTo(position.getLeft(), position.getBottom());
cb.lineTo(position.getRight(), position.getBottom());
cb.stroke();
}
}
// 画表格虚线2
public class PdfPCellEventExtRightDash implements PdfPCellEvent {
public final void cellLayout(final PdfPCell cell, final Rectangle position,
final PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
cb.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
cb.setLineDash(2, 0);
cb.setLineWidth(0.5f);
cb.moveTo(position.getRight(), position.getTop());
cb.lineTo(position.getRight(), position.getBottom());
cb.stroke();
}
}
相关文章推荐
- 利用java操作Itext创建pdf文件
- 从 Java 应用程序动态生成 PDF 文件一个简化了 PDF 创建过程的开源库 iText 的分步指南
- 利用itext创建PDF表格
- 如何利用iText在java程序中生成PDF文件
- 利用itext操作pdf从数据库导出大量数据--创建PDF表格(三)
- 利用java的iText操作PDF文件
- java 通过itext 创建pdf 添加,插入 表格,html格式
- 利用itext操作pdf从数据库导出大量数据--创建PDF表格(三)
- 如何利用iText在java程序中生成PDF文件
- 如何利用iText在java程序中生成PDF文件
- Java开源工具iText在PDF文件中创建表格
- JAVA按模版导出PDF文件,含条码,二维码,表格
- Java 利用itext填写pdf模板并导出(亲自试过,没问题,并附上itextpdf-5.5.8.jar包和itext-asian.jar包))
- Java 实现 pdf文件添加 图片水印 itextpdf 5.5.2
- java利用itext编辑pdf
- java利用itext编辑pdf
- 利用java创建和读取excel文件
- iText创建pdf文件里,解决ClassNotFoundException错误
- java使用iText生成pdf表格
- 使用iText库创建PDF文件