Java PDF: Ajouter un événement aux cellules de la table

Author:

Java PDF: Ajouter un événement aux cellules de la table
Download

/***** Code de MesExemples.com *******/
/**
 * @(#)CellEventsPDF.java
 *
 *
 * @author 
 * @version 1.00 2013/5/29
 */
 
 
 
import java.io.FileOutputStream;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
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;
 
public class CellEventsPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter pdf_writer = PdfWriter.getInstance(document, 
      	new FileOutputStream("c:/CellEventsPDF.pdf"));
      document.open();
      PdfPTable table = new PdfPTable(1);
      PdfPCell cell = new PdfPCell();
      cell.setCellEvent(new MyCellEvent());
      cell.setPhrase(new Phrase("asdfasdf"));
      table.addCell(cell);
      table.setTotalWidth(300);
 
      document.add(table);
    } catch (Exception de) {
      de.printStackTrace();
    }
    document.close();
  }
 
}
 
class MyCellEvent implements PdfPCellEvent{
  public void cellLayout(PdfPCell cell, Rectangle position,
      PdfContentByte[] canvases) {
    PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
    cb.moveTo(position.getLeft(), position.getBottom());
    cb.lineTo(position.getRight(), position.getTop());
    cb.stroke();
  }  
}

Cet article Java PDF: Ajouter un événement aux cellules de la table est apparu en premier sur .