Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit a PdfTable in next page
    text
    copied!<p>How to split a <code>PdfTable</code> in next page in Java?</p> <p>I am generating a <strong>pdf</strong> document using <strong>itext</strong> in java. The pdf has a header and a table. When the table continues to the next page, two rows of the table overlaps into the header. How to display the remaining table rows below the header in the next page. Here I am using <strong>Header_Footer java class</strong> and call the object of the Header_Footer in the <strong>jsp</strong> page like-</p> <pre><code>Header_Footer event = new Header_Footer(); writer.setPageEvent(event); </code></pre> <p>My Header_Footer class is give below</p> <pre><code>/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mis4acjml; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.ExceptionConverter; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; /** * * @author kanika */ public class Header_Footer extends PdfPageEventHelper{ String header; PdfTemplate total; /** * Allows us to change the content of the header. * @param header The new header String */ public Header_Footer() { header="The Assam Co-operative Jute Mills Ltd."; } public void setHeader(String header) { this.header = header; } /** * Creates the PdfTemplate that will hold the total number of pages. * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onOpenDocument(PdfWriter writer, Document document) { total = writer.getDirectContent().createTemplate(30, 16); } /** * Adds a header to every page * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { PdfPTable table = new PdfPTable(3); try { table.setWidths(new int[]{24, 24, 2}); table.setTotalWidth(527); table.setLockedWidth(true); table.getDefaultCell().setFixedHeight(20); table.getDefaultCell().setBorder(Rectangle.BOTTOM); table.addCell(header); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell(String.format("Page %d of", writer.getPageNumber())); PdfPCell cell = new PdfPCell(Image.getInstance(total)); cell.setBorder(Rectangle.BOTTOM); table.addCell(cell); table.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent()); } catch (DocumentException de) { throw new ExceptionConverter(de); } } /** * Fills out the total number of pages before the document is closed. * * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onCloseDocument(PdfWriter writer, Document document) { ColumnText.showTextAligned(total, Element.ALIGN_LEFT,new Phrase(String.valueOf(writer.getPageNumber() - 1)),2, 2, 0); } } ` </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload