Tables and fonts

  1. /**
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/27577633/itext-library-exception-throwing-on-adding-blank-cell-with-space
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.BaseColor;
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import sandbox.WrapToTest;   import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;   @WrapToTest
  18. public class CellMethod {
  19. public static final String DEST = "results/tables/cell_method.pdf";
  20. public static final String FONT = "resources/fonts/FreeSans.ttf";   public static void main(String[] args) throws IOException,
  21. DocumentException {
  22. File file = new File(DEST);
  23. file.getParentFile().mkdirs();
  24. new CellMethod().createPdf(DEST);
  25. }   public void createPdf(String dest) throws IOException, DocumentException {
  26. Document document = new Document();
  27. PdfWriter.getInstance(document, new FileOutputStream(dest));
  28. document.open();
  29. PdfPTable table = new PdfPTable(2);
  30. table.addCell("Winansi");
  31. table.addCell(getNormalCell("Test", null, 12));
  32. table.addCell("Winansi");
  33. table.addCell(getNormalCell("Test", null, -12));
  34. table.addCell("Greek");
  35. table.addCell(getNormalCell("\u039d\u03cd\u03c6\u03b5\u03c2", "greek", 12));
  36. table.addCell("Czech");
  37. table.addCell(getNormalCell("\u010c,\u0106,\u0160,\u017d,\u0110", "czech", 12));
  38. table.addCell("Test");
  39. table.addCell(getNormalCell(" ", null, 12));
  40. table.addCell("Test");
  41. table.addCell(getNormalCell(" ", "greek", 12));
  42. table.addCell("Test");
  43. table.addCell(getNormalCell(" ", "czech", 12));
  44. document.add(table);
  45. document.close();
  46. }   public static PdfPCell getNormalCell(String string, String language, float size)
  47. throws DocumentException, IOException {
  48. if(string != null && "".equals(string)){
  49. return new PdfPCell();
  50. }
  51. Font f = getFontForThisLanguage(language);
  52. if(size < 0) {
  53. f.setColor(BaseColor.RED);
  54. size = -size;
  55. }
  56. f.setSize(size);
  57. PdfPCell cell = new PdfPCell(new Phrase(string, f));
  58. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  59. return cell;
  60. }
  61. public static Font getFontForThisLanguage(String language) {
  62. if ("czech".equals(language)) {
  63. return FontFactory.getFont(FONT, "Cp1250", true);
  64. }
  65. if ("greek".equals(language)) {
  66. return FontFactory.getFont(FONT, "Cp1253", true);
  67. }
  68. return FontFactory.getFont(FONT, null, true);
  69. }
  70. }

  1. /**
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/29548762/why-is-my-table-not-being-generated-on-my-pdf-file-using-itextsharp
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.BaseColor;
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;   @WrapToTest
  18. public class SimpleTable7 {
  19. public static final String DEST = "results/tables/simple_table7.pdf";   public static void main(String[] args) throws IOException,
  20. DocumentException {
  21. File file = new File(DEST);
  22. file.getParentFile().mkdirs();
  23. new SimpleTable7().createPdf(DEST);
  24. }
  25. public void createPdf(String dest) throws IOException, DocumentException {
  26. Document document = new Document();
  27. PdfWriter.getInstance(document, new FileOutputStream(dest));
  28. document.open();
  29. Font titleFont = FontFactory.getFont(FontFactory.COURIER_BOLD, 11, BaseColor.BLACK);
  30. Paragraph docTitle = new Paragraph("UCSC Direct - Direct Payment Form", titleFont);
  31. document.add(docTitle);
  32. Font subtitleFont = FontFactory.getFont("Times Roman", 9, BaseColor.BLACK);
  33. Paragraph subTitle = new Paragraph("(not to be used for reimbursement of services)", subtitleFont);
  34. document.add(subTitle);
  35. Font importantNoticeFont = FontFactory.getFont("Courier", 9, BaseColor.RED);
  36. Paragraph importantNotice = new Paragraph("Important: Form must be filled out in Adobe Reader or Acrobat Professional 8.1 or above. To save completed forms, Acrobat Professional is required. For technical and accessibility assistance, contact the Campus Controller's Office.", importantNoticeFont);
  37. document.add(importantNotice);   PdfPTable table = new PdfPTable(10); // the arg is the number of columns
  38. PdfPCell cell = new PdfPCell(docTitle);
  39. cell.setColspan(3);
  40. cell.setBorder(PdfPCell.NO_BORDER);
  41. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  42. table.addCell(cell);
  43. PdfPCell cellCaveat = new PdfPCell(subTitle);
  44. cellCaveat.setColspan(2);
  45. cellCaveat.setBorder(PdfPCell.NO_BORDER);
  46. table.addCell(cellCaveat);
  47. PdfPCell cellImportantNote = new PdfPCell(importantNotice);
  48. cellImportantNote.setColspan(5);
  49. cellImportantNote.setBorder(PdfPCell.NO_BORDER);
  50. table.addCell(cellImportantNote);
  51. document.add(table);   document.add(new Paragraph(20, "This is the same table, created differently", subtitleFont));
  52. table = new PdfPTable(3);
  53. table.setWidths(new int[]{3, 2, 5});
  54. cell.setColspan(1);
  55. table.addCell(cell);
  56. cellCaveat.setColspan(1);
  57. table.addCell(cellCaveat);
  58. cellImportantNote.setColspan(1);
  59. table.addCell(cellImportantNote);
  60. document.add(table);   document.close();
  61. }  }
  1. Table and cell events to draw borders
  1. package sandbox.tables;   import com.itextpdf.text.Document;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.Paragraph;
  4. import com.itextpdf.text.Phrase;
  5. import com.itextpdf.text.Rectangle;
  6. import com.itextpdf.text.pdf.PdfContentByte;
  7. import com.itextpdf.text.pdf.PdfPCell;
  8. import com.itextpdf.text.pdf.PdfPCellEvent;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfPTableEvent;
  11. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
  14. 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,
  15. float[] heights, int headerRows, int rowStart,
  16. PdfContentByte[] canvases) {
  17. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  18. canvas.setLineDash(3f, 3f);
  19. float llx = widths[0][0];
  20. float urx = widths[0][widths.length];
  21. for (int i = 0; i < heights.length; i++) {
  22. canvas.moveTo(llx, heights[i]);
  23. canvas.lineTo(urx, heights[i]);
  24. }
  25. for (int i = 0; i < widths.length; i++) {
  26. for (int j = 0; j < widths[i].length; j++) {
  27. canvas.moveTo(widths[i][j], heights[i]);
  28. canvas.lineTo(widths[i][j], heights[i+1]);
  29. }
  30. }
  31. canvas.stroke();
  32. }
  33. }   class DottedCell implements PdfPCellEvent {
  34. public void cellLayout(PdfPCell cell, Rectangle position,
  35. PdfContentByte[] canvases) {
  36. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  37. canvas.setLineDash(3f, 3f);
  38. canvas.rectangle(position.getLeft(), position.getBottom(),
  39. position.getWidth(), position.getHeight());
  40. canvas.stroke();
  41. }
  42. }   public static void main(String[] args) throws IOException, DocumentException {
  43. File file = new File(DEST);
  44. file.getParentFile().mkdirs();
  45. new DottedLineCell().createPdf(DEST);
  46. }   public void createPdf(String dest) throws IOException, DocumentException {
  47. DottedLineCell app = new DottedLineCell();
  48. Document document = new Document();
  49. PdfWriter.getInstance(document, new FileOutputStream(dest));
  50. document.open();
  51. document.add(new Paragraph("Table event"));
  52. PdfPTable table = new PdfPTable(3);
  53. table.setTableEvent(app.new DottedCells());
  54. table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
  55. table.addCell("A1");
  56. table.addCell("A2");
  57. table.addCell("A3");
  58. table.addCell("B1");
  59. table.addCell("B2");
  60. table.addCell("B3");
  61. table.addCell("C1");
  62. table.addCell("C2");
  63. table.addCell("C3");
  64. document.add(table);
  65. document.add(new Paragraph("Cell event"));
  66. table = new PdfPTable(1);
  67. PdfPCell cell = new PdfPCell(new Phrase("Test"));
  68. cell.setCellEvent(app.new DottedCell());
  69. cell.setBorder(PdfPCell.NO_BORDER);
  70. table.addCell(cell);
  71. document.add(table);
  72. document.close();
  73. }
  74. }

RoundedCorners.java

  1. /**
  2. * This example was written by Bruno Lowagie in answer to the following questions:
  3. * http://stackoverflow.com/questions/30106862/left-and-right-top-round-corner-for-rectangelroundrectangle
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Phrase;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfContentByte;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPCellEvent;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;   @WrapToTest
  17. public class RoundedCorners {   public static final String DEST = "results/tables/rounded_corners.pdf";   class SpecialRoundedCell implements PdfPCellEvent {
  18. public void cellLayout(PdfPCell cell, Rectangle position,
  19. PdfContentByte[] canvases) {
  20. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  21. float llx = position.getLeft() + 2;
  22. float lly = position.getBottom() + 2;
  23. float urx = position.getRight() - 2;
  24. float ury = position.getTop() - 2;
  25. float r = 4;
  26. float b = 0.4477f;
  27. canvas.moveTo(llx, lly);
  28. canvas.lineTo(urx, lly);
  29. canvas.lineTo(urx, ury - r);
  30. canvas.curveTo(urx, ury - r * b, urx - r * b, ury, urx - r, ury);
  31. canvas.lineTo(llx + r, ury);
  32. canvas.curveTo(llx + r * b, ury, llx, ury - r * b, llx, ury - r);
  33. canvas.lineTo(llx, lly);
  34. canvas.stroke();
  35. }
  36. }   public static void main(String[] args) throws IOException, DocumentException {
  37. File file = new File(DEST);
  38. file.getParentFile().mkdirs();
  39. new RoundedCorners().createPdf(DEST);
  40. }   public void createPdf(String dest) throws IOException, DocumentException {
  41. Document document = new Document();
  42. PdfWriter.getInstance(document, new FileOutputStream(dest));
  43. document.open();
  44. PdfPTable table = new PdfPTable(3);
  45. PdfPCell cell = getCell("These cells have rounded borders at the top.");
  46. table.addCell(cell);
  47. cell = getCell("These cells aren't rounded at the bottom.");
  48. table.addCell(cell);
  49. cell = getCell("A custom cell event was used to achieve this.");
  50. table.addCell(cell);
  51. document.add(table);
  52. document.close();
  53. }   public PdfPCell getCell(String content) {
  54. PdfPCell cell = new PdfPCell(new Phrase(content));
  55. cell.setCellEvent(new SpecialRoundedCell());
  56. cell.setPadding(5);
  57. cell.setBorder(PdfPCell.NO_BORDER);
  58. return cell;
  59. }
  60. }

DottedLineHeader.java

  1. package sandbox.tables;   import com.itextpdf.text.Document;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.Paragraph;
  4. import com.itextpdf.text.Rectangle;
  5. import com.itextpdf.text.pdf.PdfContentByte;
  6. import com.itextpdf.text.pdf.PdfPCell;
  7. import com.itextpdf.text.pdf.PdfPCellEvent;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfPTableEvent;
  10. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import sandbox.WrapToTest;   @WrapToTest
  14. 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,
  15. float[] heights, int headerRows, int rowStart,
  16. PdfContentByte[] canvases) {
  17. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  18. canvas.setLineDash(3f, 3f);
  19. float x1 = widths[0][0];
  20. float x2 = widths[0][widths.length];
  21. canvas.moveTo(x1, heights[0]);
  22. canvas.lineTo(x2, heights[0]);
  23. canvas.moveTo(x1, heights[headerRows]);
  24. canvas.lineTo(x2, heights[headerRows]);
  25. canvas.stroke();
  26. }
  27. }   class DottedCell implements PdfPCellEvent {
  28. public void cellLayout(PdfPCell cell, Rectangle position,
  29. PdfContentByte[] canvases) {
  30. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  31. canvas.setLineDash(3f, 3f);
  32. canvas.moveTo(position.getLeft(), position.getTop());
  33. canvas.lineTo(position.getRight(), position.getTop());
  34. canvas.moveTo(position.getLeft(), position.getBottom());
  35. canvas.lineTo(position.getRight(), position.getBottom());
  36. canvas.stroke();
  37. }
  38. }   public static void main(String[] args) throws IOException, DocumentException {
  39. File file = new File(DEST);
  40. file.getParentFile().mkdirs();
  41. new DottedLineHeader().createPdf(DEST);
  42. }   public void createPdf(String dest) throws IOException, DocumentException {
  43. Document document = new Document();
  44. PdfWriter.getInstance(document, new FileOutputStream(dest));
  45. document.open();
  46. document.add(new Paragraph("Table event"));
  47. PdfPTable table = new PdfPTable(3);
  48. table.setTableEvent(new DottedHeader());
  49. table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
  50. table.addCell("A1");
  51. table.addCell("A2");
  52. table.addCell("A3");
  53. table.setHeaderRows(1);
  54. table.addCell("B1");
  55. table.addCell("B2");
  56. table.addCell("B3");
  57. table.addCell("C1");
  58. table.addCell("C2");
  59. table.addCell("C3");
  60. document.add(table);
  61. document.add(new Paragraph("Cell event"));
  62. table = new PdfPTable(3);
  63. table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
  64. table.getDefaultCell().setCellEvent(new DottedCell());
  65. table.addCell("A1");
  66. table.addCell("A2");
  67. table.addCell("A3");
  68. table.getDefaultCell().setCellEvent(null);
  69. table.addCell("B1");
  70. table.addCell("B2");
  71. table.addCell("B3");
  72. table.addCell("C1");
  73. table.addCell("C2");
  74. table.addCell("C3");
  75. document.add(table);
  76. document.close();
  77. }
  78. }

CustomBorder.java

  1. /**
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Phrase;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfContentByte;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPRow;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfPTableEventAfterSplit;
  14. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;   @WrapToTest
  18. 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 {
  19. File file = new File(DEST);
  20. file.getParentFile().mkdirs();
  21. new CustomBorder().createPdf(DEST);
  22. }   class BorderEvent implements PdfPTableEventAfterSplit {   protected int rowCount;
  23. protected boolean bottom = true;
  24. protected boolean top = true;   public void setRowCount(int rowCount) {
  25. this.rowCount = rowCount;
  26. }   public void splitTable(PdfPTable table) {
  27. if (table.getRows().size() != rowCount) {
  28. bottom = false;
  29. }
  30. }   public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
  31. if (table.getRows().size() != rowCount) {
  32. // if the table gains a row, a row was split
  33. rowCount = table.getRows().size();
  34. top = false;
  35. }
  36. }   public void tableLayout(PdfPTable table, float[][] width, float[] height,
  37. int headerRows, int rowStart, PdfContentByte[] canvas) {
  38. float widths[] = width[0];
  39. float y1 = height[0];
  40. float y2 = height[height.length - 1];
  41. PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
  42. for (int i = 0; i < widths.length; i++) {
  43. cb.moveTo(widths[i], y1);
  44. cb.lineTo(widths[i], y2);
  45. }
  46. float x1 = widths[0];
  47. float x2 = widths[widths.length - 1];
  48. for (int i = top ? 0 : 1; i < (bottom ? height.length : height.length - 1); i++) {
  49. cb.moveTo(x1, height[i]);
  50. cb.lineTo(x2, height[i]);
  51. }
  52. cb.stroke();
  53. cb.resetRGBColorStroke();
  54. bottom = true;
  55. top = true;
  56. }
  57. }   public void createPdf(String dest) throws IOException, DocumentException {
  58. Document document = new Document();
  59. PdfWriter.getInstance(document, new FileOutputStream(dest));
  60. document.open();
  61. PdfPTable table = new PdfPTable(2);
  62. table.setTotalWidth(500);
  63. table.setLockedWidth(true);
  64. BorderEvent event = new BorderEvent();
  65. table.setTableEvent(event);
  66. table.setWidthPercentage(100);
  67. table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  68. table.setSplitLate(false);
  69. PdfPCell cell = new PdfPCell(new Phrase(TEXT));
  70. cell.setBorder(Rectangle.NO_BORDER);
  71. for (int i = 0; i < 60; ) {
  72. table.addCell("Cell " + (++i));
  73. table.addCell(cell);
  74. }
  75. event.setRowCount(table.getRows().size());
  76. document.add(table);
  77. document.close();
  78. }
  79. }

CustomBorder2.java

  1. /**
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Phrase;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfContentByte;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPRow;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfPTableEventAfterSplit;
  14. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;   @WrapToTest
  18. 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 {
  19. File file = new File(DEST);
  20. file.getParentFile().mkdirs();
  21. new CustomBorder2().createPdf(DEST);
  22. }   class BorderEvent implements PdfPTableEventAfterSplit {   protected boolean bottom = true;
  23. protected boolean top = true;   public void splitTable(PdfPTable table) {
  24. bottom = false;
  25. }   public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
  26. top = false;
  27. }   public void tableLayout(PdfPTable table, float[][] width, float[] height,
  28. int headerRows, int rowStart, PdfContentByte[] canvas) {
  29. float widths[] = width[0];
  30. float y1 = height[0];
  31. float y2 = height[height.length - 1];
  32. float x1 = widths[0];
  33. float x2 = widths[widths.length - 1];
  34. PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
  35. cb.moveTo(x1, y1);
  36. cb.lineTo(x1, y2);
  37. cb.moveTo(x2, y1);
  38. cb.lineTo(x2, y2);
  39. if (top) {
  40. cb.moveTo(x1, y1);
  41. cb.lineTo(x2, y1);
  42. }
  43. if (bottom) {
  44. cb.moveTo(x1, y2);
  45. cb.lineTo(x2, y2);
  46. }
  47. cb.stroke();
  48. cb.resetRGBColorStroke();
  49. bottom = true;
  50. top = true;
  51. }
  52. }   public void createPdf(String dest) throws IOException, DocumentException {
  53. Document document = new Document();
  54. PdfWriter.getInstance(document, new FileOutputStream(dest));
  55. document.open();
  56. PdfPTable table = new PdfPTable(2);
  57. table.setTotalWidth(500);
  58. table.setLockedWidth(true);
  59. BorderEvent event = new BorderEvent();
  60. table.setTableEvent(event);
  61. table.setWidthPercentage(100);
  62. table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  63. table.setSplitLate(false);
  64. PdfPCell cell = new PdfPCell(new Phrase(TEXT));
  65. cell.setBorder(Rectangle.NO_BORDER);
  66. for (int i = 0; i < 60; ) {
  67. table.addCell("Cell " + (++i));
  68. table.addCell(cell);
  69. }
  70. document.add(table);
  71. document.close();
  72. }
  73. }

DottedLineCell2.java

  1. package sandbox.tables;   import com.itextpdf.text.Document;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.Phrase;
  4. import com.itextpdf.text.Rectangle;
  5. import com.itextpdf.text.pdf.PdfContentByte;
  6. import com.itextpdf.text.pdf.PdfPCell;
  7. import com.itextpdf.text.pdf.PdfPCellEvent;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
  12. public class DottedLineCell2 {   public static final String DEST = "results/tables/dotted_line_cell2.pdf";   class DottedCell implements PdfPCellEvent {
  13. private int border = 0;
  14. public DottedCell(int border) {
  15. this.border = border;
  16. }
  17. public void cellLayout(PdfPCell cell, Rectangle position,
  18. PdfContentByte[] canvases) {
  19. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  20. canvas.saveState();
  21. canvas.setLineDash(0, 4, 2);
  22. if ((border & PdfPCell.TOP) == PdfPCell.TOP) {
  23. canvas.moveTo(position.getRight(), position.getTop());
  24. canvas.lineTo(position.getLeft(), position.getTop());
  25. }
  26. if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) {
  27. canvas.moveTo(position.getRight(), position.getBottom());
  28. canvas.lineTo(position.getLeft(), position.getBottom());
  29. }
  30. if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) {
  31. canvas.moveTo(position.getRight(), position.getTop());
  32. canvas.lineTo(position.getRight(), position.getBottom());
  33. }
  34. if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) {
  35. canvas.moveTo(position.getLeft(), position.getTop());
  36. canvas.lineTo(position.getLeft(), position.getBottom());
  37. }
  38. canvas.stroke();
  39. canvas.restoreState();
  40. }
  41. }   public static void main(String[] args) throws IOException, DocumentException {
  42. File file = new File(DEST);
  43. file.getParentFile().mkdirs();
  44. new DottedLineCell2().createPdf(DEST);
  45. }   public void createPdf(String dest) throws IOException, DocumentException {
  46. Document document = new Document();
  47. PdfWriter.getInstance(document, new FileOutputStream(dest));
  48. document.open();   PdfPTable table;
  49. PdfPCell cell;   table = new PdfPTable(4);
  50. table.setSpacingAfter(30);
  51. cell = new PdfPCell(new Phrase("left border"));
  52. cell.setBorder(PdfPCell.NO_BORDER);
  53. cell.setCellEvent(new DottedCell(PdfPCell.LEFT));
  54. table.addCell(cell);
  55. cell = new PdfPCell(new Phrase("right border"));
  56. cell.setBorder(PdfPCell.NO_BORDER);
  57. cell.setCellEvent(new DottedCell(PdfPCell.RIGHT));
  58. table.addCell(cell);
  59. cell = new PdfPCell(new Phrase("top border"));
  60. cell.setBorder(PdfPCell.NO_BORDER);
  61. cell.setCellEvent(new DottedCell(PdfPCell.TOP));
  62. table.addCell(cell);
  63. cell = new PdfPCell(new Phrase("bottom border"));
  64. cell.setBorder(PdfPCell.NO_BORDER);
  65. cell.setCellEvent(new DottedCell(PdfPCell.BOTTOM));
  66. table.addCell(cell);
  67. document.add(table);   table = new PdfPTable(4);
  68. table.setSpacingAfter(30);
  69. cell = new PdfPCell(new Phrase("left and top border"));
  70. cell.setBorder(PdfPCell.NO_BORDER);
  71. cell.setCellEvent(new DottedCell(PdfPCell.LEFT | PdfPCell.TOP));
  72. table.addCell(cell);
  73. cell = new PdfPCell(new Phrase("right and bottom border"));
  74. cell.setBorder(PdfPCell.NO_BORDER);
  75. cell.setCellEvent(new DottedCell(PdfPCell.RIGHT | PdfPCell.BOTTOM));
  76. table.addCell(cell);
  77. cell = new PdfPCell(new Phrase("no border"));
  78. cell.setBorder(PdfPCell.NO_BORDER);
  79. table.addCell(cell);
  80. cell = new PdfPCell(new Phrase("full border"));
  81. cell.setBorder(PdfPCell.NO_BORDER);
  82. cell.setCellEvent(new DottedCell(PdfPCell.BOX));
  83. table.addCell(cell);
  84. document.add(table);
  85. document.close();
  86. }
  87. }

CustomBorder3.java

  1. package sandbox.tables;   import com.itextpdf.text.Document;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.Phrase;
  4. import com.itextpdf.text.Rectangle;
  5. import com.itextpdf.text.pdf.PdfContentByte;
  6. import com.itextpdf.text.pdf.PdfPCell;
  7. import com.itextpdf.text.pdf.PdfPCellEvent;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
  12. public class CustomBorder3 {   public static final String DEST = "results/tables/custom_border_3.pdf";   interface LineDash {
  13. public void applyLineDash(PdfContentByte canvas);
  14. }   class SolidLine implements LineDash {
  15. public void applyLineDash(PdfContentByte canvas) { }
  16. }   class DottedLine implements LineDash {
  17. public void applyLineDash(PdfContentByte canvas) {
  18. canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
  19. canvas.setLineDash(0, 4, 2);
  20. }
  21. }   class DashedLine implements LineDash {
  22. public void applyLineDash(PdfContentByte canvas) {
  23. canvas.setLineDash(3, 3);
  24. }
  25. }   class CustomBorder implements PdfPCellEvent {
  26. protected LineDash left;
  27. protected LineDash right;
  28. protected LineDash top;
  29. protected LineDash bottom;
  30. public CustomBorder(LineDash left, LineDash right,
  31. LineDash top, LineDash bottom) {
  32. this.left = left;
  33. this.right = right;
  34. this.top = top;
  35. this.bottom = bottom;
  36. }
  37. public void cellLayout(PdfPCell cell, Rectangle position,
  38. PdfContentByte[] canvases) {
  39. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  40. if (top != null) {
  41. canvas.saveState();
  42. top.applyLineDash(canvas);
  43. canvas.moveTo(position.getRight(), position.getTop());
  44. canvas.lineTo(position.getLeft(), position.getTop());
  45. canvas.stroke();
  46. canvas.restoreState();
  47. }
  48. if (bottom != null) {
  49. canvas.saveState();
  50. bottom.applyLineDash(canvas);
  51. canvas.moveTo(position.getRight(), position.getBottom());
  52. canvas.lineTo(position.getLeft(), position.getBottom());
  53. canvas.stroke();
  54. canvas.restoreState();
  55. }
  56. if (right != null) {
  57. canvas.saveState();
  58. right.applyLineDash(canvas);
  59. canvas.moveTo(position.getRight(), position.getTop());
  60. canvas.lineTo(position.getRight(), position.getBottom());
  61. canvas.stroke();
  62. canvas.restoreState();
  63. }
  64. if (left != null) {
  65. canvas.saveState();
  66. left.applyLineDash(canvas);
  67. canvas.moveTo(position.getLeft(), position.getTop());
  68. canvas.lineTo(position.getLeft(), position.getBottom());
  69. canvas.stroke();
  70. canvas.restoreState();
  71. }
  72. }
  73. }   public static void main(String[] args) throws IOException, DocumentException {
  74. File file = new File(DEST);
  75. file.getParentFile().mkdirs();
  76. new CustomBorder3().createPdf(DEST);
  77. }   public void createPdf(String dest) throws IOException, DocumentException {
  78. Document document = new Document();
  79. PdfWriter.getInstance(document, new FileOutputStream(dest));
  80. document.open();   PdfPTable table;
  81. PdfPCell cell;
  82. LineDash solid = new SolidLine();
  83. LineDash dotted = new DottedLine();
  84. LineDash dashed = new DashedLine();   table = new PdfPTable(4);
  85. table.setSpacingAfter(30);
  86. cell = new PdfPCell(new Phrase("dotted left border"));
  87. cell.setBorder(PdfPCell.NO_BORDER);
  88. cell.setCellEvent(new CustomBorder(dotted, null, null, null));
  89. table.addCell(cell);
  90. cell = new PdfPCell(new Phrase("solid right border"));
  91. cell.setBorder(PdfPCell.NO_BORDER);
  92. cell.setCellEvent(new CustomBorder(null, solid, null, null));
  93. table.addCell(cell);
  94. cell = new PdfPCell(new Phrase("dashed top border"));
  95. cell.setBorder(PdfPCell.NO_BORDER);
  96. cell.setCellEvent(new CustomBorder(null, null, dashed, null));
  97. table.addCell(cell);
  98. cell = new PdfPCell(new Phrase("bottom border"));
  99. cell.setBorder(PdfPCell.NO_BORDER);
  100. cell.setCellEvent(new CustomBorder(null, null, null, solid));
  101. table.addCell(cell);
  102. document.add(table);   table = new PdfPTable(4);
  103. table.setSpacingAfter(30);
  104. cell = new PdfPCell(new Phrase("dotted left and solid top border"));
  105. cell.setBorder(PdfPCell.NO_BORDER);
  106. cell.setCellEvent(new CustomBorder(dotted, null, solid, null));
  107. table.addCell(cell);
  108. cell = new PdfPCell(new Phrase("dashed right and dashed bottom border"));
  109. cell.setBorder(PdfPCell.NO_BORDER);
  110. cell.setCellEvent(new CustomBorder(null, dashed, null, dashed));
  111. table.addCell(cell);
  112. cell = new PdfPCell(new Phrase("no border"));
  113. cell.setBorder(PdfPCell.NO_BORDER);
  114. table.addCell(cell);
  115. cell = new PdfPCell(new Phrase("full solid border"));
  116. cell.setBorder(PdfPCell.NO_BORDER);
  117. cell.setCellEvent(new CustomBorder(solid, solid, solid, solid));
  118. table.addCell(cell);
  119. document.add(table);
  120. document.close();
  121. }
  122. }

CustomBorder4.java

  1. package sandbox.tables;   import com.itextpdf.text.Document;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.Phrase;
  4. import com.itextpdf.text.Rectangle;
  5. import com.itextpdf.text.pdf.PdfContentByte;
  6. import com.itextpdf.text.pdf.PdfPCell;
  7. import com.itextpdf.text.pdf.PdfPCellEvent;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
  12. public class CustomBorder4 {   public static final String DEST = "results/tables/custom_border_4.pdf";   abstract class CustomBorder implements PdfPCellEvent {
  13. private int border = 0;
  14. public CustomBorder(int border) {
  15. this.border = border;
  16. }
  17. public void cellLayout(PdfPCell cell, Rectangle position,
  18. PdfContentByte[] canvases) {
  19. PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  20. canvas.saveState();
  21. setLineDash(canvas);
  22. if ((border & PdfPCell.TOP) == PdfPCell.TOP) {
  23. canvas.moveTo(position.getRight(), position.getTop());
  24. canvas.lineTo(position.getLeft(), position.getTop());
  25. }
  26. if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) {
  27. canvas.moveTo(position.getRight(), position.getBottom());
  28. canvas.lineTo(position.getLeft(), position.getBottom());
  29. }
  30. if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) {
  31. canvas.moveTo(position.getRight(), position.getTop());
  32. canvas.lineTo(position.getRight(), position.getBottom());
  33. }
  34. if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) {
  35. canvas.moveTo(position.getLeft(), position.getTop());
  36. canvas.lineTo(position.getLeft(), position.getBottom());
  37. }
  38. canvas.stroke();
  39. canvas.restoreState();
  40. }   public abstract void setLineDash(PdfContentByte canvas);
  41. }   class SolidBorder extends CustomBorder {
  42. public SolidBorder(int border) { super(border); }
  43. public void setLineDash(PdfContentByte canvas) {}
  44. }
  45. class DottedBorder extends CustomBorder {
  46. public DottedBorder(int border) { super(border); }
  47. public void setLineDash(PdfContentByte canvas) {
  48. canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
  49. canvas.setLineDash(0, 4, 2);
  50. }
  51. }
  52. class DashedBorder extends CustomBorder {
  53. public DashedBorder(int border) { super(border); }
  54. public void setLineDash(PdfContentByte canvas) {
  55. canvas.setLineDash(3, 3);
  56. }
  57. }   public static void main(String[] args) throws IOException, DocumentException {
  58. File file = new File(DEST);
  59. file.getParentFile().mkdirs();
  60. new CustomBorder4().createPdf(DEST);
  61. }   public void createPdf(String dest) throws IOException, DocumentException {
  62. Document document = new Document();
  63. PdfWriter.getInstance(document, new FileOutputStream(dest));
  64. document.open();   PdfPTable table;
  65. PdfPCell cell;   table = new PdfPTable(4);
  66. table.setSpacingAfter(30);
  67. cell = new PdfPCell(new Phrase("dotted left border"));
  68. cell.setBorder(PdfPCell.NO_BORDER);
  69. cell.setCellEvent(new DottedBorder(PdfPCell.LEFT));
  70. table.addCell(cell);
  71. cell = new PdfPCell(new Phrase("solid right border"));
  72. cell.setBorder(PdfPCell.NO_BORDER);
  73. cell.setCellEvent(new SolidBorder(PdfPCell.RIGHT));
  74. table.addCell(cell);
  75. cell = new PdfPCell(new Phrase("solid top border"));
  76. cell.setBorder(PdfPCell.NO_BORDER);
  77. cell.setCellEvent(new SolidBorder(PdfPCell.TOP));
  78. table.addCell(cell);
  79. cell = new PdfPCell(new Phrase("dashed bottom border"));
  80. cell.setBorder(PdfPCell.NO_BORDER);
  81. cell.setCellEvent(new DashedBorder(PdfPCell.BOTTOM));
  82. table.addCell(cell);
  83. document.add(table);   table = new PdfPTable(4);
  84. table.setSpacingAfter(30);
  85. cell = new PdfPCell(new Phrase("dotted left and dashed top border"));
  86. cell.setBorder(PdfPCell.NO_BORDER);
  87. cell.setCellEvent(new DottedBorder(PdfPCell.LEFT));
  88. cell.setCellEvent(new DashedBorder(PdfPCell.TOP));
  89. table.addCell(cell);
  90. cell = new PdfPCell(new Phrase("solid right and dotted bottom border"));
  91. cell.setBorder(PdfPCell.NO_BORDER);
  92. cell.setCellEvent(new DottedBorder(PdfPCell.BOTTOM));
  93. cell.setCellEvent(new SolidBorder(PdfPCell.RIGHT));
  94. table.addCell(cell);
  95. cell = new PdfPCell(new Phrase("no border"));
  96. cell.setBorder(PdfPCell.NO_BORDER);
  97. table.addCell(cell);
  98. cell = new PdfPCell(new Phrase("full border"));
  99. cell.setBorder(PdfPCell.NO_BORDER);
  100. cell.setCellEvent(new DottedBorder(PdfPCell.LEFT | PdfPCell.RIGHT));
  101. cell.setCellEvent(new SolidBorder(PdfPCell.TOP));
  102. cell.setCellEvent(new DashedBorder(PdfPCell.BOTTOM));
  103. table.addCell(cell);
  104. document.add(table);
  105. document.close();
  106. }
  107. }

TableBorder.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/35340003
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Rectangle;
  8. import com.itextpdf.text.pdf.PdfContentByte;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfPTableEvent;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;     /**
  16. * @author Bruno Lowagie (iText Software)
  17. */
  18. @WrapToTest
  19. public class TableBorder {   public static final String DEST = "results/tables/table_border_outer_only.pdf";   public static void main(String[] args) throws IOException,
  20. DocumentException {
  21. File file = new File(DEST);
  22. file.getParentFile().mkdirs();
  23. new TableBorder().createPdf(DEST);
  24. }   public void createPdf(String dest) throws IOException, DocumentException {
  25. Document document = new Document();
  26. PdfWriter.getInstance(document, new FileOutputStream(dest));
  27. document.open();
  28. PdfPTable table = new PdfPTable(4);
  29. table.setTableEvent(new BorderEvent());
  30. table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  31. for(int aw = 0; aw < 16; aw++){
  32. table.addCell("hi");
  33. }
  34. document.add(table);
  35. document.close();
  36. }   public class BorderEvent implements PdfPTableEvent {
  37. public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
  38. float width[] = widths[0];
  39. float x1 = width[0];
  40. float x2 = width[width.length - 1];
  41. float y1 = heights[0];
  42. float y2 = heights[heights.length - 1];
  43. PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
  44. cb.rectangle(x1, y1, x2 - x1, y2 - y1);
  45. cb.stroke();
  46. cb.resetRGBColorStroke();
  47. }
  48. }
  49. }

CellTitle.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/35746651
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.BaseColor;
  6. import com.itextpdf.text.Chunk;
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPCellEvent;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;   /**
  22. * @author Bruno Lowagie (iText Software)
  23. */
  24. @WrapToTest
  25. public class CellTitle {   public static final String DEST = "results/tables/cell_title.pdf";   class Title implements PdfPCellEvent {
  26. protected String title;   public Title(String title) {
  27. this.title = title;
  28. }   public void cellLayout(PdfPCell cell, Rectangle position,
  29. PdfContentByte[] canvases) {
  30. Chunk c = new Chunk(title);
  31. c.setBackground(BaseColor.LIGHT_GRAY);
  32. PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  33. ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  34. new Phrase(c), position.getLeft(5), position.getTop(5), 0);
  35. }
  36. }   public static void main(String[] args) throws IOException, DocumentException {
  37. File file = new File(DEST);
  38. file.getParentFile().mkdirs();
  39. new CellTitle().createPdf(DEST);
  40. }   public void createPdf(String dest) throws IOException, DocumentException {
  41. Document document = new Document();
  42. PdfWriter.getInstance(document, new FileOutputStream(dest));
  43. document.open();
  44. PdfPTable table = new PdfPTable(1);
  45. PdfPCell cell = getCell("The title of this cell is title 1", "title 1");
  46. table.addCell(cell);
  47. cell = getCell("The title of this cell is title 2", "title 2");
  48. table.addCell(cell);
  49. cell = getCell("The title of this cell is title 3", "title 3");
  50. table.addCell(cell);
  51. document.add(table);
  52. document.close();
  53. }   public PdfPCell getCell(String content, String title) {
  54. PdfPCell cell = new PdfPCell(new Phrase(content));
  55. cell.setCellEvent(new Title(title));
  56. cell.setPadding(5);
  57. return cell;
  58. }  }

Splitting tables

Splitting.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Paragraph;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;   /**
  14. *
  15. * @author Bruno Lowagie (iText Software)
  16. */
  17. public class Splitting {
  18. public static final String DEST = "results/tables/splitting.pdf";
  19. public static void main(String[] args) throws IOException,
  20. DocumentException {
  21. File file = new File(DEST);
  22. file.getParentFile().mkdirs();
  23. new Splitting().createPdf(DEST);
  24. }   public void createPdf(String dest) throws IOException, DocumentException {
  25. Document document = new Document();
  26. PdfWriter.getInstance(document, new FileOutputStream(dest));
  27. document.open();
  28. Paragraph p = new Paragraph("Test");
  29. PdfPTable table = new PdfPTable(2);
  30. for (int i = 1; i < 6; i++) {
  31. table.addCell("key " + i);
  32. table.addCell("value " + i);
  33. }
  34. for (int i = 0; i < 40; i++) {
  35. document.add(p);
  36. }
  37. document.add(table);
  38. for (int i = 0; i < 38; i++) {
  39. document.add(p);
  40. }
  41. PdfPTable nesting = new PdfPTable(1);
  42. PdfPCell cell = new PdfPCell(table);
  43. cell.setBorder(PdfPCell.NO_BORDER);
  44. nesting.addCell(cell);
  45. document.add(nesting);
  46. document.close();
  47. }
  48. }

Splitting2.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Paragraph;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import sandbox.WrapToTest;   /**
  14. *
  15. * @author Bruno Lowagie (iText Software)
  16. */
  17. @WrapToTest
  18. public class Splitting2 {
  19. public static final String DEST = "results/tables/splitting2.pdf";
  20. public static void main(String[] args) throws IOException,
  21. DocumentException {
  22. File file = new File(DEST);
  23. file.getParentFile().mkdirs();
  24. new Splitting2().createPdf(DEST);
  25. }   public void createPdf(String dest) throws IOException, DocumentException {
  26. Document document = new Document();
  27. PdfWriter.getInstance(document, new FileOutputStream(dest));
  28. document.open();
  29. Paragraph p = new Paragraph("Test");
  30. PdfPTable table = new PdfPTable(2);
  31. for (int i = 1; i < 6; i++) {
  32. table.addCell("key " + i);
  33. table.addCell("value " + i);
  34. }
  35. for (int i = 0; i < 40; i++) {
  36. document.add(p);
  37. }
  38. document.add(table);
  39. for (int i = 0; i < 38; i++) {
  40. document.add(p);
  41. }
  42. table.setKeepTogether(true);
  43. document.add(table);
  44. document.close();
  45. }
  46. }

TableSplitTest.java

  1. /**
  2. * Example written by Ramesh in the context of a question on SO:
  3. * http://stackoverflow.com/questions/29345454/itext-avoid-row-splitting-in-table
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.BaseColor;
  6. import com.itextpdf.text.Chunk;
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.FontFactory;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17. import com.itextpdf.text.pdf.draw.LineSeparator;   import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;   @WrapToTest
  21. public class TableSplitTest {
  22. public static final String DEST = "results/tables/split_test.pdf";   public static void main(String[] args) throws IOException, DocumentException {
  23. File file = new File(DEST);
  24. file.getParentFile().mkdirs();
  25. new TableSplitTest().createPdf(DEST);
  26. }   public void createPdf(String dest) throws DocumentException, IOException {
  27. Document document = new Document();
  28. PdfWriter.getInstance(document, new FileOutputStream(dest));
  29. document.setMargins(15, 15, 55, 35);
  30. document.open();
  31. String[] header = new String[] { "Header1", "Header2", "Header3",
  32. "Header4", "Header5" };
  33. String[] content = new String[] { "column 1", "column 2",
  34. "some Text in column 3", "Test data ", "column 5" };
  35. PdfPTable table = new PdfPTable(header.length);
  36. table.setHeaderRows(1);
  37. table.setWidths(new int[] { 3, 2, 4, 3, 2 });
  38. table.setWidthPercentage(98);
  39. table.setSpacingBefore(15);
  40. table.setSplitLate(false);
  41. for (String columnHeader : header) {
  42. PdfPCell headerCell = new PdfPCell();
  43. headerCell.addElement(new Phrase(columnHeader, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD)));
  44. headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  45. headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  46. headerCell.setBorderColor(BaseColor.LIGHT_GRAY);
  47. headerCell.setPadding(8);
  48. table.addCell(headerCell);
  49. }
  50. for (int i = 0; i < 15; i++) {
  51. int j = 0;
  52. for (String text : content) {
  53. if (i == 13 && j == 3) {
  54. text = "Test data \n Test data \n Test data";
  55. }
  56. j++;
  57. PdfPCell cell = new PdfPCell();
  58. cell.addElement(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL)));
  59. cell.setBorderColor(BaseColor.LIGHT_GRAY);
  60. cell.setPadding(5);
  61. table.addCell(cell);
  62. }
  63. }
  64. document.add(table);
  65. document.add(new Phrase("\n"));
  66. LineSeparator separator = new LineSeparator();
  67. separator.setPercentage(98);
  68. separator.setLineColor(BaseColor.LIGHT_GRAY);
  69. Chunk linebreak = new Chunk(separator);
  70. document.add(linebreak);
  71. for (int k = 0; k < 5; k++) {
  72. Paragraph info = new Paragraph("Some title", FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL));
  73. info.setSpacingBefore(12f);
  74. document.add(info);
  75. table = new PdfPTable(header.length);
  76. table.setHeaderRows(1);
  77. table.setWidths(new int[] { 3, 2, 4, 3, 2 });
  78. table.setWidthPercentage(98);
  79. table.setSpacingBefore(15);
  80. table.setSplitLate(false);
  81. for (String columnHeader : header) {
  82. PdfPCell headerCell = new PdfPCell();
  83. headerCell.addElement(new Phrase(columnHeader, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD)));
  84. headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  85. headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  86. headerCell.setBorderColor(BaseColor.LIGHT_GRAY);
  87. headerCell.setPadding(8);
  88. table.addCell(headerCell);
  89. }
  90. for (String text : content) {
  91. PdfPCell cell = new PdfPCell();
  92. cell.addElement(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL)));
  93. cell.setBorderColor(BaseColor.LIGHT_GRAY);
  94. cell.setPadding(5);
  95. table.addCell(cell);
  96. }
  97. document.add(table);
  98. separator = new LineSeparator();
  99. separator.setPercentage(98);
  100. separator.setLineColor(BaseColor.LIGHT_GRAY);
  101. linebreak = new Chunk(separator);
  102. document.add(linebreak);
  103. }
  104. document.close();
  105. }   }

SplitRowAtSpecificRow.java

  1. /**
  2. * Example written by Bruno Lowagie in answer to:
  3. * http://stackoverflow.com/questions/24665167/table-keeprowstogether-in-itext-5-5-1-doesnt-seem-to-work-correctly
  4. */   package sandbox.tables;   import com.itextpdf.text.Document;
  5. import com.itextpdf.text.DocumentException;
  6. import com.itextpdf.text.Phrase;
  7. import com.itextpdf.text.Rectangle;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import sandbox.WrapToTest;   @WrapToTest
  14. public class SplitRowAtSpecificRow {
  15. public static final String DEST = "results/tables/split_at_row.pdf";   public static void main(String[] args) throws IOException, DocumentException {
  16. File file = new File(DEST);
  17. file.getParentFile().mkdirs();
  18. new SplitRowAtSpecificRow().createPdf(DEST);
  19. }   public void createPdf(String dest) throws IOException, DocumentException {
  20. PdfPTable table = new PdfPTable(1);
  21. table.setTotalWidth(550);
  22. table.setLockedWidth(true);
  23. for (int i = 0; i < 10; i++) {
  24. PdfPCell cell;
  25. if (i == 9) {
  26. cell = new PdfPCell(new Phrase("Two\nLines"));
  27. }
  28. else {
  29. cell = new PdfPCell(new Phrase(Integer.toString(i)));
  30. }
  31. table.addCell(cell);
  32. }
  33. Document document = new Document(new Rectangle(612, 242));
  34. PdfWriter.getInstance(document, new FileOutputStream(dest));
  35. document.open();
  36. table.setSplitLate(false);
  37. table.setBreakPoints(8);
  38. document.add(table);
  39. document.close();
  40. }
  41. }

SplitRowAtEndOfPage.java

  1. /**
  2. * Example written by Bruno Lowagie in answer to:
  3. * http://stackoverflow.com/questions/24616920/last-row-in-itext-table-extending-when-it-shouldnt
  4. */   package sandbox.tables;   import com.itextpdf.text.Document;
  5. import com.itextpdf.text.DocumentException;
  6. import com.itextpdf.text.Phrase;
  7. import com.itextpdf.text.Rectangle;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import sandbox.WrapToTest;   @WrapToTest
  14. public class SplitRowAtEndOfPage {
  15. public static final String DEST = "results/tables/split_row_at_end_of_page.pdf";   public static void main(String[] args) throws IOException, DocumentException {
  16. File file = new File(DEST);
  17. file.getParentFile().mkdirs();
  18. new SplitRowAtEndOfPage().createPdf(DEST);
  19. }   public void createPdf(String dest) throws IOException, DocumentException {
  20. PdfPTable table = new PdfPTable(1);
  21. table.setTotalWidth(550);
  22. table.setLockedWidth(true);
  23. for (int i = 0; i < 10; i++) {
  24. PdfPCell cell;
  25. if (i == 9) {
  26. cell = new PdfPCell(new Phrase("Two\nLines"));
  27. }
  28. else {
  29. cell = new PdfPCell(new Phrase(Integer.toString(i)));
  30. }
  31. table.addCell(cell);
  32. }
  33. Document document = new Document(new Rectangle(612, 242));
  34. PdfWriter.getInstance(document, new FileOutputStream(dest));
  35. document.open();
  36. table.setSplitLate(false);
  37. document.add(table);
  38. document.close();
  39. }
  40. }

Rowspan and splitting

SplittingAndRowspan.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/35356847
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Paragraph;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfPCell;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;   @WrapToTest
  16. /**
  17. * @author Bruno Lowagie (iText Software)
  18. */
  19. public class SplittingAndRowspan {
  20. public static final String DEST = "results/tables/splitting_and_rowspan.pdf";   public static void main(String[] args) throws IOException,
  21. DocumentException {
  22. File file = new File(DEST);
  23. file.getParentFile().mkdirs();
  24. new SplittingAndRowspan().createPdf(DEST);
  25. }   public void createPdf(String dest) throws IOException, DocumentException {
  26. Document document = new Document(new Rectangle(300, 150));
  27. PdfWriter.getInstance(document, new FileOutputStream(dest));
  28. document.open();
  29. document.add(new Paragraph("Table with setSplitLate(true):"));
  30. PdfPTable table = new PdfPTable(2);
  31. table.setSpacingBefore(10);
  32. PdfPCell cell = new PdfPCell();
  33. cell.addElement(new Paragraph("G"));
  34. cell.addElement(new Paragraph("R"));
  35. cell.addElement(new Paragraph("P"));
  36. cell.setRowspan(3);
  37. table.addCell(cell);
  38. table.addCell("row 1");
  39. table.addCell("row 2");
  40. table.addCell("row 3");
  41. document.add(table);
  42. document.add(new Paragraph("Table with setSplitLate(false):"));
  43. table.setSplitLate(false);
  44. document.add(table);
  45. document.close();
  46. }
  47. }

SplittingNestedTable1.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/35356847
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Paragraph;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfPCell;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;   @WrapToTest
  16. /**
  17. * @author Bruno Lowagie (iText Software)
  18. */
  19. public class SplittingNestedTable1 {
  20. public static final String DEST = "results/tables/splitting_nested_table1.pdf";   public static void main(String[] args) throws IOException,
  21. DocumentException {
  22. File file = new File(DEST);
  23. file.getParentFile().mkdirs();
  24. new SplittingNestedTable1().createPdf(DEST);
  25. }   public void createPdf(String dest) throws IOException, DocumentException {
  26. Document document = new Document(new Rectangle(300, 150));
  27. PdfWriter.getInstance(document, new FileOutputStream(dest));
  28. document.open();
  29. document.add(new Paragraph("Table with setSplitLate(true):"));
  30. PdfPTable table = new PdfPTable(2);
  31. table.setSpacingBefore(10);
  32. PdfPCell cell = new PdfPCell();
  33. cell.addElement(new Paragraph("G"));
  34. cell.addElement(new Paragraph("R"));
  35. cell.addElement(new Paragraph("O"));
  36. cell.addElement(new Paragraph("U"));
  37. cell.addElement(new Paragraph("P"));
  38. table.addCell(cell);
  39. PdfPTable inner = new PdfPTable(1);
  40. inner.addCell("row 1");
  41. inner.addCell("row 2");
  42. inner.addCell("row 3");
  43. inner.addCell("row 4");
  44. inner.addCell("row 5");
  45. cell = new PdfPCell(inner);
  46. cell.setPadding(0);
  47. table.addCell(cell);
  48. document.add(table);
  49. document.newPage();
  50. document.add(new Paragraph("Table with setSplitLate(false):"));
  51. table.setSplitLate(false);
  52. document.add(table);
  53. document.close();
  54. }
  55. }

SplittingNestedTable2.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/35356847
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Element;
  8. import com.itextpdf.text.Paragraph;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;   @WrapToTest
  18. /**
  19. * @author Bruno Lowagie (iText Software)
  20. */
  21. public class SplittingNestedTable2 {
  22. public static final String DEST = "results/tables/splitting_nested_table2.pdf";   public static void main(String[] args) throws IOException,
  23. DocumentException {
  24. File file = new File(DEST);
  25. file.getParentFile().mkdirs();
  26. new SplittingNestedTable2().createPdf(DEST);
  27. }   public void createPdf(String dest) throws IOException, DocumentException {
  28. Document document = new Document(new Rectangle(300, 150));
  29. PdfWriter.getInstance(document, new FileOutputStream(dest));
  30. document.open();
  31. document.add(new Paragraph("Table with setSplitLate(true):"));
  32. PdfPTable table = new PdfPTable(2);
  33. table.setSpacingBefore(10);
  34. PdfPCell cell = new PdfPCell( new Phrase("GROUPS"));
  35. cell.setRotation(90);
  36. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  37. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  38. table.addCell(cell);
  39. PdfPTable inner = new PdfPTable(1);
  40. inner.addCell("row 1");
  41. inner.addCell("row 2");
  42. inner.addCell("row 3");
  43. inner.addCell("row 4");
  44. inner.addCell("row 5");
  45. cell = new PdfPCell(inner);
  46. cell.setPadding(0);
  47. table.addCell(cell);
  48. document.add(table);
  49. document.newPage();
  50. document.add(new Paragraph("Table with setSplitLate(false):"));
  51. table.setSplitLate(false);
  52. document.add(table);
  53. document.close();
  54. }
  55. }

Rotating cell content

RotatedCell.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to:
  3. * http://stackoverflow.com/questions/37246838
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Element;
  8. import com.itextpdf.text.Phrase;
  9. import com.itextpdf.text.pdf.PdfPCell;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;   /**
  15. * @author Bruno Lowagie (iText Software)
  16. */
  17. public class RotatedCell {
  18. public static final String DEST = "results/tables/rotated_cell.pdf";   public static void main(String[] args) throws IOException,
  19. DocumentException {
  20. File file = new File(DEST);
  21. file.getParentFile().mkdirs();
  22. new RotatedCell().createPdf(DEST);
  23. }
  24. public void createPdf(String dest) throws IOException, DocumentException {
  25. Document document = new Document();
  26. PdfWriter.getInstance(document, new FileOutputStream(dest));
  27. document.open();
  28. PdfPTable table = new PdfPTable(8);
  29. for (int i = 0; i < 8; i++) {
  30. PdfPCell cell =
  31. new PdfPCell(new Phrase(String.format("May %s, 2016", i + 15)));
  32. cell.setRotation(90);
  33. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  34. table.addCell(cell);
  35. }
  36. for(int i = 0; i < 16; i++){
  37. table.addCell("hi");
  38. }
  39. document.add(table);
  40. document.close();
  41. }  }
  1. Repeating rows

HeaderRowRepeated.java

  1. /**
  2. * This example is written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/32582927/itext-pdfwriter-writing-table-header-if-the-few-table-rows-go-to-new-page
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.pdf.PdfPTable;
  8. import com.itextpdf.text.pdf.PdfWriter;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import sandbox.WrapToTest;   /**
  13. * @author iText
  14. */
  15. @WrapToTest
  16. public class HeaderRowRepeated {
  17. public static final String DEST = "results/tables/repeat_header_row.pdf";   public static void main(String[] args) throws IOException,
  18. DocumentException {
  19. File file = new File(DEST);
  20. file.getParentFile().mkdirs();
  21. new HeaderRowRepeated().createPdf(DEST);
  22. }
  23. public void createPdf(String dest) throws IOException, DocumentException {
  24. Document document = new Document();
  25. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  26. document.open();
  27. // table with 2 columns:
  28. PdfPTable table = new PdfPTable(2);
  29. // header row:
  30. table.addCell("Key");
  31. table.addCell("Value");
  32. table.setHeaderRows(1);
  33. table.setSkipFirstHeader(true);
  34. // many data rows:
  35. for (int i = 1; i < 51; i++) {
  36. table.addCell("key: " + i);
  37. table.addCell("value: " + i);
  38. }
  39. document.add(table);
  40. document.close();
  41. }
  42. }

RepeatLastRows.java

  1. /**
  2. * This example is written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
  12. public class RepeatLastRows {
  13. public static final String DEST = "results/tables/repeat_last_rows.pdf";   public static void main(String[] args) throws IOException,
  14. DocumentException {
  15. File file = new File(DEST);
  16. file.getParentFile().mkdirs();
  17. new RepeatLastRows().createPdf(DEST);
  18. }   public void createPdf(String dest) throws IOException, DocumentException {
  19. Document document = new Document();
  20. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  21. document.open();
  22. // we create a table that spans the width of the page and that has 99 rows
  23. PdfPTable table = new PdfPTable(1);
  24. table.setTotalWidth(523);
  25. for (int i = 1; i < 100; i++)
  26. table.addCell("row " + i);
  27. // we add the table at an absolute position (starting at the top of the page)
  28. PdfContentByte canvas = writer.getDirectContent();
  29. int currentRowStart = 0;
  30. int currentRow = 0;
  31. int totalRows = table.getRows().size();
  32. while (true) {
  33. // available height of the page
  34. float available_height = 770;
  35. // how many rows fit the height?
  36. while (available_height > 0 && currentRow < totalRows) {
  37. available_height -= table.getRowHeight(currentRow++);
  38. }
  39. // we stop as soon as all the rows are counted
  40. if (currentRow == totalRows)
  41. break;
  42. // we draw part the rows that fit the page and start a new page
  43. table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
  44. document.newPage();
  45. currentRowStart = currentRow;
  46. }
  47. // if there are less than 5 rows left, we adjust the row start value
  48. if (currentRow - currentRowStart < 5)
  49. currentRowStart = currentRow - 5;
  50. // we write the remaining rows
  51. table.writeSelectedRows(currentRowStart, currentRow, 36, 806, canvas);
  52. document.close();
  53. }   }

RepeatLastRows2.java

  1. /**
  2. * This example is written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
  12. public class RepeatLastRows2 {
  13. public static final String DEST = "results/tables/repeat_last_rows2.pdf";   public static void main(String[] args) throws IOException,
  14. DocumentException {
  15. File file = new File(DEST);
  16. file.getParentFile().mkdirs();
  17. new RepeatLastRows2().createPdf(DEST);
  18. }   public void createPdf(String dest) throws IOException, DocumentException {
  19. Document document = new Document();
  20. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  21. document.open();
  22. // we create a table that spans the width of the page and that has 99 rows
  23. PdfPTable table = new PdfPTable(1);
  24. table.setTotalWidth(523);
  25. for (int i = 1; i < 100; i++)
  26. table.addCell("row " + i);
  27. // we add the table at an absolute position (starting at the top of the page)
  28. PdfContentByte canvas = writer.getDirectContent();
  29. int currentRowStart = 0;
  30. int currentRow = 0;
  31. int totalRows = table.getRows().size();
  32. while (true) {
  33. // available height of the page
  34. float available_height = 770;
  35. // how many rows fit the height?
  36. while (available_height > 0 && currentRow < totalRows) {
  37. available_height -= table.getRowHeight(currentRow++);
  38. }
  39. // we stop as soon as all the rows are counted
  40. if (currentRow == totalRows) {
  41. break;
  42. }
  43. // we draw part the rows that fit the page and start a new page
  44. table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
  45. document.newPage();
  46. currentRowStart = currentRow - 5;
  47. currentRow -= 5;
  48. if (currentRow < 1) {
  49. currentRow = 1;
  50. currentRowStart = 1;
  51. }
  52. }
  53. // we draw the remaining rows
  54. table.writeSelectedRows(currentRowStart, -1, 36, 806, canvas);
  55. document.close();
  56. }  }
  1.  

Render data as table

ArrayToTable.java

  1. /**
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/24404686/i-need-to-create-a-table-and-assign-the-values-into-the-table-in-pdf-using-javaf
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.pdf.PdfPTable;
  8. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.List;
  14. import sandbox.WrapToTest;   @WrapToTest
  15. public class ArrayToTable {   public static final String DEST = "results/tables/array_to_table.pdf";   public static void main(String[] args) throws IOException, DocumentException {
  16. File file = new File(DEST);
  17. file.getParentFile().mkdirs();
  18. new ArrayToTable().createPdf(DEST);
  19. }   public void createPdf(String dest) throws IOException, DocumentException {
  20. Document document = new Document();
  21. PdfWriter.getInstance(document, new FileOutputStream(dest));
  22. document.open();
  23. PdfPTable table = new PdfPTable(8);
  24. table.setWidthPercentage(100);
  25. List<List<String>> dataset = getData();
  26. for (List<String> record : dataset) {
  27. for (String field : record) {
  28. table.addCell(field);
  29. }
  30. }
  31. document.add(table);
  32. document.close();
  33. }   public List<List<String>> getData() {
  34. List<List<String>> data = new ArrayList<List<String>>();
  35. String[] tableTitleList = {" Title", " (Re)set", " Obs", " Mean", " Std.Dev", " Min", " Max", "Unit"};
  36. data.add(Arrays.asList(tableTitleList));
  37. for (int i = 0; i < 10; ) {
  38. List<String> dataLine = new ArrayList<String>();
  39. i++;
  40. for (int j = 0; j < tableTitleList.length; j++) {
  41. dataLine.add(tableTitleList[j] + " " + i);
  42. }
  43. data.add(dataLine);
  44. }
  45. return data;
  46. }
  47. }

UnitedStates.java

  1. /**
  2. * This example was written by Bruno Lowagie.
  3. */
  4. package sandbox.tables;   import com.itextpdf.text.Document;
  5. import com.itextpdf.text.DocumentException;
  6. import com.itextpdf.text.Font;
  7. import com.itextpdf.text.Font.FontFamily;
  8. import com.itextpdf.text.PageSize;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;   import java.io.BufferedReader;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.FileReader;
  15. import java.io.IOException;
  16. import java.util.StringTokenizer;   import sandbox.WrapToTest;   @WrapToTest
  17. public class UnitedStates {   public static final String DEST = "results/tables/united_states.pdf";
  18. public static final String DATA = "resources/data/united_states.csv";
  19. public static final Font FONT = new Font();
  20. public static final Font BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD);   public static void main(String[] args) throws IOException, DocumentException {
  21. File file = new File(DEST);
  22. file.getParentFile().mkdirs();
  23. new UnitedStates().createPdf(DEST);
  24. }   public void createPdf(String dest) throws IOException, DocumentException {
  25. Document document = new Document(PageSize.A4.rotate());
  26. PdfWriter.getInstance(document, new FileOutputStream(dest));
  27. document.open();
  28. PdfPTable table = new PdfPTable(9);
  29. table.setWidthPercentage(100);
  30. table.setWidths(new int[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
  31. BufferedReader br = new BufferedReader(new FileReader(DATA));
  32. String line = br.readLine();
  33. process(table, line, BOLD);
  34. table.setHeaderRows(1);
  35. while ((line = br.readLine()) != null) {
  36. process(table, line, FONT);
  37. }
  38. br.close();
  39. document.add(table);
  40. document.close();
  41. }   public void process(PdfPTable table, String line, Font font) {
  42. StringTokenizer tokenizer = new StringTokenizer(line, ";");
  43. while (tokenizer.hasMoreTokens()) {
  44. table.addCell(new Phrase(tokenizer.nextToken(), font));
  45. }
  46. }
  47. }

RowColumnOrder.java

  1. /*
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/37526223
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Paragraph;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import sandbox.WrapToTest;   @WrapToTest
  14. public class RowColumnOrder {   public static final String DEST = "results/tables/row_column_order.pdf";   public static void main(String[] args) throws IOException, DocumentException {
  15. File file = new File(DEST);
  16. file.getParentFile().mkdirs();
  17. new RowColumnOrder().createPdf(DEST);
  18. }   public void createPdf(String dest) throws IOException, DocumentException {
  19. Document document = new Document();
  20. PdfWriter.getInstance(document, new FileOutputStream(dest));
  21. document.open();
  22. document.add(new Paragraph("By design tables are filled row by row:"));
  23. PdfPTable table = new PdfPTable(5);
  24. table.setSpacingBefore(10);
  25. table.setSpacingAfter(10);
  26. for (int i = 1; i <= 15; i++) {
  27. table.addCell("cell " + i);
  28. }
  29. document.add(table);   document.add(new Paragraph("If you want to change this behavior, you need to create a two-dimensional array first:"));
  30. String[][] array = new String[3][];
  31. int column = 0;
  32. int row = 0;
  33. for (int i = 1; i <= 15; i++) {
  34. if (column == 0) {
  35. array[row] = new String[5];
  36. }
  37. array[row++][column] = "cell " + i;
  38. if (row == 3) {
  39. column++;
  40. row = 0;
  41. }
  42. }
  43. table = new PdfPTable(5);
  44. table.setSpacingBefore(10);
  45. for (String[] r : array) {
  46. for (String c : r) {
  47. table.addCell(c);
  48. }
  49. }
  50. document.add(table);
  51. document.close();
  52. }
  53. }
  1.  

PdfPTable and PdfTemplate

TableTemplate.java

  1. /**
  2. * Example written by Bruno Lowagie in answer to the following question:
  3. * http://stackoverflow.com/questions/22093993/itext-whats-an-easy-to-print-first-right-then-down
  4. */
  5. package sandbox.tables;   import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Paragraph;
  8. import com.itextpdf.text.pdf.PdfContentByte;
  9. import com.itextpdf.text.pdf.PdfPCell;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfTemplate;
  12. import com.itextpdf.text.pdf.PdfWriter;   import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;   import sandbox.WrapToTest;   @WrapToTest
  15. public class TableTemplate {   public static final String DEST = "results/tables/table_template.pdf";   public static void main(String[] args) throws IOException, DocumentException {
  16. File file = new File(DEST);
  17. file.getParentFile().mkdirs();
  18. new TableTemplate().createPdf(DEST);
  19. }   public void createPdf(String dest) throws IOException, DocumentException {
  20. Document document = new Document();
  21. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  22. document.open();
  23. PdfPTable table = new PdfPTable(15);
  24. table.setTotalWidth(1500);
  25. PdfPCell cell;
  26. for (int r = 'A'; r <= 'Z'; r++) {
  27. for (int c = 1; c <= 15; c++) {
  28. cell = new PdfPCell();
  29. cell.setFixedHeight(50);
  30. cell.addElement(new Paragraph(String.valueOf((char) r) + String.valueOf(c)));
  31. table.addCell(cell);
  32. }
  33. }
  34. PdfContentByte canvas = writer.getDirectContent();
  35. PdfTemplate tableTemplate = canvas.createTemplate(1500, 1300);
  36. table.writeSelectedRows(0, -1, 0, 1300, tableTemplate);
  37. PdfTemplate clip;
  38. for (int j = 0; j < 1500; j += 500) {
  39. for (int i = 1300; i > 0; i -= 650) {
  40. clip = canvas.createTemplate(500, 650);
  41. clip.addTemplate(tableTemplate, -j, 650 - i);
  42. canvas.addTemplate(clip, 36, 156);
  43. document.newPage();
  44. }
  45. }
  46. document.close();
  47. }
  48. }
  1.  
  1.  

Itext Demo的更多相关文章

  1. .net快速创建PDF文档 by c#

    原文地址:http://www.cnblogs.com/Creator/archive/2010/03/13/1685020.html C#引用IText创建PDF文档 先引用IText    可以从 ...

  2. java输出pdf

    package snake; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...

  3. 关于itext生成pdf的新的demo(包含简单的提取txt文件的内容 和xml内容转化为pdf)

    一.用的iText版本为7.0.2版本,maven的配置如下: <dependencies> <!-- always needed --> <dependency> ...

  4. PDF 生成插件 flying saucer 和 iText

    最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...

  5. 【itext】7步制作兼容各种文档格式的Itext5页眉页脚 实现page x pf y

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...

  6. 【Itext】解决Itext5大并发大数据量下输出PDF发生内存溢出outofmemery异常

    尼玛,这个问题干扰了我两个星期!! 关键字 itext5 outofmemery 内存溢出 大数据 高并发 多线程 pdf 导出 报表 itext 并发 在读<<iText in Acti ...

  7. 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...

  8. IText实现对PDF文档属性的基本设置

    一.Itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  9. 使用iText快速更新书签

    一.介绍 pdfbox基于Apache协议,商用无需开放源代码. iText基于APGL协议,打包和修改需发布源码,除非花钱买断. 二.用途 下载的电子书,有的书签是FitHeight,也就是缩放后整 ...

随机推荐

  1. 【练习】数据移动---导入(IMPDP)

    1.导入表并验证: :: SYS@ORA11GR2>grant connect,resource to jj identified by jj; Grant succeeded. :: SYS@ ...

  2. Java-生成指定长度验证码的一种简单思路

     前言:以前做过的一个项目,刚开的时候始验证码是在前端生成前端验证的,后来觉得不靠谱,另外就是找回密码的功能也需要发送邮件和短信的验证码,所以,验证码就必须在后端生成并且保存到应用会话中才行了!所以, ...

  3. HTML5中与页面显示相关的API

    1.HTML5中与页面显示相关的API 在HTML5中,增加了几个与页面显示相关的API,其中一个是Page Visibility API Page Visibility API  是指当页面变为最小 ...

  4. 在xilinxFPGA上使用microblaze及自写GPIO中断

    很久很久没有更新过博客了,今天来扒一扒FPGA上CPU软核的使用. 主要完成的功能:使用的开发板是nexys 4 DDR,板上有16个switch以及16个LED,需要完成microblaze对led ...

  5. Python自动化 【第十二篇】:Python进阶-MySQL和ORM

    本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 外键 增删改查表 权限 事务 索引 python 操作mysql ORM sql ...

  6. 高可用Hadoop平台-Flume NG实战图解篇

    1.概述 今天补充一篇关于Flume的博客,前面在讲解高可用的Hadoop平台的时候遗漏了这篇,本篇博客为大家讲述以下内容: Flume NG简述 单点Flume NG搭建.运行 高可用Flume N ...

  7. mybatis执行批量更新update

    Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...

  8. 6.openssl rsautl和openssl pkeyutl

    rsautl是rsa的工具,相当于rsa.dgst的部分功能集合.可用于签名.验证.加密和解密文件.非对称密钥的密钥是随机生成的,因此不需要也无法指定salt参与加密. pkeyutl是非对称加密的通 ...

  9. 一个简单的synchronized多线程问题、梳理与思考

    一个程序,多个线程同时操作一个变量,给这个变量+1().功能很简单,可是怎么样去实现呢?这其中涉及到了哪些问题? 最基础想法 见代码: public class Test extends Thread ...

  10. Wince 6.0适用 .NET 使用HttpRequest的Post上传文件,服务端的Web API接收Post上传上来的文件 代码

    //调用的示例 private string fileName = "InStorageData.csv"; string filePath = parentPath + Comm ...