Example usage for com.itextpdf.text Document add

List of usage examples for com.itextpdf.text Document add

Introduction

In this page you can find the example usage for com.itextpdf.text Document add.

Prototype


public boolean add(Element element) throws DocumentException 

Source Link

Document

Adds an Element to the Document.

Usage

From source file:com.coderbd.pos.pdf.OrderFileBuilder.java

public String makePdf(ShopOrder shopOrder) {
    Document document = new Document(new Rectangle(205, 800));
    String fileName = directory + "\\" + shopOrder.getCustomerOrder().getOrderBarcode() + ".pdf";
    String receiptText = receipt.getIndentedOrder(shopOrder);
    System.out.println(receiptText);

    document.setMargins(3, 2, 2, 2);//from  w  w  w.j  a va  2s  .  c om

    Font courierFont = FontFactory.getFont("courier");
    courierFont.setSize(10f);

    try {
        PdfWriter.getInstance(document, new FileOutputStream(fileName));

        document.open();
        document.add(new Paragraph(receiptText, courierFont));
        document.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return fileName;
}

From source file:com.coderbd.pos.pdf.OrderReportPDF.java

public boolean genPdf() throws UnsupportedEncodingException {
    List<SupplierOrderProductReport> soprs = sor.getSupplierOrderProductReports();
    IDBuilder idBuilder = new IDBuilder();
    Font innerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
    Font headerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.BOLD);
    try {/*from   ww  w.j  a  v  a 2 s  . c  o  m*/
        String timestamp = idBuilder.getUniqueTimeStampID();
        String filename = sor.getSupplier().getSupplierName() + "_O" + sor.getSupplierOrderId() + "_"
                + timestamp + ".pdf";
        String filePath = directory + "\\" + filename;

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
        document.open();

        PdfPTable pdfPTable = new PdfPTable(5);

        pdfPTable.setWidthPercentage(90);
        float[] widths = { 0.40f, 0.13f, 0.15f, 0.15f, 0.17f };
        pdfPTable.addCell(new Paragraph("Product Name", headerFont));
        pdfPTable.addCell(new Paragraph("Buy Rate", headerFont));
        pdfPTable.addCell(new Paragraph("Primary Qty", headerFont));
        pdfPTable.addCell(new Paragraph("Unsold Qty", headerFont));
        pdfPTable.addCell(new Paragraph("Unsold Amount", headerFont));

        pdfPTable.setWidths(widths);
        for (SupplierOrderProductReport sopr : soprs) {

            SupplierOrderProduct sop = sopr.getSupplierOrderProduct();
            String name = sop.getSupplierProductName();
            Double rate = sop.getSupplierRate();
            Integer pQty = sop.getSupplierProductQuantity();
            Integer unQty = sopr.getUnSoldProductQuantity();
            Double unAmount = sopr.getUnSoldProductAmount();

            Paragraph nameParag = new Paragraph(name, innerFont);
            Paragraph buyRateParag = new Paragraph(rate.toString(), innerFont);
            Paragraph primaryQuantityParag = new Paragraph(pQty.toString(), innerFont);
            Paragraph unsoldQuantityParag = new Paragraph(unQty.toString(), innerFont);
            Paragraph unsoldAmountParag = new Paragraph(unAmount.toString(), innerFont);

            PdfPCell nameCell = new PdfPCell(nameParag);
            PdfPCell buyRateCell = new PdfPCell(buyRateParag);
            PdfPCell primaryQuantityCell = new PdfPCell(primaryQuantityParag);
            PdfPCell unsoldQuantityCell = new PdfPCell(unsoldQuantityParag);
            PdfPCell unsoldAmountCell = new PdfPCell(unsoldAmountParag);

            pdfPTable.addCell(nameCell);
            pdfPTable.addCell(buyRateCell);
            pdfPTable.addCell(primaryQuantityCell);
            pdfPTable.addCell(unsoldQuantityCell);
            pdfPTable.addCell(unsoldAmountCell);
        }
        document.add(getHeader());

        document.add(pdfPTable);
        document.add(getFooter());
        document.add(new Paragraph("            Report Generated: " + new Date().toString(), innerFont));
        document.close();
        return true;
    } catch (DocumentException | FileNotFoundException dex) {
        System.out.println(dex.getMessage());
        return false;
    }
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioEstoqueProdutos(boolean valoresNegativos, File destino) {
    Document doc = new Document();
    try {/*from   w w  w  .j  a v  a2s .  c om*/
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        //String subTitulo = "Estoque Verificado no dia "
        //        + new SimpleDateFormat("dd/MM/yyyy HH:mm").format(Calendar.getInstance().getTime());

        String subTitulo = "Balano Geral de Estoque do perodo 01/12/2017  31/12/2017";
        inserirHead(doc,
                "Mercadinho Popular\n" + "Edvaneide Torres Vilar de Carvalho\n" + "CNPJ: 07.643.907/0001-18",
                subTitulo);

        PdfPTable table = getTableEstoqueProdutos(valoresNegativos);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioEstoqueProdutos(boolean valoresNegativos, boolean pararLista,
        Calendar dataInicio, Calendar dataFim, List<String> codigos_retirados, double total_requisitado,
        File destino) {//w w  w .j a v a 2  s  .  c  o  m
    Document doc = new Document();
    try {
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter instance = PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();

        //String subTitulo = "Estoque Verificado no dia "
        //        + new SimpleDateFormat("dd/MM/yyyy HH:mm").format(Calendar.getInstance().getTime());

        String data = "";
        try {
            data += OperacaoStringUtil.formatDataValor(dataInicio) + "  "
                    + OperacaoStringUtil.formatDataValor(dataFim);
        } catch (NullPointerException e) {
            data += ("01/01/" + (Calendar.getInstance().get(Calendar.YEAR) - 1)) + "  ";
            data += ("31/12/" + (Calendar.getInstance().get(Calendar.YEAR) - 1));
        }

        String subTitulo = "Balano Geral de Estoque do perodo " + data; //" 01/12/2017  31/12/2017";
        inserirHead(doc,
                "Mercadinho Popular\n" + "Edvaneide Torres Vilar de Carvalho\n" + "CNPJ: 07.643.907/0001-18",
                subTitulo);

        PdfPTable table = getTableEstoqueProdutos(valoresNegativos, pararLista, codigos_retirados,
                total_requisitado);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioDebitoClientes(double maiorQue, File destino) {
    Document doc = new Document();
    try {//from ww w  .  ja v  a 2  s  .c  o m
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        String subTitulo;
        if (maiorQue == 0) {
            subTitulo = "Dbito dos Clientes \n Refernte ao dia "
                    + new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime());
        } else {
            subTitulo = "Clientes com Dbitos Maiores que "
                    + OperacaoStringUtil.formatarStringValorMoedaComDescricao(maiorQue)
                    + "\n Refernte ao dia "
                    + new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime());
        }
        inserirHead(doc, "Dbito dos Clientes", subTitulo);

        PdfPTable table = getTableDebitoClientes(maiorQue);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }

}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioBalancoProdutos(Date inicio, Date fim, File destino) {
    Document doc = new Document();
    try {/*  w  w w  .j  a  va2s. co m*/
        String path = a.getRelatorio().getCanonicalPath() + destino.getName();
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        String subTitulo = "Sada de produtos \n Refernte do dia "
                + new SimpleDateFormat("dd/MM/yyyy").format(inicio) + " ao dia "
                + new SimpleDateFormat("dd/MM/yyyy").format(fim);
        inserirHead(doc, "Balano de Produtos", subTitulo);

        PdfPTable table = getTableBalancoProdutos(inicio, fim);

        doc.add(table);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }

}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfDaVenda(Venda v, List<ItemDeVenda> itens, File destino) throws IOException {
    Document doc = new Document();

    try {//from w  ww .  java 2s .  c o  m
        String path = a.getRelatorio().getCanonicalPath() + "/Venda_" + v.getId() + ".pdf";
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        String subTitulo = "Venda Realizada no dia "
                + new SimpleDateFormat("dd/MM/yyyy HH:mm").format(v.getDia().getTime());
        try {
            inserirHead(doc, "Venda do Cliente " + v.getCliente().getNome(), subTitulo);
        } catch (NullPointerException ne) {
            inserirHead(doc, "Venda  vista ID: " + v.getId(), subTitulo);
        }

        PdfPTable table = getTableDeItens(itens);

        doc.add(table);

        Paragraph p2 = new Paragraph("Total: " + new DecimalFormat("0.00").format(v.getTotal())
                + " \n Funcionrio: " + v.getFuncionario().getNome(),
                new Font(Font.FontFamily.COURIER, 16, Font.BOLD));
        doc.add(p2);

        doc.close();
        Arquivo.copyFile(new File(path), destino);
        return destino.getAbsolutePath();
    } catch (DocumentException e) {
        e.printStackTrace();
        return "";
    }

}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfRelatorioBalancoProdutos(Date inicio, Date fim) {
    Document doc = new Document();
    try {/* w  w w . j av  a  2s. c  om*/
        String path = a.getRelatorio().getCanonicalPath() + "/RelatorioBalancoProdutos.pdf";
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        String subTitulo = "Sada de produtos \n Refernte do dia "
                + new SimpleDateFormat("dd/MM/yyyy").format(inicio) + " ao dia "
                + new SimpleDateFormat("dd/MM/yyyy").format(fim);
        inserirHead(doc, "Balano de Produtos", subTitulo);

        PdfPTable table = getTableBalancoProdutos(inicio, fim);

        doc.add(table);

        doc.close();
        return path;
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }

}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public String gerarPdfDaVenda(Venda v, List<ItemDeVenda> itens) {
    Document doc = new Document();

    try {// w w w  .  j  ava2 s . c  o m
        String path = a.getRelatorio().getCanonicalPath() + "/Venda_" + v.getId() + ".pdf";
        PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.open();
        String subTitulo = "Venda Realizada no dia "
                + new SimpleDateFormat("dd/MM/yyyy HH:mm").format(v.getDia().getTime());
        if (v.getCliente() != null) {
            inserirHead(doc, "Venda do Cliente " + v.getCliente().getNome(), subTitulo);
        } else {//venda a vista
            inserirHead(doc, "Venda  Vista", subTitulo);
        }

        PdfPTable table = getTableDeItens(itens);

        doc.add(table);

        Paragraph p2 = new Paragraph("Total: " + new DecimalFormat("0.00").format(v.getTotal())
                + " \n Funcionrio: " + v.getFuncionario().getNome(),
                new Font(Font.FontFamily.COURIER, 16, Font.BOLD));
        doc.add(p2);

        doc.close();
        return path;
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
        return "";
    }

}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

public void inserirHead(Document doc, String titulo, String subTitulo)
        throws MalformedURLException, DocumentException {
    Image img = null;/*from w w  w. j  a  v  a  2 s.  c  o  m*/
    try {
        img = Image.getInstance(IMG.class.getResource("logo_relatorio.png"));

        img.setAlignment(Element.ALIGN_LEFT);

        doc.add(img);
    } catch (IOException e) {
        e.printStackTrace();
    }

    Font f = new Font(Font.FontFamily.COURIER, 20, Font.BOLD);
    Paragraph p = new Paragraph(titulo, f);
    Paragraph p2 = new Paragraph(subTitulo, new Font(Font.FontFamily.COURIER, 16, Font.BOLD));
    p.setAlignment(Element.ALIGN_CENTER);

    doc.add(p);
    doc.add(p2);
}