Monday, 14 November 2016

Itext: Write selected rows from table to a document

By using 'writeSelectedRows' method of PdfPTable class, you can write selected rows to the document.

public float writeSelectedRows(final int rowStart, final int rowEnd, final float xPos, final float yPos, final PdfContentByte[] canvases)

public float writeSelectedRows(final int colStart, final int colEnd, final int rowStart, final int rowEnd, final float xPos, final float yPos, final PdfContentByte[] canvases)

public float writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, final float xPos, float yPos, final PdfContentByte[] canvases, final boolean reusable)

public float writeSelectedRows(final int rowStart, final int rowEnd, final float xPos, final float yPos, final PdfContentByte canvas)

public float writeSelectedRows(final int colStart, final int colEnd, final int rowStart, final int rowEnd, final float xPos, final float yPos, final PdfContentByte canvas)


public float writeSelectedRows(int colStart, int colEnd, final int rowStart, final int rowEnd, final float xPos, final float yPos, final PdfContentByte canvas, final boolean reusable)
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestTable {
 private static final String fileName = "/Users/harikrishna_gurram/Documents/itext/tableEx.pdf";

 private static PdfPTable getSquareTable() {
  PdfPTable table = new PdfPTable(2);
  table.setHeaderRows(1);
  PdfPCell cell = new PdfPCell();

  cell.setPhrase(new Phrase("Number"));
  table.addCell(cell);

  cell.setPhrase(new Phrase("Square"));
  table.addCell(cell);

  for (int i = 1; i < 10; i++) {

   cell.setPhrase(new Phrase(Integer.toString(i)));
   table.addCell(cell);

   cell.setPhrase(new Phrase(Integer.toString(i * i)));
   table.addCell(cell);
  }
  return table;

 }

 public static void main(String args[]) throws DocumentException, MalformedURLException, IOException {

  Document document = new Document();
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));

  document.open();
  PdfContentByte canvas = writer.getDirectContent();

  PdfPTable table = getSquareTable();
  document.add(table);

  /* Add Even rows from the table */
  table.writeSelectedRows(5, 9, 100, 300, canvas);

  document.close();
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment