List of usage examples for com.lowagie.text.pdf PdfWriter getPageNumber
public int getPageNumber()
From source file:org.sonarqube.report.extendedpdf.ExtendedEvents.java
License:Open Source License
private void printPageNumber(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();/*from w w w . j av a2 s . c o m*/ float textBase = document.bottom() - 45; try { cb.setFontAndSize(BaseFont.createFont("Helvetica", BaseFont.WINANSI, false), 12); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } cb.beginText(); cb.setTextMatrix(document.right() - 10, textBase); cb.showText(String.valueOf(writer.getPageNumber())); cb.endText(); cb.saveState(); }
From source file:org.webguitoolkit.ui.util.export.PDFEvent.java
License:Apache License
public void onEndPage(PdfWriter writer, Document document) { TableExportOptions exportOptions = wgtTable.getExportOptions(); try {/* ww w. j av a 2 s.co m*/ Rectangle page = document.getPageSize(); if (exportOptions.isShowDefaultHeader() || StringUtils.isNotEmpty(exportOptions.getHeaderImage())) { PdfPTable head = new PdfPTable(3); head.getDefaultCell().setBorder(Rectangle.NO_BORDER); Paragraph title = new Paragraph(wgtTable.getTitle()); title.setAlignment(Element.ALIGN_LEFT); head.addCell(title); Paragraph empty = new Paragraph(""); head.addCell(empty); if (StringUtils.isNotEmpty(exportOptions.getHeaderImage())) { try { URL absoluteFileUrl = wgtTable.getPage().getClass() .getResource("/" + exportOptions.getHeaderImage()); if (absoluteFileUrl != null) { String path = absoluteFileUrl.getPath(); Image jpg = Image.getInstance(path); jpg.scaleAbsoluteHeight(40); jpg.scaleAbsoluteWidth(200); head.addCell(jpg); } } catch (Exception e) { logger.error(e.getMessage()); Paragraph noImage = new Paragraph("Image not found!"); head.addCell(noImage); } } else { head.addCell(empty); } head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); } if (exportOptions.isShowDefaultFooter() || StringUtils.isNotEmpty(exportOptions.getFooterText()) || exportOptions.isShowPageNumber()) { PdfPTable foot = new PdfPTable(3); String footerText = exportOptions.getFooterText() != null ? exportOptions.getFooterText() : ""; if (!exportOptions.isShowDefaultFooter()) { foot.addCell(new Paragraph(footerText)); foot.addCell(new Paragraph("")); } else { foot.getDefaultCell().setBorder(Rectangle.NO_BORDER); String leftText = ""; if (StringUtils.isNotEmpty(exportOptions.getFooterText())) { leftText = exportOptions.getFooterText(); } Paragraph left = new Paragraph(leftText); left.setAlignment(Element.ALIGN_LEFT); foot.addCell(left); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, TextService.getLocale()); Date today = new Date(); String date = df.format(today); Paragraph center = new Paragraph(date); center.setAlignment(Element.ALIGN_CENTER); foot.addCell(center); } if (exportOptions.isShowPageNumber()) { Paragraph right = new Paragraph( TextService.getString("pdf.page@Page:") + " " + writer.getPageNumber()); right.setAlignment(Element.ALIGN_LEFT); foot.addCell(right); foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } else { foot.addCell(new Paragraph("")); } } } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:questions.forms.RadioButtonsOnDifferentPages.java
public static void main(String[] args) { Document document = new Document(); try {/*from w ww . j a v a 2 s . co m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); String[] languages = { "English", "French", "Dutch" }; Rectangle rect; // create radio button field and its kids PdfFormField language = PdfFormField.createRadioButton(writer, true); language.setFieldName("language"); language.setValueAsName(languages[0]); for (int i = 0; i < languages.length; i++) { rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40); addRadioButton(writer, rect, language, languages[i], i == 0, writer.getPageNumber() + i); } writer.addAnnotation(language); // add the page content for (int i = 0; i < languages.length; i++) { cb.beginText(); cb.setFontAndSize(bf, 18); cb.showTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0); cb.endText(); document.newPage(); } } catch (Exception e) { e.printStackTrace(); } // step 5: we close the document document.close(); }
From source file:questions.tables.AddTableAsHeaderFooter.java
public void onEndPage(PdfWriter writer, Document document) { try {/* w ww. ja v a2 s .c om*/ // Header headerTable.writeSelectedRows(0, -1, document.leftMargin(), document.top() + headerTable.getTotalHeight(), writer.getDirectContent()); // Footer PdfPTable footerTable = new PdfPTable(2); PdfPCell cell1 = new PdfPCell(new Phrase("page " + writer.getPageNumber())); footerTable.addCell(cell1); PdfPCell cell2 = new PdfPCell(Image.getInstance(tpl)); footerTable.addCell(cell2); footerTable.setTotalWidth(document.right() - document.left()); footerTable.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:questions.tables.AddTableAsHeaderFooter.java
public void onCloseDocument(PdfWriter writer, Document document) { tpl.beginText();// ww w .jav a 2s. c o m tpl.setFontAndSize(helv, 12); tpl.setTextMatrix(2, 4); tpl.showText("Number of pages = " + (writer.getPageNumber() - 1)); tpl.endText(); }
From source file:questions.tables.TableColumns.java
public static void main(String[] args) { Document document = new Document(PageSize.A4.rotate()); try {/* w w w. j ava2s. c om*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); // the content of the columns ColumnText content = new ColumnText(writer.getDirectContent()); PdfPTable items = new PdfPTable(2); items.setWidthPercentage(100); for (int i = 0; i < 100; ++i) { items.addCell("item " + i); items.addCell("some item"); } content.addElement(items); // adding the stuff to the document int column = 0; float height = 0; float[][] x = { { document.left(), document.left() + 380 }, { document.right() - 380, document.right() } }; int status = ColumnText.START_COLUMN; while (ColumnText.hasMoreText(status)) { if (column == 0) { PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); table.addCell("EmployeeSheets"); table.addCell("Page " + writer.getPageNumber()); document.add(table); height = table.getTotalHeight(); } content.setSimpleColumn(x[column][0], document.bottom(), x[column][1], document.top() - height - 10); status = content.go(); if (++column >= x.length) { column = 0; document.newPage(); } } } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } document.close(); }
From source file:questions.tables.TablesWriteSelected.java
public static void main(String[] args) { Document document = new Document(PageSize.A4.rotate()); try {/*w ww.ja v a 2 s . c o m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); // the content of the columns PdfPTable items = new PdfPTable(2); items.setTotalWidth(TOTAL_WIDTH); for (int i = 0; i < 100; ++i) { items.addCell("item " + i); items.addCell("some item"); } int rows = items.size(); // adding the stuff to the document int column = 0; int start; int end = 0; int row = 0; float available = 0; float[][] x = { { document.left(), document.left() + TOTAL_WIDTH }, { document.right() - TOTAL_WIDTH, document.right() } }; while (row < rows) { start = row; if (column == 0) { PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); table.addCell("EmployeeSheets"); table.addCell("Page " + writer.getPageNumber()); document.add(table); available = document.top() - table.getTotalHeight() - 10 - document.bottom(); } float needed = items.getRowHeight(start); while (needed < available && row < rows) { needed += items.getRowHeight(++row); end = row; } items.writeSelectedRows(start, end, x[column][0], document.bottom() + available, writer.getDirectContent()); if (++column >= x.length) { column = 0; document.newPage(); } } } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } document.close(); }
From source file:s2s.report.MyPageEvents.java
License:GNU General Public License
@Override public void onStartPage(PdfWriter writer, Document m_document) { if (!bShowHeader) { return;//from ww w .j a v a 2s . co m } int pageN = writer.getPageNumber(); if (!m_writeIndex) { if (!IndexAlreadyWrited) { pageN += m_iCount; } } else { pageN = ++m_iCurrentIndexPage; } PdfPTable tbl = new PdfPTable(3); String text = " " + ApplicationConfigurator.LanguageManager.getString("Pag") + " " + pageN + " " + ApplicationConfigurator.LanguageManager.getString("Di") + " "; tbl.setTotalWidth(m_rect.getWidth() - 60); tbl.addCell(new Phrase(strTopLeft, REPORT_SETTINGS.ftText10)); PdfPCell pc = new PdfPCell(new Phrase(strTopCenter, REPORT_SETTINGS.ftText11)); pc.setHorizontalAlignment(Element.ALIGN_CENTER); tbl.addCell(pc); Phrase ph = new Phrase(text, REPORT_SETTINGS.ftText10); tbl.addCell(ph); tbl.writeSelectedRows(0, -1, 30, m_rect.getHeight() - 30, cb); cb.addTemplate(template, bRotate ? 647 : 500, m_rect.getHeight() - 30 - 12); }
From source file:s2s.report.MyPageEvents.java
License:GNU General Public License
@Override public void onCloseDocument(PdfWriter writer, Document m_document) { int pageN = writer.getPageNumber() - 1; template.beginText();/* www.ja v a2s.com*/ template.setFontAndSize(bf, 10); template.showText(String.valueOf(pageN)); template.endText(); }