Tables and fonts
package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCellEvent; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPTableEvent; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class DottedLineCell { public static final String DEST = "results/tables/dotted_line_cell.pdf"; class DottedCells implements PdfPTableEvent { public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); float llx = widths[0][0]; float urx = widths[0][widths.length]; for (int i = 0; i < heights.length; i++) { canvas.moveTo(llx, heights[i]); canvas.lineTo(urx, heights[i]); } for (int i = 0; i < widths.length; i++) { for (int j = 0; j < widths[i].length; j++) { canvas.moveTo(widths[i][j], heights[i]); canvas.lineTo(widths[i][j], heights[i+1]); } } canvas.stroke(); } } class DottedCell implements PdfPCellEvent { public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); canvas.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight()); canvas.stroke(); } } public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new DottedLineCell().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { DottedLineCell app = new DottedLineCell(); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); document.add(new Paragraph("Table event")); PdfPTable table = new PdfPTable(3); table.setTableEvent(app.new DottedCells()); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.addCell("A1"); table.addCell("A2"); table.addCell("A3"); table.addCell("B1"); table.addCell("B2"); table.addCell("B3"); table.addCell("C1"); table.addCell("C2"); table.addCell("C3"); document.add(table); document.add(new Paragraph("Cell event")); table = new PdfPTable(1); PdfPCell cell = new PdfPCell(new Phrase("Test")); cell.setCellEvent(app.new DottedCell()); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); document.add(table); document.close(); } }
/** * This example was written by Bruno Lowagie in answer to the following questions: * http://stackoverflow.com/questions/30106862/left-and-right-top-round-corner-for-rectangelroundrectangle */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCellEvent; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class RoundedCorners { public static final String DEST = "results/tables/rounded_corners.pdf"; class SpecialRoundedCell implements PdfPCellEvent { public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; float llx = position.getLeft() + 2; float lly = position.getBottom() + 2; float urx = position.getRight() - 2; float ury = position.getTop() - 2; float r = 4; float b = 0.4477f; canvas.moveTo(llx, lly); canvas.lineTo(urx, lly); canvas.lineTo(urx, ury - r); canvas.curveTo(urx, ury - r * b, urx - r * b, ury, urx - r, ury); canvas.lineTo(llx + r, ury); canvas.curveTo(llx + r * b, ury, llx, ury - r * b, llx, ury - r); canvas.lineTo(llx, lly); canvas.stroke(); } } public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new RoundedCorners().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(3); PdfPCell cell = getCell("These cells have rounded borders at the top."); table.addCell(cell); cell = getCell("These cells aren't rounded at the bottom."); table.addCell(cell); cell = getCell("A custom cell event was used to achieve this."); table.addCell(cell); document.add(table); document.close(); } public PdfPCell getCell(String content) { PdfPCell cell = new PdfPCell(new Phrase(content)); cell.setCellEvent(new SpecialRoundedCell()); cell.setPadding(5); cell.setBorder(PdfPCell.NO_BORDER); return cell; } }
package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCellEvent; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPTableEvent; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class DottedLineHeader { public static final String DEST = "results/tables/dotted_line_header.pdf"; class DottedHeader implements PdfPTableEvent { public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); float x1 = widths[0][0]; float x2 = widths[0][widths.length]; canvas.moveTo(x1, heights[0]); canvas.lineTo(x2, heights[0]); canvas.moveTo(x1, heights[headerRows]); canvas.lineTo(x2, heights[headerRows]); canvas.stroke(); } } class DottedCell implements PdfPCellEvent { public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); canvas.moveTo(position.getLeft(), position.getTop()); canvas.lineTo(position.getRight(), position.getTop()); canvas.moveTo(position.getLeft(), position.getBottom()); canvas.lineTo(position.getRight(), position.getBottom()); canvas.stroke(); } } public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new DottedLineHeader().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); document.add(new Paragraph("Table event")); PdfPTable table = new PdfPTable(3); table.setTableEvent(new DottedHeader()); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.addCell("A1"); table.addCell("A2"); table.addCell("A3"); table.setHeaderRows(1); table.addCell("B1"); table.addCell("B2"); table.addCell("B3"); table.addCell("C1"); table.addCell("C2"); table.addCell("C3"); document.add(table); document.add(new Paragraph("Cell event")); table = new PdfPTable(3); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.getDefaultCell().setCellEvent(new DottedCell()); table.addCell("A1"); table.addCell("A2"); table.addCell("A3"); table.getDefaultCell().setCellEvent(null); table.addCell("B1"); table.addCell("B2"); table.addCell("B3"); table.addCell("C1"); table.addCell("C2"); table.addCell("C3"); document.add(table); document.close(); } }
/** * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPRow; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPTableEventAfterSplit; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class CustomBorder { public static final String DEST = "results/tables/custom_border.pdf"; public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point. It should result in rows that are split and rows that aren't."; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new CustomBorder().createPdf(DEST); } class BorderEvent implements PdfPTableEventAfterSplit { protected int rowCount; protected boolean bottom = true; protected boolean top = true; public void setRowCount(int rowCount) { this.rowCount = rowCount; } public void splitTable(PdfPTable table) { if (table.getRows().size() != rowCount) { bottom = false; } } public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) { if (table.getRows().size() != rowCount) { // if the table gains a row, a row was split rowCount = table.getRows().size(); top = false; } } public void tableLayout(PdfPTable table, float[][] width, float[] height, int headerRows, int rowStart, PdfContentByte[] canvas) { float widths[] = width[0]; float y1 = height[0]; float y2 = height[height.length - 1]; PdfContentByte cb = canvas[PdfPTable.LINECANVAS]; for (int i = 0; i < widths.length; i++) { cb.moveTo(widths[i], y1); cb.lineTo(widths[i], y2); } float x1 = widths[0]; float x2 = widths[widths.length - 1]; for (int i = top ? 0 : 1; i < (bottom ? height.length : height.length - 1); i++) { cb.moveTo(x1, height[i]); cb.lineTo(x2, height[i]); } cb.stroke(); cb.resetRGBColorStroke(); bottom = true; top = true; } } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(2); table.setTotalWidth(500); table.setLockedWidth(true); BorderEvent event = new BorderEvent(); table.setTableEvent(event); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.setSplitLate(false); PdfPCell cell = new PdfPCell(new Phrase(TEXT)); cell.setBorder(Rectangle.NO_BORDER); for (int i = 0; i < 60; ) { table.addCell("Cell " + (++i)); table.addCell(cell); } event.setRowCount(table.getRows().size()); document.add(table); document.close(); } }
/** * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPRow; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPTableEventAfterSplit; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class CustomBorder2 { public static final String DEST = "results/tables/custom_border2.pdf"; public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point. It should result in rows that are split and rows that aren't."; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new CustomBorder2().createPdf(DEST); } class BorderEvent implements PdfPTableEventAfterSplit { protected boolean bottom = true; protected boolean top = true; public void splitTable(PdfPTable table) { bottom = false; } public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) { top = false; } public void tableLayout(PdfPTable table, float[][] width, float[] height, int headerRows, int rowStart, PdfContentByte[] canvas) { float widths[] = width[0]; float y1 = height[0]; float y2 = height[height.length - 1]; float x1 = widths[0]; float x2 = widths[widths.length - 1]; PdfContentByte cb = canvas[PdfPTable.LINECANVAS]; cb.moveTo(x1, y1); cb.lineTo(x1, y2); cb.moveTo(x2, y1); cb.lineTo(x2, y2); if (top) { cb.moveTo(x1, y1); cb.lineTo(x2, y1); } if (bottom) { cb.moveTo(x1, y2); cb.lineTo(x2, y2); } cb.stroke(); cb.resetRGBColorStroke(); bottom = true; top = true; } } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(2); table.setTotalWidth(500); table.setLockedWidth(true); BorderEvent event = new BorderEvent(); table.setTableEvent(event); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.setSplitLate(false); PdfPCell cell = new PdfPCell(new Phrase(TEXT)); cell.setBorder(Rectangle.NO_BORDER); for (int i = 0; i < 60; ) { table.addCell("Cell " + (++i)); table.addCell(cell); } document.add(table); document.close(); } }
package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCellEvent; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class DottedLineCell2 { public static final String DEST = "results/tables/dotted_line_cell2.pdf"; class DottedCell implements PdfPCellEvent { private int border = 0; public DottedCell(int border) { this.border = border; } public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.saveState(); canvas.setLineDash(0, 4, 2); if ((border & PdfPCell.TOP) == PdfPCell.TOP) { canvas.moveTo(position.getRight(), position.getTop()); canvas.lineTo(position.getLeft(), position.getTop()); } if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) { canvas.moveTo(position.getRight(), position.getBottom()); canvas.lineTo(position.getLeft(), position.getBottom()); } if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) { canvas.moveTo(position.getRight(), position.getTop()); canvas.lineTo(position.getRight(), position.getBottom()); } if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) { canvas.moveTo(position.getLeft(), position.getTop()); canvas.lineTo(position.getLeft(), position.getBottom()); } canvas.stroke(); canvas.restoreState(); } } public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new DottedLineCell2().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table; PdfPCell cell; table = new PdfPTable(4); table.setSpacingAfter(30); cell = new PdfPCell(new Phrase("left border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedCell(PdfPCell.LEFT)); table.addCell(cell); cell = new PdfPCell(new Phrase("right border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedCell(PdfPCell.RIGHT)); table.addCell(cell); cell = new PdfPCell(new Phrase("top border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedCell(PdfPCell.TOP)); table.addCell(cell); cell = new PdfPCell(new Phrase("bottom border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedCell(PdfPCell.BOTTOM)); table.addCell(cell); document.add(table); table = new PdfPTable(4); table.setSpacingAfter(30); cell = new PdfPCell(new Phrase("left and top border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedCell(PdfPCell.LEFT | PdfPCell.TOP)); table.addCell(cell); cell = new PdfPCell(new Phrase("right and bottom border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedCell(PdfPCell.RIGHT | PdfPCell.BOTTOM)); table.addCell(cell); cell = new PdfPCell(new Phrase("no border")); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(new Phrase("full border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedCell(PdfPCell.BOX)); table.addCell(cell); document.add(table); document.close(); } }
package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCellEvent; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class CustomBorder3 { public static final String DEST = "results/tables/custom_border_3.pdf"; interface LineDash { public void applyLineDash(PdfContentByte canvas); } class SolidLine implements LineDash { public void applyLineDash(PdfContentByte canvas) { } } class DottedLine implements LineDash { public void applyLineDash(PdfContentByte canvas) { canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND); canvas.setLineDash(0, 4, 2); } } class DashedLine implements LineDash { public void applyLineDash(PdfContentByte canvas) { canvas.setLineDash(3, 3); } } class CustomBorder implements PdfPCellEvent { protected LineDash left; protected LineDash right; protected LineDash top; protected LineDash bottom; public CustomBorder(LineDash left, LineDash right, LineDash top, LineDash bottom) { this.left = left; this.right = right; this.top = top; this.bottom = bottom; } public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; if (top != null) { canvas.saveState(); top.applyLineDash(canvas); canvas.moveTo(position.getRight(), position.getTop()); canvas.lineTo(position.getLeft(), position.getTop()); canvas.stroke(); canvas.restoreState(); } if (bottom != null) { canvas.saveState(); bottom.applyLineDash(canvas); canvas.moveTo(position.getRight(), position.getBottom()); canvas.lineTo(position.getLeft(), position.getBottom()); canvas.stroke(); canvas.restoreState(); } if (right != null) { canvas.saveState(); right.applyLineDash(canvas); canvas.moveTo(position.getRight(), position.getTop()); canvas.lineTo(position.getRight(), position.getBottom()); canvas.stroke(); canvas.restoreState(); } if (left != null) { canvas.saveState(); left.applyLineDash(canvas); canvas.moveTo(position.getLeft(), position.getTop()); canvas.lineTo(position.getLeft(), position.getBottom()); canvas.stroke(); canvas.restoreState(); } } } public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new CustomBorder3().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table; PdfPCell cell; LineDash solid = new SolidLine(); LineDash dotted = new DottedLine(); LineDash dashed = new DashedLine(); table = new PdfPTable(4); table.setSpacingAfter(30); cell = new PdfPCell(new Phrase("dotted left border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CustomBorder(dotted, null, null, null)); table.addCell(cell); cell = new PdfPCell(new Phrase("solid right border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CustomBorder(null, solid, null, null)); table.addCell(cell); cell = new PdfPCell(new Phrase("dashed top border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CustomBorder(null, null, dashed, null)); table.addCell(cell); cell = new PdfPCell(new Phrase("bottom border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CustomBorder(null, null, null, solid)); table.addCell(cell); document.add(table); table = new PdfPTable(4); table.setSpacingAfter(30); cell = new PdfPCell(new Phrase("dotted left and solid top border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CustomBorder(dotted, null, solid, null)); table.addCell(cell); cell = new PdfPCell(new Phrase("dashed right and dashed bottom border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CustomBorder(null, dashed, null, dashed)); table.addCell(cell); cell = new PdfPCell(new Phrase("no border")); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(new Phrase("full solid border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CustomBorder(solid, solid, solid, solid)); table.addCell(cell); document.add(table); document.close(); } }
package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCellEvent; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class CustomBorder4 { public static final String DEST = "results/tables/custom_border_4.pdf"; abstract class CustomBorder implements PdfPCellEvent { private int border = 0; public CustomBorder(int border) { this.border = border; } public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.saveState(); setLineDash(canvas); if ((border & PdfPCell.TOP) == PdfPCell.TOP) { canvas.moveTo(position.getRight(), position.getTop()); canvas.lineTo(position.getLeft(), position.getTop()); } if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) { canvas.moveTo(position.getRight(), position.getBottom()); canvas.lineTo(position.getLeft(), position.getBottom()); } if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) { canvas.moveTo(position.getRight(), position.getTop()); canvas.lineTo(position.getRight(), position.getBottom()); } if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) { canvas.moveTo(position.getLeft(), position.getTop()); canvas.lineTo(position.getLeft(), position.getBottom()); } canvas.stroke(); canvas.restoreState(); } public abstract void setLineDash(PdfContentByte canvas); } class SolidBorder extends CustomBorder { public SolidBorder(int border) { super(border); } public void setLineDash(PdfContentByte canvas) {} } class DottedBorder extends CustomBorder { public DottedBorder(int border) { super(border); } public void setLineDash(PdfContentByte canvas) { canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND); canvas.setLineDash(0, 4, 2); } } class DashedBorder extends CustomBorder { public DashedBorder(int border) { super(border); } public void setLineDash(PdfContentByte canvas) { canvas.setLineDash(3, 3); } } public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new CustomBorder4().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table; PdfPCell cell; table = new PdfPTable(4); table.setSpacingAfter(30); cell = new PdfPCell(new Phrase("dotted left border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedBorder(PdfPCell.LEFT)); table.addCell(cell); cell = new PdfPCell(new Phrase("solid right border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new SolidBorder(PdfPCell.RIGHT)); table.addCell(cell); cell = new PdfPCell(new Phrase("solid top border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new SolidBorder(PdfPCell.TOP)); table.addCell(cell); cell = new PdfPCell(new Phrase("dashed bottom border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DashedBorder(PdfPCell.BOTTOM)); table.addCell(cell); document.add(table); table = new PdfPTable(4); table.setSpacingAfter(30); cell = new PdfPCell(new Phrase("dotted left and dashed top border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedBorder(PdfPCell.LEFT)); cell.setCellEvent(new DashedBorder(PdfPCell.TOP)); table.addCell(cell); cell = new PdfPCell(new Phrase("solid right and dotted bottom border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedBorder(PdfPCell.BOTTOM)); cell.setCellEvent(new SolidBorder(PdfPCell.RIGHT)); table.addCell(cell); cell = new PdfPCell(new Phrase("no border")); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(new Phrase("full border")); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new DottedBorder(PdfPCell.LEFT | PdfPCell.RIGHT)); cell.setCellEvent(new SolidBorder(PdfPCell.TOP)); cell.setCellEvent(new DashedBorder(PdfPCell.BOTTOM)); table.addCell(cell); document.add(table); document.close(); } }
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/35340003 */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPTableEvent; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; /** * @author Bruno Lowagie (iText Software) */ @WrapToTest public class TableBorder { public static final String DEST = "results/tables/table_border_outer_only.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new TableBorder().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(4); table.setTableEvent(new BorderEvent()); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); for(int aw = 0; aw < 16; aw++){ table.addCell("hi"); } document.add(table); document.close(); } public class BorderEvent implements PdfPTableEvent { public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { float width[] = widths[0]; float x1 = width[0]; float x2 = width[width.length - 1]; float y1 = heights[0]; float y2 = heights[heights.length - 1]; PdfContentByte cb = canvases[PdfPTable.LINECANVAS]; cb.rectangle(x1, y1, x2 - x1, y2 - y1); cb.stroke(); cb.resetRGBColorStroke(); } } }
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/35746651 */ package sandbox.tables; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCellEvent; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; /** * @author Bruno Lowagie (iText Software) */ @WrapToTest public class CellTitle { public static final String DEST = "results/tables/cell_title.pdf"; class Title implements PdfPCellEvent { protected String title; public Title(String title) { this.title = title; } public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { Chunk c = new Chunk(title); c.setBackground(BaseColor.LIGHT_GRAY); PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(c), position.getLeft(5), position.getTop(5), 0); } } public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new CellTitle().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(1); PdfPCell cell = getCell("The title of this cell is title 1", "title 1"); table.addCell(cell); cell = getCell("The title of this cell is title 2", "title 2"); table.addCell(cell); cell = getCell("The title of this cell is title 3", "title 3"); table.addCell(cell); document.add(table); document.close(); } public PdfPCell getCell(String content, String title) { PdfPCell cell = new PdfPCell(new Phrase(content)); cell.setCellEvent(new Title(title)); cell.setPadding(5); return cell; } }
Splitting tables
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * * @author Bruno Lowagie (iText Software) */ public class Splitting { public static final String DEST = "results/tables/splitting.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new Splitting().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); Paragraph p = new Paragraph("Test"); PdfPTable table = new PdfPTable(2); for (int i = 1; i < 6; i++) { table.addCell("key " + i); table.addCell("value " + i); } for (int i = 0; i < 40; i++) { document.add(p); } document.add(table); for (int i = 0; i < 38; i++) { document.add(p); } PdfPTable nesting = new PdfPTable(1); PdfPCell cell = new PdfPCell(table); cell.setBorder(PdfPCell.NO_BORDER); nesting.addCell(cell); document.add(nesting); document.close(); } }
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; /** * * @author Bruno Lowagie (iText Software) */ @WrapToTest public class Splitting2 { public static final String DEST = "results/tables/splitting2.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new Splitting2().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); Paragraph p = new Paragraph("Test"); PdfPTable table = new PdfPTable(2); for (int i = 1; i < 6; i++) { table.addCell("key " + i); table.addCell("value " + i); } for (int i = 0; i < 40; i++) { document.add(p); } document.add(table); for (int i = 0; i < 38; i++) { document.add(p); } table.setKeepTogether(true); document.add(table); document.close(); } }
/** * Example written by Ramesh in the context of a question on SO: * http://stackoverflow.com/questions/29345454/itext-avoid-row-splitting-in-table */ package sandbox.tables; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.draw.LineSeparator; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class TableSplitTest { public static final String DEST = "results/tables/split_test.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new TableSplitTest().createPdf(DEST); } public void createPdf(String dest) throws DocumentException, IOException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.setMargins(15, 15, 55, 35); document.open(); String[] header = new String[] { "Header1", "Header2", "Header3", "Header4", "Header5" }; String[] content = new String[] { "column 1", "column 2", "some Text in column 3", "Test data ", "column 5" }; PdfPTable table = new PdfPTable(header.length); table.setHeaderRows(1); table.setWidths(new int[] { 3, 2, 4, 3, 2 }); table.setWidthPercentage(98); table.setSpacingBefore(15); table.setSplitLate(false); for (String columnHeader : header) { PdfPCell headerCell = new PdfPCell(); headerCell.addElement(new Phrase(columnHeader, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD))); headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE); headerCell.setBorderColor(BaseColor.LIGHT_GRAY); headerCell.setPadding(8); table.addCell(headerCell); } for (int i = 0; i < 15; i++) { int j = 0; for (String text : content) { if (i == 13 && j == 3) { text = "Test data \n Test data \n Test data"; } j++; PdfPCell cell = new PdfPCell(); cell.addElement(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL))); cell.setBorderColor(BaseColor.LIGHT_GRAY); cell.setPadding(5); table.addCell(cell); } } document.add(table); document.add(new Phrase("\n")); LineSeparator separator = new LineSeparator(); separator.setPercentage(98); separator.setLineColor(BaseColor.LIGHT_GRAY); Chunk linebreak = new Chunk(separator); document.add(linebreak); for (int k = 0; k < 5; k++) { Paragraph info = new Paragraph("Some title", FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL)); info.setSpacingBefore(12f); document.add(info); table = new PdfPTable(header.length); table.setHeaderRows(1); table.setWidths(new int[] { 3, 2, 4, 3, 2 }); table.setWidthPercentage(98); table.setSpacingBefore(15); table.setSplitLate(false); for (String columnHeader : header) { PdfPCell headerCell = new PdfPCell(); headerCell.addElement(new Phrase(columnHeader, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD))); headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE); headerCell.setBorderColor(BaseColor.LIGHT_GRAY); headerCell.setPadding(8); table.addCell(headerCell); } for (String text : content) { PdfPCell cell = new PdfPCell(); cell.addElement(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL))); cell.setBorderColor(BaseColor.LIGHT_GRAY); cell.setPadding(5); table.addCell(cell); } document.add(table); separator = new LineSeparator(); separator.setPercentage(98); separator.setLineColor(BaseColor.LIGHT_GRAY); linebreak = new Chunk(separator); document.add(linebreak); } document.close(); } }
/** * Example written by Bruno Lowagie in answer to: * http://stackoverflow.com/questions/24665167/table-keeprowstogether-in-itext-5-5-1-doesnt-seem-to-work-correctly */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class SplitRowAtSpecificRow { public static final String DEST = "results/tables/split_at_row.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new SplitRowAtSpecificRow().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { PdfPTable table = new PdfPTable(1); table.setTotalWidth(550); table.setLockedWidth(true); for (int i = 0; i < 10; i++) { PdfPCell cell; if (i == 9) { cell = new PdfPCell(new Phrase("Two\nLines")); } else { cell = new PdfPCell(new Phrase(Integer.toString(i))); } table.addCell(cell); } Document document = new Document(new Rectangle(612, 242)); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); table.setSplitLate(false); table.setBreakPoints(8); document.add(table); document.close(); } }
/** * Example written by Bruno Lowagie in answer to: * http://stackoverflow.com/questions/24616920/last-row-in-itext-table-extending-when-it-shouldnt */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class SplitRowAtEndOfPage { public static final String DEST = "results/tables/split_row_at_end_of_page.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new SplitRowAtEndOfPage().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { PdfPTable table = new PdfPTable(1); table.setTotalWidth(550); table.setLockedWidth(true); for (int i = 0; i < 10; i++) { PdfPCell cell; if (i == 9) { cell = new PdfPCell(new Phrase("Two\nLines")); } else { cell = new PdfPCell(new Phrase(Integer.toString(i))); } table.addCell(cell); } Document document = new Document(new Rectangle(612, 242)); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); table.setSplitLate(false); document.add(table); document.close(); } }
Rowspan and splitting
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/35356847 */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest /** * @author Bruno Lowagie (iText Software) */ public class SplittingAndRowspan { public static final String DEST = "results/tables/splitting_and_rowspan.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new SplittingAndRowspan().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(new Rectangle(300, 150)); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); document.add(new Paragraph("Table with setSplitLate(true):")); PdfPTable table = new PdfPTable(2); table.setSpacingBefore(10); PdfPCell cell = new PdfPCell(); cell.addElement(new Paragraph("G")); cell.addElement(new Paragraph("R")); cell.addElement(new Paragraph("P")); cell.setRowspan(3); table.addCell(cell); table.addCell("row 1"); table.addCell("row 2"); table.addCell("row 3"); document.add(table); document.add(new Paragraph("Table with setSplitLate(false):")); table.setSplitLate(false); document.add(table); document.close(); } }
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/35356847 */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest /** * @author Bruno Lowagie (iText Software) */ public class SplittingNestedTable1 { public static final String DEST = "results/tables/splitting_nested_table1.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new SplittingNestedTable1().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(new Rectangle(300, 150)); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); document.add(new Paragraph("Table with setSplitLate(true):")); PdfPTable table = new PdfPTable(2); table.setSpacingBefore(10); PdfPCell cell = new PdfPCell(); cell.addElement(new Paragraph("G")); cell.addElement(new Paragraph("R")); cell.addElement(new Paragraph("O")); cell.addElement(new Paragraph("U")); cell.addElement(new Paragraph("P")); table.addCell(cell); PdfPTable inner = new PdfPTable(1); inner.addCell("row 1"); inner.addCell("row 2"); inner.addCell("row 3"); inner.addCell("row 4"); inner.addCell("row 5"); cell = new PdfPCell(inner); cell.setPadding(0); table.addCell(cell); document.add(table); document.newPage(); document.add(new Paragraph("Table with setSplitLate(false):")); table.setSplitLate(false); document.add(table); document.close(); } }
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/35356847 */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest /** * @author Bruno Lowagie (iText Software) */ public class SplittingNestedTable2 { public static final String DEST = "results/tables/splitting_nested_table2.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new SplittingNestedTable2().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(new Rectangle(300, 150)); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); document.add(new Paragraph("Table with setSplitLate(true):")); PdfPTable table = new PdfPTable(2); table.setSpacingBefore(10); PdfPCell cell = new PdfPCell( new Phrase("GROUPS")); cell.setRotation(90); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); PdfPTable inner = new PdfPTable(1); inner.addCell("row 1"); inner.addCell("row 2"); inner.addCell("row 3"); inner.addCell("row 4"); inner.addCell("row 5"); cell = new PdfPCell(inner); cell.setPadding(0); table.addCell(cell); document.add(table); document.newPage(); document.add(new Paragraph("Table with setSplitLate(false):")); table.setSplitLate(false); document.add(table); document.close(); } }
Rotating cell content
/* * Example written by Bruno Lowagie in answer to: * http://stackoverflow.com/questions/37246838 */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * @author Bruno Lowagie (iText Software) */ public class RotatedCell { public static final String DEST = "results/tables/rotated_cell.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new RotatedCell().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(8); for (int i = 0; i < 8; i++) { PdfPCell cell = new PdfPCell(new Phrase(String.format("May %s, 2016", i + 15))); cell.setRotation(90); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); } for(int i = 0; i < 16; i++){ table.addCell("hi"); } document.add(table); document.close(); } }
Repeating rows
/** * This example is written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/32582927/itext-pdfwriter-writing-table-header-if-the-few-table-rows-go-to-new-page */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; /** * @author iText */ @WrapToTest public class HeaderRowRepeated { public static final String DEST = "results/tables/repeat_header_row.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new HeaderRowRepeated().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); // table with 2 columns: PdfPTable table = new PdfPTable(2); // header row: table.addCell("Key"); table.addCell("Value"); table.setHeaderRows(1); table.setSkipFirstHeader(true); // many data rows: for (int i = 1; i < 51; i++) { table.addCell("key: " + i); table.addCell("value: " + i); } document.add(table); document.close(); } }
/** * This example is written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class RepeatLastRows { public static final String DEST = "results/tables/repeat_last_rows.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new RepeatLastRows().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); // we create a table that spans the width of the page and that has 99 rows PdfPTable table = new PdfPTable(1); table.setTotalWidth(523); for (int i = 1; i < 100; i++) table.addCell("row " + i); // we add the table at an absolute position (starting at the top of the page) PdfContentByte canvas = writer.getDirectContent(); int currentRowStart = 0; int currentRow = 0; int totalRows = table.getRows().size(); while (true) { // available height of the page float available_height = 770; // how many rows fit the height? while (available_height > 0 && currentRow < totalRows) { available_height -= table.getRowHeight(currentRow++); } // we stop as soon as all the rows are counted if (currentRow == totalRows) break; // we draw part the rows that fit the page and start a new page table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas); document.newPage(); currentRowStart = currentRow; } // if there are less than 5 rows left, we adjust the row start value if (currentRow - currentRowStart < 5) currentRowStart = currentRow - 5; // we write the remaining rows table.writeSelectedRows(currentRowStart, currentRow, 36, 806, canvas); document.close(); } }
/** * This example is written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class RepeatLastRows2 { public static final String DEST = "results/tables/repeat_last_rows2.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new RepeatLastRows2().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); // we create a table that spans the width of the page and that has 99 rows PdfPTable table = new PdfPTable(1); table.setTotalWidth(523); for (int i = 1; i < 100; i++) table.addCell("row " + i); // we add the table at an absolute position (starting at the top of the page) PdfContentByte canvas = writer.getDirectContent(); int currentRowStart = 0; int currentRow = 0; int totalRows = table.getRows().size(); while (true) { // available height of the page float available_height = 770; // how many rows fit the height? while (available_height > 0 && currentRow < totalRows) { available_height -= table.getRowHeight(currentRow++); } // we stop as soon as all the rows are counted if (currentRow == totalRows) { break; } // we draw part the rows that fit the page and start a new page table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas); document.newPage(); currentRowStart = currentRow - 5; currentRow -= 5; if (currentRow < 1) { currentRow = 1; currentRowStart = 1; } } // we draw the remaining rows table.writeSelectedRows(currentRowStart, -1, 36, 806, canvas); document.close(); } }
Render data as table
/** * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/24404686/i-need-to-create-a-table-and-assign-the-values-into-the-table-in-pdf-using-javaf */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import sandbox.WrapToTest; @WrapToTest public class ArrayToTable { public static final String DEST = "results/tables/array_to_table.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new ArrayToTable().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(8); table.setWidthPercentage(100); List<List<String>> dataset = getData(); for (List<String> record : dataset) { for (String field : record) { table.addCell(field); } } document.add(table); document.close(); } public List<List<String>> getData() { List<List<String>> data = new ArrayList<List<String>>(); String[] tableTitleList = {" Title", " (Re)set", " Obs", " Mean", " Std.Dev", " Min", " Max", "Unit"}; data.add(Arrays.asList(tableTitleList)); for (int i = 0; i < 10; ) { List<String> dataLine = new ArrayList<String>(); i++; for (int j = 0; j < tableTitleList.length; j++) { dataLine.add(tableTitleList[j] + " " + i); } data.add(dataLine); } return data; } }
/** * This example was written by Bruno Lowagie. */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Font.FontFamily; import com.itextpdf.text.PageSize; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import sandbox.WrapToTest; @WrapToTest public class UnitedStates { public static final String DEST = "results/tables/united_states.pdf"; public static final String DATA = "resources/data/united_states.csv"; public static final Font FONT = new Font(); public static final Font BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD); public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new UnitedStates().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(9); table.setWidthPercentage(100); table.setWidths(new int[]{4, 1, 3, 4, 3, 3, 3, 3, 1}); BufferedReader br = new BufferedReader(new FileReader(DATA)); String line = br.readLine(); process(table, line, BOLD); table.setHeaderRows(1); while ((line = br.readLine()) != null) { process(table, line, FONT); } br.close(); document.add(table); document.close(); } public void process(PdfPTable table, String line, Font font) { StringTokenizer tokenizer = new StringTokenizer(line, ";"); while (tokenizer.hasMoreTokens()) { table.addCell(new Phrase(tokenizer.nextToken(), font)); } } }
/* * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/37526223 */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class RowColumnOrder { public static final String DEST = "results/tables/row_column_order.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new RowColumnOrder().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); document.add(new Paragraph("By design tables are filled row by row:")); PdfPTable table = new PdfPTable(5); table.setSpacingBefore(10); table.setSpacingAfter(10); for (int i = 1; i <= 15; i++) { table.addCell("cell " + i); } document.add(table); document.add(new Paragraph("If you want to change this behavior, you need to create a two-dimensional array first:")); String[][] array = new String[3][]; int column = 0; int row = 0; for (int i = 1; i <= 15; i++) { if (column == 0) { array[row] = new String[5]; } array[row++][column] = "cell " + i; if (row == 3) { column++; row = 0; } } table = new PdfPTable(5); table.setSpacingBefore(10); for (String[] r : array) { for (String c : r) { table.addCell(c); } } document.add(table); document.close(); } }
PdfPTable and PdfTemplate
/** * Example written by Bruno Lowagie in answer to the following question: * http://stackoverflow.com/questions/22093993/itext-whats-an-easy-to-print-first-right-then-down */ package sandbox.tables; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sandbox.WrapToTest; @WrapToTest public class TableTemplate { public static final String DEST = "results/tables/table_template.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new TableTemplate().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfPTable table = new PdfPTable(15); table.setTotalWidth(1500); PdfPCell cell; for (int r = 'A'; r <= 'Z'; r++) { for (int c = 1; c <= 15; c++) { cell = new PdfPCell(); cell.setFixedHeight(50); cell.addElement(new Paragraph(String.valueOf((char) r) + String.valueOf(c))); table.addCell(cell); } } PdfContentByte canvas = writer.getDirectContent(); PdfTemplate tableTemplate = canvas.createTemplate(1500, 1300); table.writeSelectedRows(0, -1, 0, 1300, tableTemplate); PdfTemplate clip; for (int j = 0; j < 1500; j += 500) { for (int i = 1300; i > 0; i -= 650) { clip = canvas.createTemplate(500, 650); clip.addTemplate(tableTemplate, -j, 650 - i); canvas.addTemplate(clip, 36, 156); document.newPage(); } } document.close(); } }