java生成pdf完整,无法在java中生成PDF文件的完整数据

I have a jtable that i'm using to display some data. Say I have around 200 rows of data. I am able to generate the pdf, by using the iText library, but the problem i'm facing is that all the rows aren't generated. How can I add a new page dynamically so that I generate all the rows? Kindly have a look at the code below and please help me out here.

Document doc = new Document(new Rectangle(1350, 1450));

PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 800, 0.50f);

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

java.util.Date date = new java.util.Date();

String generatedDate = formatter.format(date);try {

PdfWriter writer;

writer = PdfWriter.getInstance(doc, new FileOutputStream(save_pdf.getSelectedFile().getAbsoluteFile() + ".pdf"));

writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);

doc.open();

PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, writer);

writer.setOpenAction(action);

doc.add(new Paragraph("REPORTS", f));

doc.add(new Paragraph("Document Generated On - " + generatedDate, f));

PdfContentByte cb = writer.getDirectContent();

cb.saveState();

Graphics2D g2;

g2 = cb.createGraphics(1350, 1275);

Shape oldClip = g2.getClip();

g2.clipRect(0, 0, 1350, 1275);//1275

table1.print(g2);

JTableHeader h = table1.getTableHeader();

h.print(g2);

g2.setClip(oldClip);

writer.newPage();

g2.dispose();

cb.restoreState();

} catch (DocumentException | FileNotFoundException e) {

}

doc.close();

解决方案

Okay, so this is pretty basic example...

JTable supports printing already, through it's various print methods, basically this boils down to getting an instance of the JTable Printable interface and passing it off to the print API, which needs a Graphics2D context to paint to...

Oddly enough, you have a Graphics2D context, so the trick here is to "act" as the printer and call the JTable's Printable print method yourself...

4Su1F.jpg

DefaultTableModel model = new DefaultTableModel(0, 10);

for (int row = 0; row < 400; row++) {

Object[] values = new Object[10];

for (int col = 0; col < 10; col++) {

values[col] = ((char) ('A' + col)) + "x" + row;

}

model.addRow(values);

}

JTable table = new JTable(model);

table.setSize(table.getPreferredSize());

JTableHeader tableHeader = table.getTableHeader();

tableHeader.setSize(tableHeader.getPreferredSize());

Document doc = new Document(new Rectangle(1350, 1450));

PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 800, 0.50f);

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

java.util.Date date = new java.util.Date();

String generatedDate = formatter.format(date);

Paper paper = new Paper();

paper.setSize(1350, 1450);

paper.setImageableArea(10, 10, 1350 - 20, 1450 - 20);

PageFormat pf = new PageFormat();

pf.setPaper(paper);

Printable printable = table.getPrintable(JTable.PrintMode.NORMAL, null, null);

try {

PdfWriter writer;

writer = PdfWriter.getInstance(doc, new FileOutputStream("test.pdf"));

writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);

doc.open();

// Use this to "test" if there is page

// available for printing, otherwise it prints

// a empty page and I can't figure out

// how to remove it :P

BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

Graphics2D g = img.createGraphics();

Font f = new Font(Font.TIMES_ROMAN, 12f);

int page = 0;

int result = Printable.NO_SUCH_PAGE;

PdfContentByte cb = writer.getDirectContent();

do {

result = printable.print(g, pf, page);

if (result == Printable.PAGE_EXISTS) {

cb.saveState();

Graphics2D g2 = cb.createGraphics(1350, 1450);

System.out.println(page);

result = printable.print(g2, pf, page);

g2.dispose();

cb.restoreState();

doc.add(new Paragraph("REPORTS", f));

doc.add(new Paragraph("Document Generated On - " + generatedDate, f));

page++;

doc.newPage();

}

} while (result == Printable.PAGE_EXISTS);

g.dispose();

} catch (DocumentException | PrinterException | FileNotFoundException e) {

e.printStackTrace();

} finally {

doc.close();

}

Now, I need to display the table in order to get the row headers to display, but there might be another work around for this.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值