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:billerfx.FXMLBillController.java

File generatePDF() {
    File fi = null;// ww w.ja v a  2s  .  c  o m
    try {
        Stage gg = ((Stage) imv.getParent().getScene().getWindow());
        fi = File.createTempFile("billerfx_" + gg.getTitle(), ".pdf");
        fi.deleteOnExit();

        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(fi));
        Rectangle r = new RectangleReadOnly(207, 575);
        document.setPageSize(r);
        document.setMargins(15, 15, 0, 0);
        document.open();
        Font fontbold = FontFactory.getFont("Times-Roman", 10, Font.NORMAL);
        Font fontbold2 = FontFactory.getFont("Times-Roman", 9, Font.NORMAL);

        String[] ll = Database.getCurrentSettings();
        String ss = "-----------------------------------------------------\n";
        ss += ll[0] + "\nAddress: " + ll[1] + "\nPhone: " + ll[2] + "\n";
        String kk = Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM ? "AM"
                : (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.PM ? "PM" : "");
        ss += Calendar.getInstance().get(Calendar.DATE) + "/" + (1 + Calendar.getInstance().get(Calendar.MONTH))
                + "/" + Calendar.getInstance().get(Calendar.YEAR) + " at "
                + Calendar.getInstance().get(Calendar.HOUR) + ":" + Calendar.getInstance().get(Calendar.MINUTE)
                + " " + kk + "\n";
        ss += "Table No. " + c.getText() + "     Bill No. " + a.getText() + "\n";
        ss += "-----------------------------------------------------\n";
        Paragraph para = new Paragraph(ss, fontbold);
        para.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(para);

        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        float[] columnWidths = { 3f, 1f };
        table.setWidths(columnWidths);
        PdfPCell defaultCell = table.getDefaultCell();
        defaultCell.setBorder(PdfPCell.NO_BORDER);

        ObservableList ob = map.get(Integer.parseInt(gg.getTitle()));
        for (int i = 0; i < ob.size(); i++) {
            Item2 ii = (Item2) ob.get(i);
            String s1 = ii.getQuantity() + " x " + ii.getName();
            String s2 = "Rs. " + ii.getTotal();
            Paragraph para1 = new Paragraph(s1, fontbold2);
            para1.setAlignment(Paragraph.ALIGN_LEFT);
            Paragraph para2 = new Paragraph(s2, fontbold2);
            para2.setAlignment(Paragraph.ALIGN_RIGHT);
            PdfPCell cell1 = new PdfPCell(para1);
            cell1.setBorder(PdfPCell.NO_BORDER);
            cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            PdfPCell cell2 = new PdfPCell(para2);
            cell2.setBorder(PdfPCell.NO_BORDER);
            cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell1);
            table.addCell(cell2);
        }
        document.add(table);

        para = new Paragraph("-----------------------------------------------------\n", fontbold);
        para.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(para);

        table = new PdfPTable(2);
        table.setWidthPercentage(100);
        table.setWidths(columnWidths);
        defaultCell = table.getDefaultCell();
        defaultCell.setBorder(PdfPCell.NO_BORDER);

        String s1 = "Total:";
        String s2 = "Rs. " + h.getText();
        Paragraph para1 = new Paragraph(s1, fontbold2);
        para1.setAlignment(Paragraph.ALIGN_LEFT);
        Paragraph para2 = new Paragraph(s2, fontbold2);
        para2.setAlignment(Paragraph.ALIGN_RIGHT);
        PdfPCell cell1 = new PdfPCell(para1);
        cell1.setBorder(PdfPCell.NO_BORDER);
        cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        PdfPCell cell2 = new PdfPCell(para2);
        cell2.setBorder(PdfPCell.NO_BORDER);
        cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell1);
        table.addCell(cell2);

        if (Double.parseDouble(h.getText()) - Double.parseDouble(i.getText()) > 0.0) {
            s1 = "Discount:";
            s2 = "Rs. " + BillerFX.df.format(Double.parseDouble(h.getText()) - Double.parseDouble(i.getText()));
            para1 = new Paragraph(s1, fontbold2);
            para1.setAlignment(Paragraph.ALIGN_LEFT);
            para2 = new Paragraph(s2, fontbold2);
            para2.setAlignment(Paragraph.ALIGN_RIGHT);
            cell1 = new PdfPCell(para1);
            cell1.setBorder(PdfPCell.NO_BORDER);
            cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell2 = new PdfPCell(para2);
            cell2.setBorder(PdfPCell.NO_BORDER);
            cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell1);
            table.addCell(cell2);
        }

        if (ll[7].equals("1")) {
            s1 = ll[3];
            s2 = "Rs. " + k.getText();
            para1 = new Paragraph(s1, fontbold2);
            para1.setAlignment(Paragraph.ALIGN_LEFT);
            para2 = new Paragraph(s2, fontbold2);
            para2.setAlignment(Paragraph.ALIGN_RIGHT);
            cell1 = new PdfPCell(para1);
            cell1.setBorder(PdfPCell.NO_BORDER);
            cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell2 = new PdfPCell(para2);
            cell2.setBorder(PdfPCell.NO_BORDER);
            cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell1);
            table.addCell(cell2);
        }

        if (ll[8].equals("1")) {
            s1 = ll[4];
            s2 = "Rs. " + l.getText();
            para1 = new Paragraph(s1, fontbold2);
            para1.setAlignment(Paragraph.ALIGN_LEFT);
            para2 = new Paragraph(s2, fontbold2);
            para2.setAlignment(Paragraph.ALIGN_RIGHT);
            cell1 = new PdfPCell(para1);
            cell1.setBorder(PdfPCell.NO_BORDER);
            cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell2 = new PdfPCell(para2);
            cell2.setBorder(PdfPCell.NO_BORDER);
            cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(cell1);
            table.addCell(cell2);
        }

        s1 = "Grand Total:";
        s2 = "Rs. " + m.getText();
        para1 = new Paragraph(s1, fontbold2);
        para1.setAlignment(Paragraph.ALIGN_LEFT);
        para2 = new Paragraph(s2, fontbold2);
        para2.setAlignment(Paragraph.ALIGN_RIGHT);
        cell1 = new PdfPCell(para1);
        cell1.setBorder(PdfPCell.NO_BORDER);
        cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell2 = new PdfPCell(para2);
        cell2.setBorder(PdfPCell.NO_BORDER);
        cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell1);
        table.addCell(cell2);

        document.add(table);

        para = new Paragraph("-----------------------------------------------------\n", fontbold);
        para.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(para);

        fontbold2 = FontFactory.getFont("Times-Roman", 8, Font.NORMAL);
        para = new Paragraph(
                "Thank You.\nThis invoice was created using BillerFX.\n (BillerFX Contact: ayushmaanbhav1008@gmail.com)\n",
                fontbold2);
        para.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(para);

        para = new Paragraph("-----------------------------------------------------\n", fontbold);
        para.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(para);

        document.close();
    } catch (Exception mm) {
    }
    return fi;
}

From source file:bl.pdf.PDFFile.java

@SuppressWarnings("unused")
private void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);/*from   ww  w .ja v  a 2s. c  o  m*/
    // Lets write a big header
    preface.add(new Paragraph("Title of the document", catFont));

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
    preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ", smallBold));

    addEmptyLine(preface, 8);

    preface.add(new Paragraph(
            "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
            redFont));

    document.add(preface);
    // Start a new page
    document.newPage();
}

From source file:bl.pdf.PDFFile.java

@SuppressWarnings("unused")
private void addContent(Document document) throws DocumentException {
    Anchor anchor = new Anchor("First Chapter", catFont);
    anchor.setName("First Chapter");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategory 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Hello"));

    subPara = new Paragraph("Subcategory 2", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Paragraph 1"));
    subCatPart.add(new Paragraph("Paragraph 2"));
    subCatPart.add(new Paragraph("Paragraph 3"));

    // Add a list
    createList(subCatPart);/*w w w .j  av a  2 s.  c o m*/
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    // Add a table
    createTable(subCatPart);

    // Now add all this to the document
    document.add(catPart);

    // Next section
    anchor = new Anchor("Second Chapter", catFont);
    anchor.setName("Second Chapter");

    // Second parameter is the number of the chapter
    catPart = new Chapter(new Paragraph(anchor), 1);

    subPara = new Paragraph("Subcategory", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("This is a very important message"));

    // Now add all this to the document
    document.add(catPart);

}

From source file:bladwin.web.reg.regPDF.java

public void genPed(String loc, regMgr regMgr, dbMgrInterface db) {
    Document document = null;
    //String loc = "C:/_bisc/_javaApps/jlData/jvp/CellWidths.pdf";

    //regMgr.getCustomerRegBean().setPdf(loc);
    try {//from  w w  w  .  j a  v  a  2 s  .  co  m

        document = new Document(PageSize.A4, 30, 30, 30, 30);
        PdfWriter.getInstance(document, new FileOutputStream(loc));
        document.open();
        customerRegBean r = regMgr.getCustomerRegBean();
        customerBean c = regMgr.getCustomerBean();
        customerBean a = new custObj().getcustomerBean(r.getRegCustId(), db);
        mgnLookupBean bean = new mgnLookupObj().getLookupBean(-947, db);
        Paragraph pg;
        pg = new Paragraph(r.getLookupDesc());
        pg.setAlignment(Paragraph.ALIGN_CENTER);
        pg.setFont(FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD));
        document.add(getHeader(r.getLookupDesc()));
        document.add(getHeader("BALDWIN BLAZERS TRACK CLUB"));
        document.add(getHeader("REGISTRATION"));
        pg = new Paragraph("\n\r");
        document.add(pg);
        // -------------------------------------------------------
        document.add(genAthleteInfo(r, a));
        pg = new Paragraph("\n\r");
        document.add(pg);
        document.add(genParentInfo(c));
        pg = new Paragraph("\n\r");
        document.add(pg);
        document.add(genSport(r));
        pg = new Paragraph("\n\r");
        document.add(pg);
        pg = new Paragraph("PARENTS SIGNATURE");
        document.add(pg);
        pg = new Paragraph("\n\r");
        document.add(pg);
        pg = new Paragraph("\n\r");
        document.add(pg);
        pg = new Paragraph("_____________________________________________________________________");
        document.add(pg);
        pg = new Paragraph("\n\r");
        document.add(pg);
        document.add(getHeader(r.getFeeDesc() + "\n\r" + bean.getSubjectBody()));
        // ----------------------------------------------------------------------------

    } catch (DocumentException ex) {
        logger.error("DocumentException", ex);
    } catch (FileNotFoundException ex) {
        logger.error("FileNotFoundException", ex);
    } finally {
        try {
            if (document != null)
                document.close();

            //pdfFileOpen(loc);

        } catch (Exception e) {
            logger.error("FileNotFoundException", e);
        }
    }
}

From source file:bodyfitness.relatorios.Relatorio.java

public void GeraRelatorioAdimplente(String caminho) {
    Document documento = new Document();
    try {//  w  ww  . j a v  a 2 s. c o  m
        PdfWriter.getInstance(documento, new FileOutputStream(caminho + "/Adimplentes.pdf"));
        documento.open();
        ClienteDAO dao = new ClienteDAO();
        List<Cliente> clientes = dao.consultarAdimplentes();
        PdfPTable tabela = new PdfPTable(2);
        tabela.addCell("ID");
        tabela.addCell("Nome");
        Paragraph p = new Paragraph("     Relatrio - Clientes Adimplentes\n\n\n");
        documento.add(p);
        for (Cliente c : clientes) {
            tabela.addCell(c.getId().toString());
            tabela.addCell(c.getNome());
        }
        documento.add(tabela);
        documento.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro ao gerar documento:\n" + e.getMessage(), "Erro",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:bodyfitness.relatorios.Relatorio.java

public void GeraRelatorioInadimplente(String caminho) {
    Document documento = new Document();
    try {//from   ww  w  .java  2 s . c om
        PdfWriter.getInstance(documento, new FileOutputStream(caminho + "/Adimplentes.pdf"));
        documento.open();
        ClienteDAO dao = new ClienteDAO();
        List<Cliente> clientes = dao.consultarInadimplentes();
        PdfPTable tabela = new PdfPTable(2);
        tabela.addCell("ID");
        tabela.addCell("Nome");
        Paragraph p = new Paragraph("     Relatrio - Clientes Inadimplentes\n\n\n");
        documento.add(p);
        for (Cliente c : clientes) {
            tabela.addCell(c.getId().toString());
            tabela.addCell(c.getNome());
        }
        documento.add(tabela);
        documento.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro ao gerar documento:\n" + e.getMessage(), "Erro",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:bodyfitness.relatorios.Relatorio.java

public void GeraRelatorioMaquinaSManutencao(String caminho) {
    Document documento = new Document();
    try {// www . ja v  a 2s.  c  o  m
        PdfWriter.getInstance(documento, new FileOutputStream(caminho + "/MaquinaSManutencao.pdf"));
        documento.open();
        EquipamentoDAO dao = new EquipamentoDAO();
        List<Equipamento> equips = dao.consultarSemManutancao();
        PdfPTable tabela = new PdfPTable(3);
        tabela.addCell("ID");
        tabela.addCell("Nome");
        tabela.addCell("D. Ultima Mantencao");
        SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        Paragraph p = new Paragraph("     Relatrio - Clientes Mquinas Sem Manuteno\n\n\n");
        documento.add(p);
        for (Equipamento e : equips) {
            tabela.addCell(e.getId().toString());
            tabela.addCell(e.getNome());
            tabela.addCell(formato.format(e.getDataDaUltimaManutencao()));
        }
        documento.add(tabela);
        documento.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro ao gerar documento:\n" + e.getMessage(), "Erro",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:bodyfitness.relatorios.Relatorio.java

public void GeraRelatorioTreino(String caminho, Long id) {
    ClienteDAO cdao = new ClienteDAO();
    Cliente cliente = cdao.consultarPorId(id);
    Document documento = new Document();
    Paragraph p = new Paragraph("Nome: " + cliente.getNome() + "\n" + "Id: " + cliente.getId().toString() + "\n"
            + "Idade: " + cliente.getIdade().toString() + "\n\n");
    PdfPTable tabela = new PdfPTable(3);
    tabela.addCell("Maquina");
    tabela.addCell("Sries");
    tabela.addCell("Repeties");
    try {//w  ww  .  ja v  a 2 s  .  c o m
        PdfWriter.getInstance(documento, new FileOutputStream(caminho + "/MaquinaSManutencao.pdf"));
        Treino t = cliente.getTreino();
        for (int i = 0; i < t.getSeries().size(); i++) {
            tabela.addCell(t.getSeries().get(i).getEquipamento().getNome());
            tabela.addCell(String.valueOf(t.getSeries().get(i).getQuantidadeDeSeries()));
            tabela.addCell(String.valueOf(t.getSeries().get(i).getRepeticoes()));
        }
        documento.add(p);
        documento.add(tabela);

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro em relatrio de treino:\n" + e.getMessage());
    } finally {
        documento.close();
    }

}

From source file:bodyfitness.relatorios.Relatorio.java

public void GeraRelatorioCondFisico(String caminho, Long id) {
    Document documento = new Document();
    ClienteDAO cdao = new ClienteDAO();
    Cliente c = cdao.consultarPorId(id);
    Paragraph p = new Paragraph("Nome: " + c.getNome() + "\n" + "Id: " + c.getId().toString() + "\n" + "Idade: "
            + c.getIdade().toString() + "\n\n");
    try {/*w  w  w .java2 s  .com*/
        PdfWriter.getInstance(documento, new FileOutputStream(caminho + "/CondFisico.pdf"));
        documento.open();
        PdfPTable tabela = new PdfPTable(2);
        tabela.addCell("Aspecto");
        tabela.addCell("Valor");

        //SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        CondicionamentoFisico cf = c.getCondicionamentoFisico();
        tabela.addCell("IMC");
        tabela.addCell(cf.getComposicaoCorporal().getImc().toString());
        tabela.addCell("Massa Gorda");
        tabela.addCell(cf.getComposicaoCorporal().getMassaGorda().toString());
        tabela.addCell("Massa Magra");
        tabela.addCell(cf.getComposicaoCorporal().getMassaMagra().toString());
        tabela.addCell("Percentual atual de gordura");
        tabela.addCell(cf.getComposicaoCorporal().getPercentualGorduraAtual().toString());
        tabela.addCell("Percentual ideal de gordura");
        tabela.addCell(cf.getComposicaoCorporal().getPercentualGorduraIdeal().toString());
        tabela.addCell("Abdominal");
        tabela.addCell(cf.getDobrasCutaneas().getAbdominal().toString());
        tabela.addCell("Axilar Media");
        tabela.addCell(cf.getDobrasCutaneas().getAxilarMedia().toString());
        tabela.addCell("Bicipital");
        tabela.addCell(cf.getDobrasCutaneas().getBicipital().toString());
        tabela.addCell("Coxa");
        tabela.addCell(cf.getDobrasCutaneas().getCoxa().toString());
        tabela.addCell("Panturrilha");
        tabela.addCell(cf.getDobrasCutaneas().getPanturrilha().toString());
        tabela.addCell("Peitoral");
        tabela.addCell(cf.getDobrasCutaneas().getPeitoral().toString());
        tabela.addCell("Subescapular");
        tabela.addCell(cf.getDobrasCutaneas().getSubescapular().toString());
        tabela.addCell("Supra iliaca");
        tabela.addCell(cf.getDobrasCutaneas().getSupraIliaca().toString());
        tabela.addCell("Tricipital");
        tabela.addCell(cf.getDobrasCutaneas().getTricipital().toString());
        tabela.addCell("Antebrao Direito");
        tabela.addCell(cf.getMembrosInferiores().getAnteBracoDireito().toString());
        tabela.addCell("Brao Contrado Direito");
        tabela.addCell(cf.getMembrosInferiores().getBracoContraidoDireito().toString());
        tabela.addCell("Brao Relaxado Direito");
        tabela.addCell(cf.getMembrosInferiores().getBracoRelaxadoDireito().toString());
        tabela.addCell("Brao Relaxado Esquerdo");
        tabela.addCell(cf.getMembrosInferiores().getBracoRelaxadoEsquerdo().toString());
        tabela.addCell("Coxa Esquerda");
        tabela.addCell(cf.getMembrosInferiores().getCoxaEsquerdo().toString());
        tabela.addCell("Panturrilha Direita");
        tabela.addCell(cf.getMembrosInferiores().getPanturrilhaDireito().toString());
        tabela.addCell("Abdome");
        tabela.addCell(cf.getMembrosSuperiores().getAbdome().toString());
        tabela.addCell("Cintura");
        tabela.addCell(cf.getMembrosSuperiores().getCintura().toString());
        tabela.addCell("Ombro");
        tabela.addCell(cf.getMembrosSuperiores().getOmbro().toString());
        tabela.addCell("Pescoo");
        tabela.addCell(cf.getMembrosSuperiores().getPescoco().toString());
        tabela.addCell("Quadril");
        tabela.addCell(cf.getMembrosSuperiores().getQuadril().toString());
        tabela.addCell("Torx");
        tabela.addCell(cf.getMembrosSuperiores().getToraxRelaxado().toString());
        documento.add(p);

        documento.add(tabela);

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro ao gerar documento:\n" + e.getMessage(), "Erro",
                JOptionPane.ERROR_MESSAGE);
    } finally {
        documento.close();
    }
}

From source file:bookshopautomationsoftware.GenerateReceipt.java

private static void addContent(Document document) throws DocumentException {
    Anchor anchor = new Anchor("BILL DETAILS(Generated by BAS) on " + new Date(), catFont);
    // anchor.setName("First Chapter");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("CUSTOMER BILL", subFont);

    Section subCatPart = catPart.addSection(subPara);
    addEmptyLine(preface, 2);/*from  w w  w .j  a  v a2 s  .  co  m*/
    // add a table
    createTable(subCatPart);

    Paragraph subPara2 = new Paragraph("Thanks for choosing us .. please visit again", subFont);
    Section subCatPart2 = catPart.addSection(subPara2);
    /* Anchor anchor2 = new Anchor("Thanks for choosing us .. please visit again", catFont);
     Chapter catPart2 = new Chapter(new Paragraph(anchor), 1);*/

    // now add all this to the document
    document.add(catPart);
    //document.add(catPart2);

}