Foros del Web » Programación para mayores de 30 ;) » Java »

create table with itext

Estas en el tema de create table with itext en el foro de Java en Foros del Web. Hi friend, I took advantage of the opportunity to share this, hope this helps someone , Código PHP: import com . lowagie . text . Document ...
  #1 (permalink)  
Antiguo 19/05/2011, 16:20
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 14 años
Puntos: 5
create table with itext

Hi friend, I took advantage of the opportunity to share this, hope this helps someone ,

Código PHP:

import com
.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;

import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import java.awt.Color;

import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.ResourceBundle;

import javax.swing.JTextArea;
import javax.swing.JTextField;

public class 
FrmtClaimSinister {

    
JTextField ttlTitulo = new JTextField(contenido.getString("frmtClaimsin.titulo"));
    
JTextArea ttlFormatoEfec = new JTextArea("                           "+contenido.getString("frmtClaimsin.cadcomeroxxo") + "\n" "\n" +  
                                        
"                 "+contenido.getString("frmtClaimsin.repmensinefec"));
    
JTextArea ttlFormatoMerc = new JTextArea("                           "+contenido.getString("frmtClaimsin.cadcomeroxxo") + "\n" "\n" +  
                                        
"                 "+contenido.getString("frmtClaimsin.repmensinmerc"));                                        
    
JTextField ttlEfectivo = new JTextField(contenido.getString("frmtClaimsin.efectivo"));
    
JTextField ttlMercancia = new JTextField(contenido.getString("frmtClaimsin.merchandise"));
    
JTextField ttlCadenaComerOXXO = new JTextField(contenido.getString("frmtClaimsin.cadcomeroxxo"));
    
JTextField ttlTituloEfec = new JTextField(contenido.getString("frmtClaimsin.repmensinefec"));
    
JTextField ttlTituloMerc = new JTextField(contenido.getString("frmtClaimsin.repmensinmerc"));
    
JTextField ttlPlaza = new JTextField(contenido.getString("frmtClaimsin.plaza"));
    
JTextField ttlFecha  = new JTextField(contenido.getString("frmtClaimsin.fecha"));
    
JTextField ttlPoliEfec = new JTextField(contenido.getString("frmtClaimsin.cashpolicy"));
    
JTextField ttlPoliMerc = new JTextField(contenido.getString("frmtClaimsin.mercpolicy"));
    
JTextField ttlConsec = new JTextField(contenido.getString("frmtClaimsin.consec"));
    
JTextField ttlTienda    = new JTextField(contenido.getString("frmtClaimsin.store"));
    
JTextField ttlFechaSin  = new JTextField(contenido.getString("frmtClaimsin.sinisterdate"));
    
JTextField ttlFechaX    = new JTextField(contenido.getString("frmtClaimsin.datex"));
    
JTextField ttlFechaY    = new JTextField(contenido.getString("frmtClaimsin.datey"));
    
JTextField ttlFolio = new JTextField(contenido.getString("frmtClaimsin.folio"));
    
JTextField ttlComisionista = new JTextField(contenido.getString("frmtReportAdic.namecom"));
    
JTextField ttlImporteTotRec  = new JTextField(contenido.getString("frmtClaimsin.importotrec")); 
    
JTextField ttlImporteTotRecla = new JTextField(contenido.getString("frmtClaimsin.importotrecla"));
    
JTextField ttlObservaciones = new JTextField(contenido.getString("frmtClaimsin.note"));
    
JTextArea ttlTotal  = new JTextArea(contenido.getString("frmtClaimsin.total"));
    
JTextArea ttlOtraCol  = new JTextArea(contenido.getString("frmtClaimsin.otracol"));
    
JTextArea ttlElabora  = new JTextArea(contenido.getString("frmtClaimsin.elabora"));
    
JTextArea ttlAutoriza  = new JTextArea(contenido.getString("frmtClaimsin.autoriza"));
    
JTextArea ttlEncargado  = new JTextArea(contenido.getString("frmtClaimsin.encargado"));
    
JTextArea ttlJefeAdmvo  = new JTextArea(contenido.getString("frmtClaimsin.jefeadmvo"));
    
JTextArea ttlJefeRH  = new JTextArea(contenido.getString("frmtClaimsin.jeferh"));
    
JTextArea ttlGerOper  = new JTextArea(contenido.getString("frmtClaimsin.gerente"));
    
JTextArea ttlFima = new JTextArea (contenido.getString("frmtReportAdic.nameandsign"));
    
JTextArea ttlLegenda4  = new JTextArea(contenido.getString("frmtTesoreria.content3"));

    public 
Font FUENTE_NUM1 = new Font(Font.TIMES_ROMAN,7,Font.BOLD);
    public 
Font FUENTE_NUM2 = new Font(Font.TIMES_ROMAN9,Font.BOLD);
    public 
Font FUENTE_NUM3 = new Font(Font.TIMES_ROMAN13,Font.BOLD); 
    protected static 
ResourceBundle contenido ResourceBundle.getBundle("com.femsa.oxxo.siniestros.ApplicationResources");
    private  
Document document = new Document(PageSize.LEGAL);
    
    public 
FrmtClaimSinister() {
    }
    
    public static 
void main(String [] args) {
        new 
FrmtClaimSinister().generaPdf();
    }
    
    public  
void generaPdf() {
        
    
Document document = new Document(PageSize.LEGAL);
    
PdfPCell cell;
    
//document.setMargins(40,40,42,40) ;//Right,Left,Top,Bottom 
    
    
try {

        
//            PdfWriter.getInstance(document,resp.getOutputStream()); //FIXME for web
        
PdfWriter.getInstance(document, new FileOutputStream("c:/tmp_Siniestros/prueba.pdf"));
        
document.open();
        
Image img1 Image.getInstance("/JDeveloper10132/jdev/mywork/JDK4_4/ViewController/public_html/css/images/OXXOwm2.png");//agrega imagen
        //            Image img1 = Image.getInstance(this.getClass().getClassLoader().getResource("/css/images/OXXOwm2.png"));//agrega imagen //FIXME FOR WEB
        
img1.scaleToFit(600600);//escala de la imagen
        
img1.setAlignment(Image.MIDDLEImage.UNDERLYING);//imagen centrada y de fondo.
        
document.add(img1);
        
        
pintaEncabezado(ttlTitulo,ttlEfectivo,ttlFormatoEfec,ttlPoliEfec,document);
        
        
document.add(getTablaInfo());
        
        
float[] width6 = { 6f,15f};  
        
PdfPTable comentario = new PdfPTable(1);
        
comentario.setWidthPercentage(100f); 
        
comentario.setSpacingBefore(10f);

        
cell = new PdfPCell(new Paragraph("rerererr",FUENTE_NUM2));
        
cell.setBorderWidth(0);
        
cell.setPaddingTop(30f);
        
cell.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
        
cell.setVerticalAlignment(Element.ALIGN_JUSTIFIED);
        
comentario.addCell(cell);
        
document.add(comentario);    
        
        
pintaEncabezado(null,ttlMercancia,ttlFormatoMerc,ttlPoliMerc,document);
        
       
        
document.add(getTablaFima());

    }
    catch (
Exception ioe
    {
            
System.err.println(ioe.getMessage());
    }
    
document.close();
    } 
Cheers
  #2 (permalink)  
Antiguo 19/05/2011, 16:28
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 14 años
Puntos: 5
Respuesta: create table with itext

Oops!, There I go again, put the remaining code here.

Código PHP:
public void pintaEncabezado(JTextField tituloini,JTextField textJTextArea textArea,JTextField tipoliza,Document document) {
        try{


            
//            PdfWriter.getInstance(document,resp.getOutputStream()); //FIXME for web
//                    PdfWriter.getInstance(document, new FileOutputStream("c:/tmp_Siniestros/prueba.pdf"));
//                    document.open();
//                    Image img1 = Image.getInstance("/JDeveloper10132/jdev/mywork/JDK4_4/ViewController/public_html/css/images/OXXOwm2.png");//agrega imagen
//            //            Image img1 = Image.getInstance(this.getClass().getClassLoader().getResource("/css/images/OXXOwm2.png"));//agrega imagen //FIXME FOR WEB
//                    img1.scaleToFit(600, 600);//escala de la imagen
//                    img1.setAlignment(Image.MIDDLE| Image.UNDERLYING);//imagen centrada y de fondo.
//                    document.add(img1);
                    
Image img2 Image.getInstance("/JDeveloper10132/jdev/mywork/JDK4_4/ViewController/public_html/css/images/Logo_OXXO.gif");//agrega imagen
            //            Image img2 = Image.getInstance(this.getClass().getClassLoader().getResource("/css/images/Logo_OXXO.gif"));//agrega imagen //FIXME FOR WEB
                    
img2.scaleToFit(100100);//escala de la imagen
                    
img2.setAlignment(Image.ALIGN_LEFT);//imagen centrada y de fondo.
                    
                //[TABLA #1] Encabezado
                     
PdfPCell cell;
                     
PdfPTable titulo = new PdfPTable(1);
                     
//titulo.setSpacingAfter(10f);
                     
titulo.setWidthPercentage(100);
                     
                     
cell = new PdfPCell(new Paragraph(tituloini!=null?tituloini.getText():"",FUENTE_NUM3));
                     
cell.setBorder(0);
                     
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                     
cell.setVerticalAlignment(Element.ALIGN_TOP);
                     
titulo.addCell(cell);
                     
                     
cell = new PdfPCell(new Paragraph(text.getText(),FUENTE_NUM3));
                     
cell.setBorder(0);
                     
cell.setPaddingTop(10f);
                     
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                     
cell.setVerticalAlignment(Element.ALIGN_TOP);
                     
titulo.addCell(cell);
                     
document.add(titulo);
                
                     
float[] widths = {83f,346f };//ancho tabla
                     
PdfPTable table = new PdfPTable(widths);
                     
                     
cell = new PdfPCell(img2);
                     
cell.setFixedHeight(50f);
                     
cell.setBorder(0);
                     
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                     
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                     
table.addCell(cell);  
                     
                     
cell = new PdfPCell(new Paragraph(textArea.getText(), FUENTE_NUM2));
                     
cell.setBorder(0);
                     
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                     
cell.setVerticalAlignment(Element.ALIGN_TOP);
                     
table.addCell(cell);
                     
        
//             float[] widths = {83f,346f };//ancho tabla
        //             Rectangle r = new Rectangle(PageSize.A4.getRight(150), PageSize.A4.getTop(120));
                     
table.setWidthPercentage(100);
                     
table.setHorizontalAlignment(Element.ALIGN_LEFT);
                     
table.setSpacingBefore(20f);
                    
            
//            float[] widths = { 95f,330f };//ancho tabla
            //            Rectangle r = new Rectangle(PageSize.A4.getRight(150), PageSize.A4.getTop(120));
            //            table.setWidthPercentage(widths, r);
                    
                     
table.setHorizontalAlignment(Element.ALIGN_LEFT);
                     
table.setSpacingBefore(20);
                     
document.add(table);//AGREGAR TABLA #1 AL DOCUMENTO
                     
                      ////////tabla de plaza y fecha
                      
float[] width4 = { 11f,15f,11f,15f};  
                      
PdfPTable plazfec = new PdfPTable(width4);
                      
plazfec.setWidthPercentage(100f); 
                      
plazfec.setSpacingBefore(10f);
                      
                      
cell = new PdfPCell(new Paragraph(ttlPlaza.getText(),FUENTE_NUM2));
                      
cell.setBorder(0);
                      
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                      
cell.setVerticalAlignment(Element.ALIGN_TOP);
                      
plazfec.addCell(cell);
                      
                      
cell = new PdfPCell(new Paragraph("ok",FUENTE_NUM2));
                      
cell.setBorder(2);
                      
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                      
cell.setVerticalAlignment(Element.ALIGN_TOP);
                      
plazfec.addCell(cell); 
                      
                      
cell = new PdfPCell(new Paragraph(ttlFecha.getText(),FUENTE_NUM2));
                      
cell.setBorder(0);
                      
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                      
cell.setVerticalAlignment(Element.ALIGN_TOP);
                      
plazfec.addCell(cell);
                      
                      
cell = new PdfPCell(new Paragraph("ok",FUENTE_NUM2));
                      
cell.setBorder(2);
                      
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                      
cell.setVerticalAlignment(Element.ALIGN_TOP);
                      
plazfec.addCell(cell);
                                 
                      
document.add(plazfec);
                      
                      
PdfPTable poliza = new PdfPTable(1);
                      
poliza.setWidthPercentage(100f);
                      
poliza.setSpacingBefore(10f);
                      
                      
cell = new PdfPCell(new Paragraph(tipoliza.getText(),FUENTE_NUM2));
                      
cell.setBorder(0);
                      
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                      
cell.setVerticalAlignment(Element.ALIGN_TOP);
                      
poliza.addCell(cell);
                      
document.add(poliza);

                      
///fin tabla fecha y plaza
                     
                      
}
                      
                      catch(
DocumentException de){
                          
System.err.println(de.getMessage());
                      }
                      catch(
IOException ioe){
                          
System.err.println(ioe.getMessage());
                      }
    } 
Cheers
  #3 (permalink)  
Antiguo 19/05/2011, 16:30
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 14 años
Puntos: 5
Respuesta: create table with itext


Código PHP:
/**
     * Crea celda de tabla para pdf
     * @param JTextField y tipo de fuente
     * @return Cell
     */
    
public  PdfPCell getCell(JTextField text,Font fuente) {
        
PdfPCell celda;
        
celda = new PdfPCell(new Paragraph(text.getText(),fuente));
        
celda.setBorderWidth(1);
        
celda.setHorizontalAlignment(Element.ALIGN_CENTER);
        
celda.setVerticalAlignment(Element.ALIGN_MIDDLE);
        
//celda.setBorderWidthBottom(2);
        
return celda;
        
    }
    
    
/**
     * Crea celda de tabla para pdf
     * @param JTextField ,tipo de fuente y ancho del borde
     * @return Cell
     */
    
    
public  PdfPCell getCell(JTextField text,Font fuenteint border) {
        
PdfPCell celda;
            
celda = new PdfPCell(new Paragraph(text.getText(),fuente));
            
celda.setBorderWidth(1);
            
celda.setHorizontalAlignment(Element.ALIGN_CENTER);
            
celda.setVerticalAlignment(Element.ALIGN_MIDDLE);
            
celda.setBorderWidthBottom(border);
        return 
celda;
    }
    
    
/**
     * Crea celda de tabla para pdf
     * @param JTextField ,tipo de fuente y Color
     * @return Cell
     */
     
    
public  PdfPCell getCell(JTextField text,Font fuente,Color color) {
        
PdfPCell celda;
            
celda = new PdfPCell(new Paragraph(text.getText(),fuente));
            
celda.setBorderWidth(1);
            
celda.setBackgroundColor(color);
            
celda.setHorizontalAlignment(Element.ALIGN_CENTER);
            
celda.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return 
celda;
    }
    
    
/**
     * Crea celda de tabla para pdf
     * @param String ,tipo de fuente, orientacion horizontal y vertical, ancho del borde
     * @return Cell
     */
     
    
public  PdfPCell getCell(String text,Font fuente,int alh,int alv,int border) {
        
PdfPCell celda;
            
celda = new PdfPCell(new Paragraph(text,fuente));
            
celda.setBorderWidth(1);
            
celda.setHorizontalAlignment(alh);
            
celda.setVerticalAlignment(alv);
            
celda.setBorderWidthBottom(border);
        return 
celda;   
    }
    
    
/**
     * Create cell 
     * @param String ,tipo de fuente, orientacion horizontal y vertical
     * @return Cell
     */
    
    
public  PdfPCell getCell(String text,Font font,int alh,int alv) {
        
PdfPCell celda;
            
celda = new PdfPCell(new Paragraph(text,font));
            
celda.setBorderWidth(1);
            
celda.setHorizontalAlignment(alh);
            
celda.setVerticalAlignment(alv);
        return 
celda;
    }
   

  #4 (permalink)  
Antiguo 19/05/2011, 16:32
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 14 años
Puntos: 5
Respuesta: create table with itext

Código PHP:
 /**
     * Crea tabla de informacion
     * @return Tabla
     */
   
public PdfPTable getTablaInfo() throws DocumentException {
          
          
PdfPTable datatable = new PdfPTable(8);
          
datatable.setSpacingBefore(10f);
            
//int headerwidths[] = {12, 9, 10, 15, 11,13, 11, 15 };
          
int headerwidths[] = {1189913,131115 };
          
datatable.setWidths(headerwidths);
          
datatable.setWidthPercentage(100);
          
datatable.getDefaultCell().setPadding(5);
          
datatable.getDefaultCell().setBorderWidth(1);
          
          
/***headers table****/ 
          
datatable.addCell(getCell(ttlConsec,FUENTE_NUM1,Color.LIGHT_GRAY));
          
datatable.addCell(getCell(ttlTienda,FUENTE_NUM1,Color.LIGHT_GRAY));
          
datatable.addCell(getCell(ttlFechaSin,FUENTE_NUM1,Color.LIGHT_GRAY));
          
datatable.addCell(getCell(ttlFechaX,FUENTE_NUM1,Color.LIGHT_GRAY));
          
datatable.addCell(getCell(ttlFolio,FUENTE_NUM1,Color.LIGHT_GRAY));
          
datatable.addCell(getCell(ttlImporteTotRecla,FUENTE_NUM1,Color.LIGHT_GRAY));
          
datatable.addCell(getCell(ttlImporteTotRec,FUENTE_NUM1,Color.LIGHT_GRAY));
          
datatable.addCell(getCell(ttlObservaciones,FUENTE_NUM1,Color.LIGHT_GRAY));
          
/***end headers table****/ 
          
           /***info table****/ 
         
for (int i =0;i<=2;i++){  
         
           
datatable.addCell(getCell("1",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
           
datatable.addCell(getCell("2",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
           
datatable.addCell(getCell("3",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
           
datatable.addCell(getCell("4",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
           
datatable.addCell(getCell("5",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
           
datatable.addCell(getCell("6",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
           
datatable.addCell(getCell("7",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
           
datatable.addCell(getCell("8",FUENTE_NUM1,Element.ALIGN_CENTER,Element.ALIGN_TOP));
         }
           
/***end info table****/ 
           
          
          /***total table****/ 
           
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
           
datatable.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM);
           
datatable.getDefaultCell().setColspan(5);
          
          
           
PdfPCell cel = new PdfPCell(new Paragraph(ttlTotal.getText(),FUENTE_NUM1));
           
cel.setBorderWidth(1);
           
cel.setHorizontalAlignment(Element.ALIGN_RIGHT);
           
cel.setVerticalAlignment(Element.ALIGN_BOTTOM);
           
cel.setColspan(5);
          
           
datatable.addCell(cel);
        
//    datatable.getDefaultCell().setColspan(1);

        //    datatable.addCell(cel);
           
datatable.getDefaultCell().setRowspan(2);
        
// datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_BOTTOM);
         
           
PdfPTable table = new PdfPTable(3);
           
int headerw[] = {131115 };
           
table.setWidths(headerw);
            
           
table.getDefaultCell().setBorderWidth(1);
           
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_BASELINE);
            
//table.getDefaultCell().setRowspan(2);
            
           
table.addCell(getCell("row1",FUENTE_NUM2,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM,2));
           
table.addCell(getCell("row2",FUENTE_NUM2,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM,2));                
           
table.addCell(getCell("row3",FUENTE_NUM2,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM)); 
           
           
table.addCell(getCell("row4",FUENTE_NUM2,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM));
           
table.addCell(getCell("row5",FUENTE_NUM2,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM));                
           
table.addCell(getCell("row6",FUENTE_NUM2,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM));  
        
           
PdfPCell cell = new PdfPCell(table);
           
cell.setColspan(3);

           
datatable.addCell(cell);
           
/***end total table****/ 
        
        
return datatable;
    } 

Etiquetas: create, itext, table
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:53.