我有一个jtable我正在用来显示一些数据。假设我有大约200行数据。我能够pdf通过使用生成iText library, 但是我面临的问题是未生成所有行。如何动态添加,new page以便生成所有行?请查看下面的代码,请在这里帮助我。
jtable
pdf
new page
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();
好的,这是一个非常基本的示例……
JTable通过各种print方法,已经支持打印了,基本上,这归结为获取JTable Printable 接口实例并将其传递给打印API,该API需要Graphics2D上下文来绘制……
奇怪的是,您具有Graphics2D上下文,因此这里的技巧是“充当”打印机并自己调用JTable的Printable print方法…
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(); }
现在,我需要显示表以便显示行标题,但是可能还有其他解决方法。