利用itext创建pdf表格文件(java)

 

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();

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值