0
respostas

Como adiciono campos de um List em um PdfPTable

Olá,

como adiciono campos de um List através de for ou foreach em um PdfPTable?

segue o código…

Obrigado.

botaoListarAlfabetico.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Document documentoPdf = new Document(); 
                try {                    
                    PdfWriter.getInstance(documentoPdf, new FileOutputStream("C:\\Users\\RICARDO\\Downloads\\ALURA_JAVA\\Produto_Nome.pdf"));
                    documentoPdf.open();
                    documentoPdf.setPageSize(PageSize.A4);
                    documentoPdf.add(new Paragraph("Lista de Produtos por Nome"));

                    // Creating a table       
                    float [] pointColumnWidths = {150F, 150F, 150F, 150F, 150F};   
                    PdfPTable table = new PdfPTable(pointColumnWidths);  
                    table.addCell("ID do Produto");
                    table.addCell("Nome do Produto");
                    table.addCell("Descrição do Produto");
                    table.addCell("ID da Categoria");
                    table.addCell("Nome da Categoria");

                    List<Produto> listaDeProdutos = listarProduto();
                    listaDeProdutos.sort(new NomeProdutoComparator()); 

                        // como faço o for ou forEach carregando os campos do  List sorteado listaDeProdutos para dentro de table.addCell?

                    documentoPdf.add(table);   
                } 
                catch(DocumentException de) { 
                    de.printStackTrace();
                } 
                catch(IOException ioex) {
                    ioex.printStackTrace();
                } finally {

                    documentoPdf.close(); 
                }                
                limparTabela();
                preencherTabela();
        }
    });        

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software