Itext Demo
Tables and fonts
/**
* Example written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/27577633/itext-library-exception-throwing-on-adding-blank-cell-with-space
*/
package sandbox.tables; import com.itextpdf.text.BaseColor;
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.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import sandbox.WrapToTest; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; @WrapToTest
public class CellMethod {
public static final String DEST = "results/tables/cell_method.pdf";
public static final String FONT = "resources/fonts/FreeSans.ttf"; public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CellMethod().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(2);
table.addCell("Winansi");
table.addCell(getNormalCell("Test", null, 12));
table.addCell("Winansi");
table.addCell(getNormalCell("Test", null, -12));
table.addCell("Greek");
table.addCell(getNormalCell("\u039d\u03cd\u03c6\u03b5\u03c2", "greek", 12));
table.addCell("Czech");
table.addCell(getNormalCell("\u010c,\u0106,\u0160,\u017d,\u0110", "czech", 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", null, 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", "greek", 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", "czech", 12));
document.add(table);
document.close();
} public static PdfPCell getNormalCell(String string, String language, float size)
throws DocumentException, IOException {
if(string != null && "".equals(string)){
return new PdfPCell();
}
Font f = getFontForThisLanguage(language);
if(size < 0) {
f.setColor(BaseColor.RED);
size = -size;
}
f.setSize(size);
PdfPCell cell = new PdfPCell(new Phrase(string, f));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
return cell;
}
public static Font getFontForThisLanguage(String language) {
if ("czech".equals(language)) {
return FontFactory.getFont(FONT, "Cp1250", true);
}
if ("greek".equals(language)) {
return FontFactory.getFont(FONT, "Cp1253", true);
}
return FontFactory.getFont(FONT, null, true);
}
}
/**
* Example written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/29548762/why-is-my-table-not-being-generated-on-my-pdf-file-using-itextsharp
*/
package sandbox.tables; import com.itextpdf.text.BaseColor;
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.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 SimpleTable7 {
public static final String DEST = "results/tables/simple_table7.pdf"; public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SimpleTable7().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font titleFont = FontFactory.getFont(FontFactory.COURIER_BOLD, 11, BaseColor.BLACK);
Paragraph docTitle = new Paragraph("UCSC Direct - Direct Payment Form", titleFont);
document.add(docTitle);
Font subtitleFont = FontFactory.getFont("Times Roman", 9, BaseColor.BLACK);
Paragraph subTitle = new Paragraph("(not to be used for reimbursement of services)", subtitleFont);
document.add(subTitle);
Font importantNoticeFont = FontFactory.getFont("Courier", 9, BaseColor.RED);
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);
document.add(importantNotice); PdfPTable table = new PdfPTable(10); // the arg is the number of columns
PdfPCell cell = new PdfPCell(docTitle);
cell.setColspan(3);
cell.setBorder(PdfPCell.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
PdfPCell cellCaveat = new PdfPCell(subTitle);
cellCaveat.setColspan(2);
cellCaveat.setBorder(PdfPCell.NO_BORDER);
table.addCell(cellCaveat);
PdfPCell cellImportantNote = new PdfPCell(importantNotice);
cellImportantNote.setColspan(5);
cellImportantNote.setBorder(PdfPCell.NO_BORDER);
table.addCell(cellImportantNote);
document.add(table); document.add(new Paragraph(20, "This is the same table, created differently", subtitleFont));
table = new PdfPTable(3);
table.setWidths(new int[]{3, 2, 5});
cell.setColspan(1);
table.addCell(cell);
cellCaveat.setColspan(1);
table.addCell(cellCaveat);
cellImportantNote.setColspan(1);
table.addCell(cellImportantNote);
document.add(table); document.close();
} }
Table and cell events to draw borders
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();
}
}
Itext Demo的更多相关文章
- .net快速创建PDF文档 by c#
原文地址:http://www.cnblogs.com/Creator/archive/2010/03/13/1685020.html C#引用IText创建PDF文档 先引用IText 可以从 ...
- java输出pdf
package snake; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...
- 关于itext生成pdf的新的demo(包含简单的提取txt文件的内容 和xml内容转化为pdf)
一.用的iText版本为7.0.2版本,maven的配置如下: <dependencies> <!-- always needed --> <dependency> ...
- PDF 生成插件 flying saucer 和 iText
最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...
- 【itext】7步制作兼容各种文档格式的Itext5页眉页脚 实现page x pf y
itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...
- 【Itext】解决Itext5大并发大数据量下输出PDF发生内存溢出outofmemery异常
尼玛,这个问题干扰了我两个星期!! 关键字 itext5 outofmemery 内存溢出 大数据 高并发 多线程 pdf 导出 报表 itext 并发 在读<<iText in Acti ...
- 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页
itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...
- IText实现对PDF文档属性的基本设置
一.Itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...
- 使用iText快速更新书签
一.介绍 pdfbox基于Apache协议,商用无需开放源代码. iText基于APGL协议,打包和修改需发布源码,除非花钱买断. 二.用途 下载的电子书,有的书签是FitHeight,也就是缩放后整 ...
随机推荐
- 在linux中配置安装telnet服务
Telnet 是一种流行的用于通过 Internet 登录到远程计算机的协议.Telnet 服务器软件包为远程登录主机提供了支持.要通过 Telnet 协议与另一台主机通讯,您可以使用名称或 Inte ...
- pycharm激活码,拿走不谢
激活码 新下载pycharm编辑器以后,会遇到一个激活的问题,否则只能试用30天 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVO ...
- okhttp3 post 数据打包方法
import okhttp3.OkHttpClient; import okhttp3.FormBody; import okhttp3.Request; import okhttp3.Request ...
- Oracle11g导出空表
# Oracle11g导出空表 <!-- create time: 2015-06-01 23:35:24 --> ###原因 11G中有个新特性,当表无数据时,不分配`segment`, ...
- iOS平台使用陀螺仪传感器
在移动端开发过程中,有时候会用到陀螺仪传感器获取当前手机的姿态,下面给出iOS端如何获取陀螺仪姿态数据的代码: //根据陀螺仪的四元数转换为矩阵 + (GLKMatrix4)calculateMatr ...
- NK3C系统中ID的汉语名称
系统中的ID有需要显示给使用者看的,统一用"编号",不要用ID或者编码 例如: SampleId 样本编号 正确 样本编码 错误 样本ID 错误 如果见到系统中有用到的,修正即可. ...
- AP创建会计科目
一. 创建会计科目的途径 1. 在发票工作台对单张发票进行创建科目: 2. 提交“创建会计科目”并发请求,对所有已经验证但尚未创建会计科目的发票进行创建会计科目. 二. 对单张发票创建会计科目 发票在 ...
- js数组中去除重复对象及去除空对象的方法
(function(){//去除数组中重复对象 var unique = {}; arr.forEach(function(a){ unique[ JSON.stringify(a) ] = 1 }) ...
- highCharts 饼图动态加载
饼图的动态加载 (1):导入样式 <script type="text/javascript" src="<%=request.getContextPath( ...
- Redis从基础命令到实战之有序集合类型(SortedSet)
有序集合类型是Redis五种数据类型中最高级的.也是最复杂的类型.有序集合具有集合类型的特性,在其基础上给每个元素关联了一个分值,或称为权重,操作时既可以在添加元素时指定分值,也可以单独修改集合中某一 ...