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...
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.
PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, writer);
Here you use only one page to write the content. So iText can not write the content in next page. gotoLocalPage method only acted in only specific page. Please refer the below link and change your code...
Just gothrough this page