Example usage for com.lowagie.text Document add

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

Introduction

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

Prototype


public boolean add(Element element) throws DocumentException 

Source Link

Document

Adds an Element to the Document.

Usage

From source file:biblivre3.administration.reports.SummaryReport.java

License:Open Source License

@Override
protected void generateReportBody(Document document, BaseReportDto reportData) throws Exception {
    SummaryReportDto dto = (SummaryReportDto) reportData;
    Paragraph p1 = new Paragraph(this.getText("REPORTS_SUMMARY_TITLE"));
    p1.setAlignment(Paragraph.ALIGN_CENTER);
    document.add(p1);
    document.add(new Phrase("\n"));
    PdfPTable table = new PdfPTable(10);
    table.setWidthPercentage(100f);// w  w  w . j  a v  a  2  s  .c  o  m
    createHeader(table);
    Collections.sort(dto.getData(), this);
    PdfPCell cell;
    for (String[] data : dto.getData()) {
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[6])));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[0])));
        cell.setColspan(2);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[1])));
        cell.setColspan(2);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[2])));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[3])));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[4])));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[5])));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(this.getSmallFontChunk(data[7])));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        table.addCell(cell);
    }
    document.add(table);
}

From source file:biblivre3.administration.reports.UserReport.java

License:Open Source License

@Override
protected void generateReportBody(Document document, BaseReportDto reportData) throws Exception {
    UserReportDto dto = (UserReportDto) reportData;
    Paragraph p1 = new Paragraph(this.getText("REPORTS_USER_TITLE"));
    p1.setAlignment(Paragraph.ALIGN_CENTER);
    document.add(p1);
    document.add(new Phrase("\n"));

    PdfPTable dataTable = createUserDataTable(dto.getUser());
    document.add(dataTable);//from  w  w w .  ja  v a2  s .co  m

    Paragraph p2 = new Paragraph(this.getText("REPORTS_ADDRESS"));
    p2.setAlignment(Paragraph.ALIGN_CENTER);
    document.add(p2);
    document.add(new Phrase("\n"));
    document.add(createAddressTable(dto.getUser()));

    Paragraph p3 = null;
    if (dto.getLendings() != null && dto.getLendings().size() > 0) {
        document.add(new Phrase("\n"));
        p3 = new Paragraph(this.getText("REPORTS_USER_LENDINGS"));
        p3.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(p3);
        document.add(new Phrase("\n"));
        document.add(createLendingsTable(dto.getLendings()));
    }

    if (dto.getLateLendings() != null && dto.getLateLendings().size() > 0) {
        document.add(new Phrase("\n"));
        p3 = new Paragraph(this.getText("REPORTS_USER_LATE_LENDINGS"));
        p3.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(p3);
        document.add(new Phrase("\n"));
        document.add(createLendingsTable(dto.getLateLendings()));
    }

    if (dto.getReturnedLendings() != null && dto.getReturnedLendings().size() > 0) {
        document.add(new Phrase("\n"));
        p3 = new Paragraph(this.getText("REPORTS_USER_RETURNED_LENDINGS"));
        p3.setAlignment(Paragraph.ALIGN_CENTER);
        document.add(p3);
        document.add(new Phrase("\n"));
        document.add(createLendingsTable(dto.getReturnedLendings()));
    }
}

From source file:biblivre3.cataloging.bibliographic.BiblioBO.java

License:Open Source License

public MemoryFileDTO createFileLabelsPDF(ArrayList<LabelDTO> labels, LabelConfigDTO labelConfig) {
    Document document = new Document();
    final MemoryFileDTO file = new MemoryFileDTO();
    file.setFileName("biblivre_etiquetas_" + new Date().getTime() + ".pdf");
    try {/*  ww  w.  j av a2s .com*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.setPageSize(PageSize.A4);
        float verticalMargin = (297.0f - (labelConfig.getHeight() * labelConfig.getRows())) / 2;
        document.setMargins(7.15f * ApplicationConstants.MM_UNIT, 7.15f * ApplicationConstants.MM_UNIT,
                verticalMargin * ApplicationConstants.MM_UNIT, verticalMargin * ApplicationConstants.MM_UNIT);
        document.open();

        PdfPTable table = new PdfPTable(labelConfig.getColumns());
        table.setWidthPercentage(100f);
        PdfPCell cell;

        int i = 0;
        for (i = 0; i < labelConfig.getOffset(); i++) {
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setFixedHeight(labelConfig.getHeight() * ApplicationConstants.MM_UNIT);
            table.addCell(cell);
        }

        for (LabelDTO ldto : labels) {
            PdfContentByte cb = writer.getDirectContent();

            String holdingSerial = String.valueOf(ldto.getHoldingSerial());
            while (holdingSerial.length() < 10) {
                holdingSerial = "0" + holdingSerial;
            }
            Barcode39 code39 = new Barcode39();
            code39.setExtended(true);
            code39.setCode(holdingSerial);
            code39.setStartStopText(false);

            Image image39 = code39.createImageWithBarcode(cb, null, null);
            if (labelConfig.getHeight() > 30.0f) {
                image39.scalePercent(110f);
            } else {
                image39.scalePercent(90f);
            }

            Paragraph para = new Paragraph();
            Phrase p1 = new Phrase(StringUtils.left(ldto.getAuthor(), 28) + "\n");
            Phrase p2 = new Phrase(StringUtils.left(ldto.getTitle(), 28) + "\n\n");
            Phrase p3 = new Phrase(new Chunk(image39, 0, 0));
            para.add(p1);
            para.add(p2);
            para.add(p3);

            cell = new PdfPCell(para);
            i++;
            cell.setNoWrap(true);
            cell.setFixedHeight(labelConfig.getHeight() * ApplicationConstants.MM_UNIT);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);

            Paragraph para2 = new Paragraph();
            Phrase p5 = new Phrase(ldto.getLocationA() + "\n");
            Phrase p6 = new Phrase(ldto.getLocationB() + "\n");
            Phrase p7 = new Phrase(ldto.getLocationC() + "\n");
            Phrase p8 = new Phrase(ldto.getLocationD() + "\n");
            Phrase p4 = new Phrase(ldto.getAssetHolding() + "\n");
            para2.add(p5);
            para2.add(p6);
            para2.add(p7);
            para2.add(p8);
            para2.add(p4);

            cell = new PdfPCell(para2);
            i++;
            cell.setFixedHeight(labelConfig.getHeight() * ApplicationConstants.MM_UNIT);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
        }
        if ((i % labelConfig.getColumns()) != 0) {
            while ((i % labelConfig.getColumns()) != 0) {
                i++;
                cell = new PdfPCell();
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);
            }
        }
        document.add(table);
        writer.flush();
        document.close();
        file.setFileData(baos.toByteArray());
    } catch (Exception de) {
        System.out.println(de.getMessage());
    }
    return file;
}

From source file:biblivre3.circulation.CirculationBO.java

License:Open Source License

public MemoryFileDTO createFileUserCardsPDF(ArrayList<UserCardDTO> cards, int startOffset, Properties i18n) {
    Document document = new Document();
    MemoryFileDTO file = new MemoryFileDTO();
    file.setFileName("user_cards_" + new Date().getTime() + ".pdf");
    try {/*from   www  . j a va  2 s . c  o m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.setPageSize(PageSize.A4);
        document.setMargins(7.15f * ApplicationConstants.MM_UNIT, 7.15f * ApplicationConstants.MM_UNIT,
                9.09f * ApplicationConstants.MM_UNIT, 9.09f * ApplicationConstants.MM_UNIT);
        document.open();
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100f);
        PdfPCell cell;
        int i = 0;
        for (i = 0; i < startOffset; i++) {
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setFixedHeight(46.47f * ApplicationConstants.MM_UNIT);
            table.addCell(cell);
        }
        for (UserCardDTO card : cards) {
            String userEnrollNumber = String.valueOf(card.getUserId());
            PdfContentByte cb = writer.getDirectContent();
            Barcode39 code39 = new Barcode39();
            code39.setExtended(true);
            while (userEnrollNumber.length() < 10) {
                userEnrollNumber = "0" + userEnrollNumber;
            }
            code39.setCode(userEnrollNumber);
            code39.setStartStopText(false);
            Image image39 = code39.createImageWithBarcode(cb, null, null);
            image39.scalePercent(110f);
            Paragraph para = new Paragraph();
            String name = card.getUserName();
            name = name.length() >= 30 ? name.substring(0, 30) : name;
            Phrase p1 = new Phrase(name + "\n");
            Phrase p2 = new Phrase(new Chunk(image39, 0, 0));
            Phrase p3 = new Phrase(
                    I18nUtils.getText(i18n, "LABEL_USER_SERIAL") + ": " + card.getUserId() + "\n");
            Phrase p4 = new Phrase(
                    I18nUtils.getText(i18n, "LABEL_USER_TYPE") + ": " + card.getUserType() + "\n\n");
            para.add(p1);
            para.add(p3);
            para.add(p4);
            para.add(p2);
            cell = new PdfPCell(para);
            i++;
            cell.setFixedHeight(46.47f * ApplicationConstants.MM_UNIT);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
        }
        if ((i % 3) != 0) {
            while ((i % 3) != 0) {
                i++;
                cell = new PdfPCell();
                cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);
            }
        }
        document.add(table);
        writer.flush();
        document.close();
        writer.close();
        file.setFileData(baos.toByteArray());
    } catch (DocumentException de) {
        System.out.println(de.getMessage());
    }
    return file;
}

From source file:binky.reportrunner.engine.renderers.exporters.HTMLExporter.java

License:Open Source License

@Override
public void export(ResultSet resultSet, String label, OutputStream outputStream) throws ExportException {
    try {/*from  w  ww .jav  a2s .  c  o  m*/
        Document document = new Document();
        HtmlWriter.getInstance(document, outputStream);

        // open the document object
        document.open();

        ResultSetMetaData metaData = resultSet.getMetaData();
        Table table = new Table(metaData.getColumnCount());
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            Paragraph para = new Paragraph(metaData.getColumnName(i), new Font(Font.HELVETICA, 10, Font.BOLD));
            Cell cell = new Cell(para);
            table.addCell(cell);
        }

        while (resultSet.next()) {
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                Paragraph para = new Paragraph("" + resultSet.getObject(i),
                        new Font(Font.HELVETICA, 10, Font.NORMAL));
                Cell cell = new Cell(para);
                table.addCell(cell);
            }

        }
        document.add(table);
        document.close();
    } catch (DocumentException e) {
        throw new ExportException(e.getMessage(), e);
    } catch (SQLException e) {
        throw new ExportException(e.getMessage(), e);
    }
}

From source file:binky.reportrunner.engine.renderers.exporters.PDFExporter.java

License:Open Source License

@Override
public void export(ResultSet resultSet, String label, OutputStream outputStream) throws ExportException {
    try {//from   w ww  .j ava  2s  .c  om
        Document document = new Document();
        PdfWriter.getInstance(document, outputStream);

        // open the document object
        document.open();

        ResultSetMetaData metaData = resultSet.getMetaData();
        PdfPTable table = new PdfPTable(metaData.getColumnCount());
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            Paragraph para = new Paragraph(metaData.getColumnName(i), new Font(Font.HELVETICA, 10, Font.BOLD));
            PdfPCell cell = new PdfPCell(para);
            table.addCell(cell);
        }

        while (resultSet.next()) {
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                Paragraph para = new Paragraph("" + resultSet.getObject(i),
                        new Font(Font.HELVETICA, 10, Font.NORMAL));
                PdfPCell cell = new PdfPCell(para);
                table.addCell(cell);
            }

        }
        document.add(table);
        document.close();
    } catch (DocumentException e) {
        throw new ExportException(e.getMessage(), e);
    } catch (SQLException e) {
        throw new ExportException(e.getMessage(), e);
    }
}

From source file:br.com.moises.servlet.Embarque.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    out.print("Pasou auqe");

    try {//w w  w. j  a v a 2  s .c om
        Document document = new Document(PageSize.A4, 30, 20, 20, 30);
        OutputStream outputStream = new FileOutputStream("embarque.pdf");
        //   response.setContentType("application/pdf");
        Long id = Long.parseLong(request.getParameter("num_embarque"));
        //recupera o embarque
        embarque = embarqueSuport.getEmbarqueById(id);
        itens = itSuport.itensEmbarquePorEmbarque(embarque);
        out.print(itens.size());

        PdfWriter.getInstance(document, response.getOutputStream());

        document.open();
        // Titulo

        document.addTitle("Comprovante de Coleta");

        Paragraph titulo = new Paragraph("Comprovante de Coleta");
        //            //
        document.add(titulo);
        document.add(new Paragraph("\n\n"));
        PdfPTable topo = new PdfPTable(2);
        Image image = Image.getInstance("E:\\icons\\32\\cliente.png");

        topo.getDefaultCell().setBorder(0);
        topo.getDefaultCell().addElement(image);
        topo.addCell(image);
        topo.addCell("MOISES JUVENAL DA SILVA\n" + "Rua.: Bertioga N 49" + "Jardim Amrica I"
                + "Vrzea Paulista SP");

        document.add(topo);
        document.add(new Paragraph(
                "-------------------------------------------------------------------------------------------------------------------"));
        document.add(new Paragraph(
                "TRANSP.: " + embarque.getTransportadora().getId() + " - "
                        + embarque.getTransportadora().getNome(),
                FontFactory.getFont(FontFactory.HELVETICA, 14, Color.BLUE)));
        document.add(new Paragraph(
                "-------------------------------------------------------------------------------------------------------------------"));
        document.add(new Paragraph(new Date().toString()));

        PdfPTable table = new PdfPTable(2);
        table.addCell("");

        for (ItensEmbarque l : itens) {
            document.add(new Paragraph(String.valueOf(l.getId())));
        }
        // step 5
        document.close();
    } catch (DocumentException de) {
        throw new IOException(de.getMessage());
    }

}

From source file:br.com.moises.servlet.RelatorioPessoa.java

private void mostrarRelatorio(byte[] arquivo, HttpServletResponse response) {
    /**/*from  w  w w  . ja  v a 2  s . c  om*/
    * A simple Hello World Servlet.
    * @see HttpServlet#doGet(
    *      HttpServletRequest request, HttpServletResponse response)
    */

    response.setContentType("application/pdf");
    try {
        // step 1
        Document document = new Document();
        try {
            // step 2
            PdfWriter.getInstance(document, response.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(RelatorioPessoa.class.getName()).log(Level.SEVERE, null, ex);
        }
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        document.add(new Paragraph(new Date().toString()));
        // step 5

        document.close();
    } catch (DocumentException de) {
        try {
            throw new IOException(de.getMessage());
        } catch (IOException ex) {
            Logger.getLogger(RelatorioPessoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:br.com.nfsconsultoria.azcontrole.bean.VendaBean.java

public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
    Document pdf = (Document) document;
    pdf.setPageSize(PageSize.A4);//  w ww.j  a  va 2  s .c o  m
    pdf.addAuthor("Luis Carlos Santos");
    pdf.addTitle("Acordo Cadastrados");
    pdf.addCreator("NFS Consultoria");
    pdf.addSubject("Acordo Cadastrados");
    pdf.open();

    Font catFont = new Font(Font.TIMES_ROMAN, 18, Font.BOLD);

    Paragraph p = new Paragraph("Relatrio de Acordos", catFont);
    p.setAlignment(Element.ALIGN_CENTER);
    p.setSpacingAfter(20);

    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String logo = externalContext.getRealPath("") + File.separator + "resources" + File.separator + "images"
            + File.separator + "banner.png";

    pdf.add(Image.getInstance(logo));
    pdf.add(p);
}

From source file:br.edu.ifrs.restinga.modulorh.controle.SetorController.java

@RequestMapping(value = "/setor/gerarPDF")
public String gerar(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DocumentException {
    response.setContentType("application/pdf");

    try {/*from   ww w. j  a v  a  2  s  .c  o m*/

        Document document = new Document();

        PdfWriter.getInstance(document, response.getOutputStream());

        Image image = Image.getInstance("http://www.planalto.gov.br/cciVil_03/decreto/Quadros/anteri1.gif");
        image.setAlignment(Image.ALIGN_CENTER);

        document.open();

        document.add(image);

        document.add(new Paragraph("Ministrio da Educao"));
        document.add(
                new Paragraph("Instituto Federal de Educao, Cincia e Tecnologia do Rio Grande do Sul "));
        document.add(new Paragraph("Campus Restinga"));

        document.close();
    } catch (DocumentException de) {
        throw new IOException(de.getMessage());
    }
    return "forward:/setor/gerar";

}