Gerar PDF

10 respostas
C

E aí pessoal…tudo certo?

O negócio é o seguinte:

Tenho um sistema aqui que gera um pdf com uma tabela. Mas como esta tabela é muito grande, está gerando a tabela em três páginas. Uma parte da tabela em cada página. Pensei em gerar o pdf na horizontal, tipo paisagem…Como eu poderia estar fazendo isso?

private Document preencheMatriz(Document document, Integer size) throws DocumentException{
	  
	  Table t = new Table(9,size);
  	  t.setWidth(100f);
  	  t.setAlignment(Element.ALIGN_JUSTIFIED);
  	 /* float[] tams = {1.2f,1f,1.4f,0.8f,1f,1f,0.6f,1f,1f};
	  t.setWidths(tams);*/
	  
  	  t = preencheCabecalho(t, 1);
      	  
  	  //PREENCHE CONTEUDO	
  	  t = preencheConteudo(t, 1, size);
      	  
  	  document.add(t);
  	  
  	  document.newPage();
  	  
  	  Table t2 = new Table(8,size);
  	  t2.setWidth(100f);
  	  t2.setAlignment(Element.ALIGN_JUSTIFIED);
  	  /*float[] tams2 = {1.4f,0.7f,0.9f,1f,0.9f,1f,0.8f,1.3f,1f,1f};
	  t2.setWidths(tams2);*/
  	  
  	  t2 = preencheCabecalho(t2, 2);
      	  
  	  //PREENCHE CONTEUDO
  	  t2 = preencheConteudo(t2, 2, size);
  	  
  	  document.add(t2);
  	  
  	  document.newPage();
	  
	  Table t3 = new Table(8,size);
	  t3.setWidth(100f);
	  t3.setAlignment(Element.ALIGN_JUSTIFIED);
	  /*float[] tams2 = {1.4f,0.7f,0.9f,1f,0.9f,1f,0.8f,1.3f,1f,1f};
	  t3.setWidths(tams2);*/
  	  
  	  t3 = preencheCabecalho(t3, 3);
      	  
	  //PREENCHE CONTEUDO
	  t3 = preencheConteudo(t3, 3, size);
  	  
  	  document.add(t3);
	  
	return document;
    }
    
    private Table preencheCabecalho(Table t, int i) throws BadElementException{
	
	if (i == 1){
	    
	  //CABECALHO DA TABELA 1
	  	  
	  	  Cell c1 = new Cell(new Paragraph("Regional", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          c1.setHeader(true);
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Região", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Projeto", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Lote", new RtfFont("Arial", 12, RtfFont.BOLD)));
		      	  t.addCell(c1);
		      	  c1 = new Cell(new Paragraph("Talhão", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Dens QQ", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Área QQ", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Dens C1", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	  t.addCell(c1);
	        	  c1 = new Cell(new Paragraph("Área C1", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	  t.addCell(c1);
	      	  t.endHeaders();
	      	  
	        //FIM CABECALHO TABELA
	    
	}
	
	if (i == 2){
	    
	  //CABECALHO DA TABELA 2
	  	  
	      	  Cell c2 = new Cell(new Paragraph("Dens C2", new RtfFont("Arial", 12, RtfFont.BOLD)));
	      	  c2.setHeader(true);
	              	  t.addCell(c2);
	              	  c2 = new Cell(new Paragraph("Área C2", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	  t.addCell(c2);
	        	  c2 = new Cell(new Paragraph("Dens C3", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	  t.addCell(c2);
	      	  	  c2 = new Cell(new Paragraph("Área C3", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c2);
	          	  c2 = new Cell(new Paragraph("Dens C4", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	  t.addCell(c2);
	          	  c2 = new Cell(new Paragraph("Área C4", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c2);
	          	  c2 = new Cell(new Paragraph("Dens C5", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	  t.addCell(c2);
	    	  	  c2 = new Cell(new Paragraph("Área C5", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	  t.addCell(c2);
	      	  t.endHeaders();
	      	  
	        //FIM CABECALHO TABELA
	    
	}
	
	if (i == 3){
	    
	  //CABECALHO DA TABELA 3
	  	  
	      	  Cell c3 = new Cell(new Paragraph("Dens C6", new RtfFont("Arial", 12, RtfFont.BOLD)));
	      	  c3.setHeader(true);
	      	  	t.addCell(c3);
	              	c3 = new Cell(new Paragraph("Área C6", new RtfFont("Arial", 12, RtfFont.BOLD)));
	              	t.addCell(c3);
	        	c3 = new Cell(new Paragraph("Dens C7", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	t.addCell(c3);
	        	c3 = new Cell(new Paragraph("Área C7", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	t.addCell(c3);
	          	c3 = new Cell(new Paragraph("DT", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	t.addCell(c3);
	        	c3 = new Cell(new Paragraph("AT", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	t.addCell(c3);
	        	c3 = new Cell(new Paragraph("LM", new RtfFont("Arial", 12, RtfFont.BOLD)));
	        	t.addCell(c3);
	          	c3 = new Cell(new Paragraph("CV", new RtfFont("Arial", 12, RtfFont.BOLD)));
	          	t.addCell(c3);
	          t.endHeaders();
	      	  
	        //FIM CABECALHO TABELA
	    
	}
	
	return t;
    }

Perceba que o código está separando 3 páginas…queria montar a tabela toda em uma página só.
Se alguém tiver uma idéia, por favor me ajude…

OBRIGADO!

10 Respostas

fernandopaiva

isso é um relatório ???

se for, pq naum usa o JasperReport ???

C

é um relatório sim…ele cria um pdf com uma tabela de dados

Eu nunca usei esse JasperReport, não faz muito tempo que programo em java. Como ele funciona?

ViniGodoy

Por favor, evite expressões como [URGENTE], [DUVIDA], [PLEEEASE] e similares no título dos tópicos.

fernandopaiva

opa…entaum uma dica “pare de sofrer com relatórios e use o jasperreports pra isso”, deixe o trabalho sujo pra ele…da uma procurada no youtube que tem video aulas mostrando como td funciona, é bem simples e gera pdf, rtf etc…etc…etc. !

t+

C

blz…vou dar uma pesquisada…valeu!

fernandopaiva

se for usar JTable veja esse exemplo que eu montei: http://guj.com.br/posts/list/225496.java

ai ele passa uma Jtable inteira pro Jasper que gera um relatório para impressão…Com Jasper vc pode usar DataSource de BD ou não.

t+

Queops82

tem um site que me recomendaram muito bom

http://davidbuzatto.com.br/2010/10/09/jasperreports-trabalhando-com-relatorios-em-java-parte-1/

flw

evertonsilvagomesjav

fernandopaiva:
se for usar JTable veja esse exemplo que eu montei: http://guj.com.br/posts/list/225496.java

ai ele passa uma Jtable inteira pro Jasper que gera um relatório para impressão…Com Jasper vc pode usar DataSource de BD ou não.

t+

JTable web?

C

Pessoal, eu não usei jasperreport não, mas queria mostrar o que eu fiz para gerar o pdf
segue o código:

package pck.pdf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.faces.event.ActionEvent;

import br.ufla.dcc.mip.controller.bean.session.LoginBean;
import br.ufla.dcc.mip.util.FacesContextUtil;
import br.ufla.dcc.mipfor.controller.bean.request.calculos.MonitInfestacaoBean;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfFont;

public class PdfBeanMonitInfestacao{
    
    public final Integer numColums = 25;
	
    private File f;
    private LoginBean loginBean;
    private MonitInfestacaoBean monitInfestacaoBean;
    private Document document;
   
    public void geraPdf(ActionEvent event) throws FileNotFoundException, DocumentException {
	  
	  this.loginBean = (LoginBean) FacesContextUtil
		.getSessionBean(LoginBean.BEAN_NAME);
	  this.monitInfestacaoBean = (MonitInfestacaoBean) FacesContextUtil
		.getRequestBean(MonitInfestacaoBean.BEAN_NAME);
         
	  //Cria pdf
	  
	  f= new File(loginBean.getNomeUsuario()+".pdf");
  	  
  	  this.document = new Document(PageSize.A4, 50, 50, 50, 50);
  	  PdfWriter writer = PdfWriter.getInstance(document,
  			  new FileOutputStream(f.getAbsolutePath()));
  	  document.setPageSize(PageSize.LETTER.rotate());  // acho que esse comando deixa o pdf no modo paisagem
  	 
          //Cabeçalho e Rodapé
  	
  	  Phrase ph = new Phrase();  
  	  Phrase ph2 = new Phrase();
        ph.add("                  "); 
        ph2.add(""); 
        HeaderFooter cabecalho = new HeaderFooter(ph, false);
        HeaderFooter cabecalho2 = new HeaderFooter(ph2, true);
        document.setHeader(cabecalho);
        document.setFooter(cabecalho2);
  	  
  	  document.open();
	  
  	try{
  	    
  	    Integer size = monitInfestacaoBean.getListMonitInfestacao().size();
  	    
  	    if (size != null)
  		document = preencheMatriz(document, size);
  	    
  	  } catch (NullPointerException e){
  	      e.printStackTrace();
  	  }
  	  
  	document.close();  
  	  
      executa();

    }
    
    private Document preencheMatriz(Document document, Integer size) throws DocumentException{
	  
	  Table t = new Table(25,size);
  	  t.setWidth(110f); // mudar de 100 para um numero inferior, a tabela fica mais compacta lateralmente falando
  	  t.setAlignment(Element.ALIGN_JUSTIFIED);
  	  float[] tams = {1.7f,1.7f,1.7f,1.7f,1.7f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f,1f};
	  t.setWidths(tams);
	  
  	 // t = preencheCabecalho(t, 1);
      	  
  	  t = preencheCabecalho(t);
  	  
  	  //PREENCHE CONTEUDO	
  	 // t = preencheConteudo(t, 1, size);
  	  t = preencheConteudo(t, size);
  	  document.add(t);
  	  
  	  document.newPage();
  	  
  	 
	return document;
    }
    
    private Table preencheCabecalho(Table t) throws BadElementException{
	
	
	    
	  //CABECALHO DA TABELA 1 
	  	  
	  	  Cell c1 = new Cell(new Paragraph("Regional", new RtfFont("Arial", 10, RtfFont.BOLD)));
	          c1.setHeader(true);
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Região", new RtfFont("Arial", 10, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Projeto", new RtfFont("Arial", 10, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Lote", new RtfFont("Arial", 10, RtfFont.BOLD)));
		      	  t.addCell(c1);
		      	  c1 = new Cell(new Paragraph("Talhão", new RtfFont("Arial", 10, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Dens QQ", new RtfFont("Arial", 10, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Área QQ", new RtfFont("Arial", 10, RtfFont.BOLD)));
	          	  t.addCell(c1);
	          	  c1 = new Cell(new Paragraph("Dens C1", new RtfFont("Arial", 10, RtfFont.BOLD)));
	        	  t.addCell(c1);
	        	  c1 = new Cell(new Paragraph("Área C1", new RtfFont("Arial", 10, RtfFont.BOLD)));
	        	  t.addCell(c1);
	        	  c1 = new Cell(new Paragraph("Dens C2", new RtfFont("Arial", 10, RtfFont.BOLD)));
	        	  t.addCell(c1);
	        	  c1 = new Cell(new Paragraph("Área C2", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Dens C3", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		      	  c1 = new Cell(new Paragraph("Área C3", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Dens C4", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Área C4", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Dens C5", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		    	  c1 = new Cell(new Paragraph("Área C5", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Dens C6", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Área C6", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Dens C7", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("Área C7", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("DT", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("AT", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("LM", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
		          c1 = new Cell(new Paragraph("CV", new RtfFont("Arial", 10, RtfFont.BOLD)));
		          t.addCell(c1);
	      	  t.endHeaders();
	      	  
	        //FIM CABECALHO TABELA
	    
	
	
	
	
	return t;
    }
    
    private Table preencheConteudo(Table t, Integer size) throws DocumentException{
	  
	  int k = 0;
	
	
	      
	      for (int i = 0; i < size; i++){
		  
		     if (k == 10){
			  this.document.add(t);
			  t = new Table(25,size);
			  t.setWidth(110f);
			  t.setAlignment(Element.ALIGN_JUSTIFIED);
			  t = preencheCabecalho(t);
			  this.document.newPage();
			  k = 0;
		      }
		     	// codigo para colocar os dados na primeira pagina da tabela em pdf
          	      t.addCell(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getId1());
          	      t.addCell(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getId2());
          	      t.addCell(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getId3());
          	      t.addCell(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getId4());
          	      t.addCell(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getId5());
          	      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDQ()));
          	      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAQ()));
          	      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensC1()));
          	      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaC1()));
          	      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensC2()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaC2()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensC3()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaC3()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensC4()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaC4()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensC5()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaC5()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensC6()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaC6()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensC7()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaC7()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getDensTot()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getAreaTot()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getLM()));
		      t.addCell(String.valueOf(this.monitInfestacaoBean.getListMonitInfestacao().get(i).getCV()));
		      
          	      k++;
	   
	      
	  }
	  
	
	  
	  return t;
    }
    
    
    public void executa (){
  	  
  	  String comando = f.getAbsolutePath();
 	  
 	   try { 
 		String sOs = System.getProperty("os.name").toUpperCase();
 		if(sOs.equals("WINDOWS NT") || sOs.equals("WINDOWS 2000") || sOs.equals("WINDOWS XP") || sOs.equals("WINDOWS 7")){
 			Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+comando);
 				  
 		} else{
 			Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+comando);
 				  
 		}
 			  	 	    
 	   } catch (IOException e1) { 
 		System.out.println(e1); 
 	   } 
    }
    
 
}

O único problema que está faltando resolver é o seguinte: quando gera o pdf, têm alguns campos que a palavra a ser inserida é muito grande logo ela fica escrita em duas linhas.
por exemplo, na primeira coluna (Regional) ao invés de aparecer na célula o nome Botucatu, está aparecendo o nome Botu(na primeira linha) e catu(na segunda linha).

entenderam? ta dando quebra de linha por falta de espaço. O que posso fazer? queria apenas modificar o código acima, sem ter que adcionar plug-in ou outra ferramenta.
Será que tem como?

Espero uma resposta. Valeuuuuu pessoal!

LPJava

caio nao queira rei-ventar a roda, o mercado, o “mundo” usa o jasperReport para gerar diferentes relatorios e tem o IReport como ferramenta, nao vejo nessecidade de vc querer fazer algo que a propria comunidade ja implementou e de uma forma simples e eficiente.

Criado 3 de dezembro de 2010
Ultima resposta 11 de dez. de 2010
Respostas 10
Participantes 6