galera, minha dúvida é se é possível colocar um PdfPTable dentro de outro PdfPTable no iText …
Estou querendo criar algo como se fosse um SubTable …
[quote=Diego Adriano]galera, minha dúvida é se é possível colocar um PdfPTable dentro de outro PdfPTable no iText …
Estou querendo criar algo como se fosse um SubTable … [/quote]
Boa tarde Diego Adriano, seria isso?
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class Table {
public static void main(String[] args) throws FileNotFoundException, Exception {
Table t = new Table();
t.gerarPDf(new FileOutputStream("testeTable.pdf"));
}
public void gerarPDf(final OutputStream outputStream) throws Exception {
try {
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, outputStream);
document.open();
document.add(tabelaMae());
document.close();
} catch (Exception e) {
throw new Exception(e);
}
}
private PdfPTable tableCabecalho(final String texto){
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100f);
PdfPCell[] cell = new PdfPCell[1];
cell[0] = new PdfPCell(this.formatarTexto(new Chunk(texto), 8, true, BaseColor.RED));
cell[0].setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell[0]);
return table;
}
private PdfPTable tableInsere(){
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(50f);
PdfPCell[] cell = new PdfPCell[6];
cell[0] = new PdfPCell(tableCabecalho("TABELA FILHA"));
cell[1] = new PdfPCell(this.formatarTexto(new Chunk("0000000000"), 8, true, BaseColor.RED));
cell[2] = new PdfPCell(this.formatarTexto(new Chunk("ZZZZZZZZZZ"), 8, true, BaseColor.ORANGE));
cell[3] = new PdfPCell(this.formatarTexto(new Chunk("1111111111"), 8, true, BaseColor.BLUE));
cell[4] = new PdfPCell(this.formatarTexto(new Chunk("XXXXXXXXXX"), 8, true, BaseColor.MAGENTA));
cell[0].setColspan(2);
table.addCell(cell[0]);
table.addCell(cell[1]);
table.addCell(cell[2]);
table.addCell(cell[3]);
table.addCell(cell[4]);
return table;
}
private PdfPTable tabelaMae(){
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100f);
PdfPCell[] cell = new PdfPCell[6];
cell[0] = new PdfPCell(tableCabecalho("TABELA MÃE"));
cell[1] = new PdfPCell(tableInsere());
cell[2] = new PdfPCell(this.formatarTexto(new Chunk("AAAAAAAAA"), 8, true, BaseColor.MAGENTA));
cell[3] = new PdfPCell(this.formatarTexto(new Chunk("000000000"), 8, true, BaseColor.YELLOW));
cell[4] = new PdfPCell(this.formatarTexto(new Chunk("111111111"), 8, true, BaseColor.GREEN));
cell[0].setColspan(2);
table.addCell(cell[0]);
table.addCell(cell[1]);
table.addCell(cell[2]);
table.addCell(cell[3]);
table.addCell(cell[4]);
return table;
}
private Font retornaFonte(final int tamanho, final boolean bold, final BaseColor color) {
Font font = new Font();
font.setColor(color);
font.setFamily(FontFactory.HELVETICA);
font.setSize(tamanho);
if(bold){
font.setStyle(Font.BOLD);
}
return font;
}
private Paragraph formatarTexto(final Chunk chunk, final int tamanho, final boolean bold, final BaseColor color) {
Paragraph paragraph = new Paragraph();
paragraph.setFont(this.retornaFonte(tamanho, bold, color));
paragraph.add(chunk);
return paragraph;
}
}
Vlw Brother !!
Era isso mesmo !!
Show !!