Ir ao conteúdo
  • Cadastre-se

Java como alinhar celulas em itext 5.5.13.3?


Posts recomendados

Bom dia pessoal, estou fazendo um relatorio usando o itextpdf-5.5.13.3 na tabela estou definindo em cada coluna onde os itens irão ficar, mas a largura de algumas colunas ficam pequenas demais e quebram para a proxima linha, como posso ajustar essas colunas para que fique tudo em uma linha?

 

RelatorioGeral.java:

package Relatorios;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import controller.Conexao;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;


public class RelatorioGeral 
{     
            
    public static void main(String[] args) throws FileNotFoundException, DocumentException, BadElementException, IOException, SQLException 
    {    
        
        Connection conexao = null;
                
        PreparedStatement pstm = null;
                
        ResultSet rs = null;     
        
        int contador = 0;
        
        try
        {
            
            String nomearquivo = "S:\\NTI\\Projeto estagio\\SemurrCEI\\src\\Relatorios\\Relatorio.pdf";
            
            Document documento = new Document();
            
            PdfWriter.getInstance(documento, new FileOutputStream(nomearquivo));
            
            documento.open();
                
            documento.setPageSize(PageSize.A4.rotate());
                    
            documento.open();
                    
            Image imagem = Image.getInstance("S:\\NTI\\Projeto estagio\\SemurrCEI\\src\\view\\imagens\\logopref.png");
                    
            imagem.scaleAbsolute(90, 70);
            
            imagem.setAlignment(Element.ALIGN_CENTER);
                    
            documento.add(imagem);
                    
            Paragraph ptitulo = new Paragraph("SEMURCEI", FontFactory.getFont(FontFactory.TIMES_BOLD, 12f));
            
            
            
            ptitulo.setAlignment(Element.ALIGN_CENTER);
            
            documento.add(ptitulo);
            
            Paragraph psubtitulo = new Paragraph("| - SISTEMA DE CONTROLE DE ESTOQUE - |", FontFactory.getFont(FontFactory.TIMES_BOLD, 12f));
            psubtitulo.setAlignment(Element.ALIGN_CENTER);

            documento.add(psubtitulo);     
                    
            Paragraph vazio = new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8f));
            
            ptitulo.setAlignment(Element.ALIGN_CENTER);
            
            documento.add(vazio);            
            
            PdfPTable tablesup= new PdfPTable(9);                
                    
            PdfPCell CELULATitulo = new PdfPCell(new Paragraph("RELATORIO EQUIPAMENTOS"));
                    
            CELULATitulo.setColspan(10);
                    
            CELULATitulo.setHorizontalAlignment(Element.ALIGN_CENTER);
                    
            CELULATitulo.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    
            tablesup.addCell(CELULATitulo);

            tablesup.addCell("UNIDADE");

            tablesup.addCell("TIPOEQUIP");

            tablesup.addCell("TOMBO");

            tablesup.addCell("SERIAL");

            tablesup.addCell("FORCENEDOR");

            tablesup.addCell("FABRICANTE");

            tablesup.addCell("MODELO");

            tablesup.addCell("STATUS");

            tablesup.addCell("EQUIPAMENTO");     

            float[] columnWidths = new float[]{29f, 35f, 25f, 25f,44f,39f,28f,28f,45f};
            tablesup.setWidths(columnWidths);
            
            try
            {
                
                conexao = new Conexao().Conectar();                
                
                String sql = "select * from equipamento as e inner join unidade u on e.unidade = u.id inner join tipoequipamento as tp on e.tipoequip = tp.id inner join\n" +
                "fornecedor as f on e.fornecedor = f.id inner join fabricante as fab on e.fabricante = fab.id";     
        
                pstm = conexao.prepareStatement(sql);
        
                rs = pstm.executeQuery();
                
                while(rs.next())
                {
                    
                    tablesup.addCell(rs.getString("unidadenome"));
                    tablesup.addCell(rs.getString("tipoequipamentonome"));
                    tablesup.addCell(rs.getString("tombo"));
                    tablesup.addCell(rs.getString("serie"));
                    tablesup.addCell(rs.getString("fornecedornome"));
                    tablesup.addCell(rs.getString("fabricantenome"));
                    tablesup.addCell(rs.getString("modelo"));
                    tablesup.addCell(rs.getString("status"));
                    tablesup.addCell(rs.getString("equipamento"));      
                    
                    contador++;
            
                }             
                
                JOptionPane.showMessageDialog(null, "Relatorio emitido com sucesso!", "RELATORIO GERAL", JOptionPane.INFORMATION_MESSAGE);
                
            }
            catch(SQLException ex)
            {
                
                JOptionPane.showMessageDialog(null, "Error ao consultar para gerar pdf: " + ex, "ERROR!", JOptionPane.INFORMATION_MESSAGE);
                
            }
            finally
            {
                
            //feche a conexao e o pstm 
            conexao.close();
            pstm.close();                
                
            }   
            
            documento.add(tablesup);
                    
            Paragraph pcontador = new Paragraph("QUANTIDADE DE EQUIPAMENTOS: " + contador, FontFactory.getFont(FontFactory.TIMES_ROMAN, 8f));
            pcontador.setAlignment(Element.ALIGN_LEFT);            
            
            documento.add(pcontador);            
            
            documento.close();
                
            }
            catch(DocumentException ex)
            {
                    
                JOptionPane.showMessageDialog(null, "Error ao gerar o documento!" +ex, "ERROR!", JOptionPane.INFORMATION_MESSAGE);
                    
            }
            catch(FileNotFoundException ex)
            {
                    
                JOptionPane.showMessageDialog(null, "Error ao gerar o documento!" +ex, "ERROR!", JOptionPane.INFORMATION_MESSAGE);
                    
            } 
            catch(IOException ex)
            {
                    
                JOptionPane.showMessageDialog(null, "Error ao gerar o documento!" +ex, "ERROR!", JOptionPane.INFORMATION_MESSAGE);
                    
            }         
            
        } 
        
    }

 

495581a0087abf713e7f5dc0bf3bca73545d133f.png

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!