Galera,
tô precisando montar uma tabela com dados vindos do jFrame, quero fazô-lo em iText por ser em PDF,
gostaria de saber como monto tabelas estilo Excel no iText, e como essa tabela venha ficar bem organizadinha,
minha dpuvida é só sobre a tabela no iText (códigos) , o resto dá pra fazer legal!
Abraço!
Dá uma olhada neste código… Acho q pode t ajudar…
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
public class ControllerTable {
public static PdfPTable criarCabecalho()
throws DocumentException {
PdfPTable table = new PdfPTable(new float[] { 10f, 5f, 3f });
PdfPCell celulaNome = new PdfPCell(new Phrase("Nome"));
celulaNome.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell celulaDataNasc = new PdfPCell(new Phrase("Data Nasc"));
celulaDataNasc.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell celulaSexo = new PdfPCell(new Phrase("Sexo"));
celulaSexo.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(celulaNome);
table.addCell(celulaDataNasc);
table.addCell(celulaSexo);
return table;
}
public static void preencherDados(Document document, PdfPTable table,
List<Pessoa> pessoas) throws DocumentException {
if (document.isOpen()) {
for (Pessoa pessoa : pessoas) {
PdfPCell celula1 = new PdfPCell(new Phrase(pessoa.getNome()));
PdfPCell celula2 = new PdfPCell(new Phrase(pessoa.getDataNasc()));
PdfPCell celula3 = new PdfPCell(new Phrase(String.valueOf(pessoa.getSexo())));
table.addCell(celula1);
table.addCell(celula2);
table.addCell(celula3);
}
document.add(table);
}
}
}
public class Pessoa {
private String nome;
private String dataNasc;
private char sexo;
public Pessoa(String nome, String dataNasc, char sexo){
this.nome = nome;
this.dataNasc = dataNasc;
this.sexo = sexo;
}
....
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class GeneratorPDF {
public static void main(String[] args) throws BadElementException {
try {
Document document = new Document();
PdfWriter pdf = PdfWriter.getInstance(document, new FileOutputStream("D:\\Temp\\PDF_DevMedia.pdf"));
// Abre documento
document.open();
PdfPTable table = ControllerTable.criarCabecalho();
List<Pessoa> lPessoa = new ArrayList<Pessoa>();
Pessoa pessoa1 = new Pessoa("Samuel", "06/09/1986", 'M');
Pessoa pessoa2 = new Pessoa("Samuel", "06/09/1986", 'M');
Pessoa pessoa3 = new Pessoa("Samuel", "06/09/1986", 'M');
lPessoa.add(pessoa1);
lPessoa.add(pessoa2);
lPessoa.add(pessoa3);
ControllerTable.preencherDados(document, table, lPessoa);
// Encerra documento
document.close();
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
}
}