/** * Writes the selected rows to the document. * * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvas the <CODE>PdfContentByte</CODE> where the rows will * be written to * @return the y coordinate position of the bottom of the last row */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvas); }
/** * Writes the selected rows to the document. * * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvas the <CODE>PdfContentByte</CODE> where the rows will * be written to * @return the y coordinate position of the bottom of the last row */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvas); }
/** * Writes the selected rows to the document. * * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvas the <CODE>PdfContentByte</CODE> where the rows will * be written to * @return the y coordinate position of the bottom of the last row */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvas); }
/** * Writes the selected rows to the document. * <CODE>canvases</CODE> is obtained from <CODE>beginWritingRows()</CODE>. * * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvases an array of 4 <CODE>PdfContentByte</CODE> obtained from * <CODE>beginWrittingRows()</CODE> * @return the y coordinate position of the bottom of the last row * @see #beginWritingRows(com.lowagie.text.pdf.PdfContentByte) */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvases); }
/** * Writes the selected rows to the document. * <CODE>canvases</CODE> is obtained from <CODE>beginWritingRows()</CODE>. * * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvases an array of 4 <CODE>PdfContentByte</CODE> obtained from * <CODE>beginWrittingRows()</CODE> * @return the y coordinate position of the bottom of the last row * @see #beginWritingRows(com.lowagie.text.pdf.PdfContentByte) */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvases); }
/** * Writes the selected rows to the document. * <CODE>canvases</CODE> is obtained from <CODE>beginWritingRows()</CODE>. * * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvases an array of 4 <CODE>PdfContentByte</CODE> obtained from * <CODE>beginWrittingRows()</CODE> * @return the y coordinate position of the bottom of the last row * @see #beginWritingRows(com.lowagie.text.pdf.PdfContentByte) */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvases); }
public void modifyPdf(PdfStamper stamper) { this.reader = stamper.getReader(); PdfContentByte under = null; PdfPTable header = null; PdfPTable footer = null; final int total = this.reader.getNumberOfPages(); for (int page = 1; page <= total; page++) { under = stamper.getUnderContent(page); final PdfDocument doc = under.getPdfDocument(); final Rectangle rect = this.reader.getPageSizeWithRotation(page); header = ... //build your header footer = ... // build your footer final float x = 0; //write header to PDF if (header != null) { float y = (rect.getTop() - 0); header.writeSelectedRows(0, -1, x, y, under); } //write footer to PDF if (footer != null) { float y = (rect.getBottom() + 20); footer.writeSelectedRows(0, -1, x, y, under); } } }
if (yPos < 0) { stop = --i; tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas); document.newPage(); tbl.writeSelectedRows(12, -1, start, stop, 5, top, canvas); start = stop; document.newPage(); tbl.writeSelectedRows(0, 12, stop, -1, document.leftMargin(), top, canvas); document.newPage(); tbl.writeSelectedRows(12, -1, stop, -1, 5, top, canvas); document.close();
public void onStartPage(PdfWriter writer, Document document) { Font font = FontFactory.getFont(Globals.getFooterFontFamily(), Globals.getFooterFontSize(), Font.NORMAL, Color.BLACK); int howManyLogos = getHowManyLogos(); //No need to draw anything in the header if no logo was set in the report. if(howManyLogos == 0) return; float[] widths = fillWidthsArray(howManyLogos); PdfPTable foot = new PdfPTable(widths); if(AppUtils.isNotEmpty(pb.getLogo1Url()) && !pb.getLogo1Url().equalsIgnoreCase("<no logo>")) addLogo(foot, font, pb.getLogo1Url().substring(pb.getLogo1Url().indexOf("|") + 1).trim(), Cell.ALIGN_LEFT, pb.getLogo1Size() == null ? DEFAULT_LOGO_SIZE : pb.getLogo1Size()); PdfPCell spacingCell = new PdfPCell(); spacingCell.setBorderColor(Color.WHITE); foot.addCell(spacingCell); //Using logo1 size for now - use logo2 size if it is required to deal it separately. if(AppUtils.isNotEmpty(pb.getLogo2Url()) && !pb.getLogo2Url().equalsIgnoreCase("<no logo>")) addLogo(foot, font, pb.getLogo2Url().substring(pb.getLogo2Url().indexOf("|") + 1).trim(), Cell.ALIGN_RIGHT, pb.getLogo2Size() == null ? DEFAULT_LOGO_SIZE : pb.getLogo2Size()); foot.setTotalWidth(getPageWidth(document)); foot.writeSelectedRows(0, -1, 36, 600, writer.getDirectContent()); }
public class MyPageEventListener extends PdfPageEventHelper { . . . @Override public void onEndPage(PdfWriter writer, Document document) { //code skeleton to write page header PdfPTable tbl = new PdfPTable(3); tbl.addCell("1st cell"); tbl.addCell("2nd cell"); tbl.addCell("3rd cell"); float x = document.leftMargin(); float hei = getMyHeaderHeight(); //custom method that return header's height //align bottom between page edge and page margin float y = document.top() + hei; //write the table tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent()); } }
Document document = new Document(PageSize.A4, 30, 30, 150, 50); FileOutputStream fos = new FileOutputStream("filepath"); PdfWriter writer = PdfWriter.getInstance(document,fos ); document.open(); Rectangle rect = writer.getPageSize(); float[] columnWidths = {2f, 1f,2.5f}; PdfPTable table = new PdfPTable(columnWidths); table.setTotalWidth(527); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell("abc left"); table.addCell(""); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell("abc right"); table.writeSelectedRows( 0, -1, rect.getLeft(30), rect.getTop(20), writer.getDirectContent()); document.close();
private void writeFooterTable(PdfWriter writer, Document document, PdfPTable table) { final int FIRST_ROW = 0; final int LAST_ROW = -1; //Table must have absolute width set. if(table.getTotalWidth()==0) table.setTotalWidth((document.right()-document.left())*table.getWidthPercentage()/100f); table.writeSelectedRows(FIRST_ROW, LAST_ROW, document.left(), document.bottom()+table.getTotalHeight(),writer.getDirectContent()); }
tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
public static void main(String[] args) throws DocumentException, FileNotFoundException, IOException { String TARGET = "temp.pdf"; Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TARGET)); document.open(); PdfPTable table = new PdfPTable(7); for (int i = 0; i < 700; i++) { Phrase p = new Phrase("some text"); PdfPCell cell = new PdfPCell(); cell.addElement(p); table.addCell(cell); } table.setTotalWidth(PageSize.A4.getWidth()); table.setLockedWidth(true); PdfContentByte canvas = writer.getDirectContent(); PdfTemplate template = canvas.createTemplate(table.getTotalWidth(), table.getTotalHeight()); table.writeSelectedRows(0, -1, 0, table.getTotalHeight(), template); Image img = Image.getInstance(template); img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()); img.setAbsolutePosition(0, (PageSize.A4.getHeight() - table.getTotalHeight()) / 2); document.add(img); document.close(); }
/** * Generates the end page for this transaction report * * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { try { Rectangle page = document.getPageSize(); PdfPTable head = new PdfPTable(3); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); PdfPCell cell = new PdfPCell(new Phrase(sdf.format(runDate), headerFont)); cell.setBorder(Rectangle.NO_BORDER); head.addCell(cell); cell = new PdfPCell(new Phrase(title, headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); head.addCell(cell); cell = new PdfPCell(new Phrase("Page: " + new Integer(writer.getPageNumber()), headerFont)); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); head.addCell(cell); head.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); } catch (Exception e) { throw new ExceptionConverter(e); } } }
public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); Anchor target = new Anchor("top"); target.setName("page18"); document.add(target); PdfPTable table = new PdfPTable(1); table.setTotalWidth(500); PdfPCell cell = new PdfPCell(); Paragraph p = new Paragraph(); Anchor anchor = new Anchor("page18 link"); anchor.setReference("#page18"); p.add(anchor); cell.addElement(p); table.addCell(cell); table.writeSelectedRows(0, -1, 36, 700, writer.getDirectContent()); document.close(); }
head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());
@Override public void onEndPage(final PdfWriter writer, final Document document) { try { Image logoImage = Image.getInstance(logo); Rectangle page = document.getPageSize(); PdfPTable head = new PdfPTable(4); head.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE); head.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER); head.addCell(logoImage); Phrase projectName = new Phrase(project.getName(), FontFactory.getFont( FontFactory.COURIER, 12, Font.NORMAL, Color.GRAY)); Phrase phrase = new Phrase("Sonar PDF Report", FontFactory.getFont( FontFactory.COURIER, 12, Font.NORMAL, Color.GRAY)); head.getDefaultCell().setColspan(2); head.addCell(phrase); head.getDefaultCell().setColspan(1); head.addCell(projectName); head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - 20, writer.getDirectContent()); head.setSpacingAfter(10); } catch (BadElementException e) { LOG.error("Can not generate PDF header", e); } catch (MalformedURLException e) { LOG.error("Can not generate PDF header", e); } catch (IOException e) { LOG.error("Can not generate PDF header", e); } }
/** * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the * footer. * * @param writer The PdfWriter for this document. * @param document The document. * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { LOG.debug("onEndPage() started."); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // write the headerTable headerTable.setTotalWidth(document.right() - document.left()); headerTable.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 10, cb); // compose the footer String text = "Page " + writer.getPageNumber() + " of "; float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(helv, 12); // show the footer float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); cb.saveState(); }
table.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 50, cb);