Example usage for com.lowagie.text Document Document

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

Introduction

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

Prototype


public Document(Rectangle pageSize) 

Source Link

Document

Constructs a new Document -object.

Usage

From source file:com.afunms.report.abstraction.ExcelReport1.java

public void createReport_equipmentWord(String filename, String roomname, String cabinetname, String uselect,
        String unumber, String rate) throws DocumentException, IOException {
    if (impReport.getTable() == null) {
        fileName = null;/*  w  w  w .  ja va  2  s .c om*/
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        RtfWriter2.getInstance(document, new FileOutputStream(filename));
        document.open();
        // new File(filename)
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        // 
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
        // title.setFont(titleFont);
        List equipmentlist = (List) reportHash.get("equipmentlist");

        Paragraph context = new Paragraph();
        // 
        context.setAlignment(Element.ALIGN_CENTER);
        //         context.setFont(contextFont);
        // 
        //         context.setSpacingBefore(0);
        // 
        //         context.setFirstLineIndent(6);
        //         document.add(context);
        //         document.add(new Paragraph("\n"));
        Table aTable = new Table(6);
        //         float[] widths = { 220f, 220f, 220f, 110f, 110f, 110f, 110f, 220f };
        //         aTable.setWidths(widths);
        aTable.setWidth(100); //  90%
        aTable.setAlignment(Element.ALIGN_CENTER);// 
        aTable.setAutoFillEmptyCells(true); // 
        //         aTable.setBorderWidth(1); // 
        //         aTable.setBorderColor(new Color(0, 125, 255)); // 
        //         aTable.setPadding(2);// 
        //         aTable.setSpacing(4);// 
        //         aTable.setBorder(2);// 
        Cell cell = null;
        cell = new Cell(new Phrase(roomname + cabinetname + "", titleFont));
        cell.setColspan(6);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + roomname, titleFont));
        cell.setColspan(3);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + cabinetname, titleFont));
        cell.setColspan(3);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U:" + uselect, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + unumber, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + rate, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);
        EquipmentReport equipmentReport = null;
        if (equipmentlist != null && equipmentlist.size() > 0) {
            for (int i = 0; i < equipmentlist.size(); i++) {

                equipmentReport = (EquipmentReport) equipmentlist.get(i);
                cell = new Cell(new Phrase(1 + i + "", contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getEquipmentname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

                cell = new Cell(new Phrase(equipmentReport.getEquipmentdesc(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getOperation(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getContactname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getContactphone(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

            }
        }
        document.add(aTable);
        document.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:com.afunms.report.abstraction.ExcelReport1.java

public void createReport_OperEquipmentWord(String filename, String opername, String contactname,
        String contactphone) throws DocumentException, IOException {
    if (impReport.getTable() == null) {
        fileName = null;//from  w w w  .  ja v a  2  s .  com
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        RtfWriter2.getInstance(document, new FileOutputStream(filename));
        document.open();
        // new File(filename)
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        // 
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
        // title.setFont(titleFont);
        List equipmentlist = (List) reportHash.get("equipmentlist");

        Paragraph context = new Paragraph();
        // 
        context.setAlignment(Element.ALIGN_CENTER);
        //         context.setFont(contextFont);
        // 
        //         context.setSpacingBefore(0);
        // 
        //         context.setFirstLineIndent(6);
        //         document.add(context);
        //         document.add(new Paragraph("\n"));
        Table aTable = new Table(7);
        //         float[] widths = { 220f, 220f, 220f, 110f, 110f, 110f, 110f, 220f };
        //         aTable.setWidths(widths);
        aTable.setWidth(100); //  90%
        aTable.setAlignment(Element.ALIGN_CENTER);// 
        aTable.setAutoFillEmptyCells(true); // 
        //         aTable.setBorderWidth(1); // 
        //         aTable.setBorderColor(new Color(0, 125, 255)); // 
        //         aTable.setPadding(2);// 
        //         aTable.setSpacing(4);// 
        //         aTable.setBorder(2);// 
        Cell cell = null;
        cell = new Cell(new Phrase(opername + "", titleFont));
        cell.setColspan(7);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + opername, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + contactname, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + contactphone, titleFont));
        cell.setColspan(3);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("IP", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);
        OperCabinet operCabinet = null;
        if (equipmentlist != null && equipmentlist.size() > 0) {
            for (int i = 0; i < equipmentlist.size(); i++) {

                operCabinet = (OperCabinet) equipmentlist.get(i);
                cell = new Cell(new Phrase(operCabinet.getRoomname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getCabinetname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

                cell = new Cell(new Phrase(operCabinet.getUseu(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getEquipmentname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getIpaddress(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getCabinetname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getContactphone(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

            }
        }
        document.add(aTable);
        document.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:com.AppModel.Reportes.GeneraPdf.java

public void generarArchivoPdf(String xml) {
    try {// w w  w .  j a v  a  2s  .  com
        Document document = new Document(PageSize.A4);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("plantillaa.pdf"));
        document.open();
        document.addAuthor("Persona creadora");
        document.addCreator("Software generador");
        document.addCreationDate();
        document.addTitle("Titulo del documento");

        HTMLWorker htmlWorker = new HTMLWorker(document);
        //            String str = "<table>\n"
        //                    + "            <tr>\n"
        //                    + "                <td>Boleta</td>\n"
        //                    + "                <td>Fecha</td>\n"
        //                    + "                <th>Operacion</th>\n"
        //                    + "                <td>Abono</td>\n"
        //                    + "                <td>Interes generado </td>\n"
        //                    + "                <td>Cargo</td>\n"
        //                    + "                <td>Usuario que realizo el movimiento</td>    \n"
        //                    + "            </tr>\n"
        //                    + "            <tr>\n"
        //                    + "                <td align=center>MLOM005817</td>\n"
        //                    + "                <td align=center>2015-04-19 15:53:37.0</td>\n"
        //                    + "                <td align=center>Nuevo empeo</td>\n"
        //                    + "                <td align=center></td>\n"
        //                    + "                <td align=center></td>\n"
        //                    + "                <td align=center>$533.00</td>\n"
        //                    + "                <td align=center>admin</td>\n"
        //                    + "            </tr>\n"
        //                    + "            <tr>\n"
        //                    + "                <td align=center>MLOM005817</td>\n"
        //                    + "                <td align=center>2015-04-19 15:56:04.0</td>\n"
        //                    + "                <td align=center>Extension de contrato</td>\n"
        //                    + "                <td align=center></td><td align=center>$1.44</td>\n"
        //                    + "                <td align=center></td>\n"
        //                    + "                <td align=center>admin</td>\n"
        //                    + "            </tr>\n"
        //                    + "            <tr>\n"
        //                    + "                <td align=center>MLOM005817</td>\n"
        //                    + "                <td align=center>2015-04-19 16:00:51.0</td>\n"
        //                    + "                <td align=center>Desempeo</td>\n"
        //                    + "                <td align=center>$554.64 </td>\n"
        //                    + "                <td align=center>$21.64 </td>\n"
        //                    + "                <td align=center></td>\n"
        //                    + "                <td align=center>admin</td>\n"
        //                    + "            </tr>\n"
        //                    + "            <tr>\n"
        //                    + "                <td align=center>MLOM005818</td>\n"
        //                    + "                <td align=center>2015-04-19 16:37:24.0</td>\n"
        //                    + "                <td align=center>Refrendo</td>\n"
        //                    + "                <td align=center>$533.00 </td>\n"
        //                    + "                <td align=center>$21.64 </td>\n"
        //                    + "                <td align=center>$533.00 </td>\n"
        //                    + "                <td align=center>admin</td>\n"
        //                    + "            </tr>\n"
        //                    + "            <tr>\n"
        //                    + "                <td></td>\n"
        //                    + "                <td></td>\n"
        //                    + "                <td></td>\n"
        //                    + "                <td></td>\n"
        //                    + "                <td align=center><b>$1,087.64 pesos</b> </td>\n"
        //                    + "                <td align=center><b>$44.72 pesos</b> </td>\n"
        //                    + "                <td align=center><b>$1,066.00 pesos</b> </td>\n"
        //                    + "            </tr>\n"
        //                    + "            <tr style=height:50px;>\n"
        //                    + "                <td align=right colspan=7> </td>\n"
        //                    + "                <td  align=left colspan=5><b>El monto de apertura dia fue de: $90000.0 pesos</b> </td>\n"
        //                    + "                <td  align=left colspan=5><b id='saldoAlCierre' title='89511.72'>Saldo al cierre: $89511.72 pesos</b> </td>\n"
        //                    + "            </tr>\n"
        //                    + "        </table>";
        String str = "<table>\n" + "            <tr>\n" + "                <td>Boleta</td>\n"
                + "                <td>Fecha</td>\n" + "                <th>Operacion</th>\n"
                + "                <td>Abono</td>\n" + "                <td>Interes generado </td>\n"
                + "                <td>Cargo</td>\n"
                + "                <td>Usuario que realizo el movimiento</td>    \n" + "            </tr>\n"
                + "            <tr>\n" + "                <td align=center>MLOM005817</td>\n"
                + "                <td align=center>2015-04-19 15:53:37.0</td>\n"
                + "                <td align=center>Nuevo empeo</td>\n"
                + "                <td align=center></td>\n" + "                <td align=center></td>\n"
                + "                <td align=center>$533.00</td>\n"
                + "                <td align=center>admin</td>\n" + "            </tr>\n" + "            <tr>\n"
                + "                <td align=center>MLOM005817</td>\n"
                + "                <td align=center>2015-04-19 15:56:04.0</td>\n"
                + "                <td align=center>Extension de contrato</td>\n"
                + "                <td align=center></td><td align=center>$1.44</td>\n"
                + "                <td align=center></td>\n" + "                <td align=center>admin</td>\n"
                + "            </tr>\n" + "            <tr>\n"
                + "                <td align=center>MLOM005817</td>\n"
                + "                <td align=center>2015-04-19 16:00:51.0</td>\n"
                + "                <td align=center>Desempeo</td>\n"
                + "                <td align=center>$554.64 </td>\n"
                + "                <td align=center>$21.64 </td>\n" + "                <td align=center></td>\n"
                + "                <td align=center>admin</td>\n" + "            </tr>\n" + "            <tr>\n"
                + "                <td align=center>MLOM005818</td>\n"
                + "                <td align=center>2015-04-19 16:37:24.0</td>\n"
                + "                <td align=center>Refrendo</td>\n"
                + "                <td align=center>$533.00 </td>\n"
                + "                <td align=center>$21.64 </td>\n"
                + "                <td align=center>$533.00 </td>\n"
                + "                <td align=center>admin</td>\n" + "            </tr>\n" + "            <tr>\n"
                + "                <td></td>\n" + "                <td></td>\n" + "                <td></td>\n"
                + "                <td align=center><b>$1,087.64 pesos</b> </td>\n"
                + "                <td align=center><b>$44.72 pesos</b> </td>\n"
                + "                <td align=center><b>$1,066.00 pesos</b> </td>\n" + "            </tr>\n"
                + "            <tr style=height:50px;>\n" + "                <td align=right colspan=7> </td>\n"
                + "            </tr><tr>\n"
                + "                <td  align=left colspan=5><b>El monto de apertura dia fue de: $90000.0 pesos</b> </td>\n"
                + "            </tr><tr>\n"
                + "                <td  align=left colspan=5><b id='saldoAlCierre' title='89511.72'>Saldo al cierre: $89511.72 pesos</b> </td>\n"
                + "            </tr>\n" + "        </table>";
        htmlWorker.parse(new StringReader(str));
        document.close();
    } catch (Exception e) {
        System.out.println("" + e.getMessage());
    }

}

From source file:com.aripd.clms.service.ContractServiceBean.java

@Override
public void generatePdf(ContractEntity contract) {
    String baseFontUrl = "/fonts/Quivira.otf";
    FontFactory.register(baseFontUrl);//from  w w  w  .  j  a  v a2 s.c  o  m

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        BaseFont bf = BaseFont.createFont(baseFontUrl, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font18n = new Font(bf, 18, Font.NORMAL);
        Font font12n = new Font(bf, 12, Font.NORMAL);
        Font font8n = new Font(bf, 8, Font.NORMAL);
        Font font8nbu = new Font(bf, 8, Font.BOLD | Font.UNDERLINE);
        Font font8ng = new Font(bf, 8, Font.NORMAL, Color.DARK_GRAY);
        Font font6n = new Font(bf, 6, Font.NORMAL);

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        addMetaData(document);
        addTitlePage(document, contract);
        Image imgBlue = Image.getInstance(1, 1, 3, 8, new byte[] { (byte) 0, (byte) 0, (byte) 255, });
        imgBlue.scaleAbsolute(document.getPageSize().getWidth(), 10);
        imgBlue.setAbsolutePosition(0, document.getPageSize().getHeight() - imgBlue.getScaledHeight());
        PdfImage stream = new PdfImage(imgBlue, "", null);
        stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
        PdfIndirectObject ref = writer.addToBody(stream);
        imgBlue.setDirectReference(ref.getIndirectReference());
        document.add(imgBlue);

        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);

        PdfPCell cell = new PdfPCell(new Paragraph(contract.getName(), font18n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Version: " + contract.getVersion(), font8n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Review: " + contract.getReview(), font8n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(contract.getRemark(), font12n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        document.add(table);
        // Start a new page
        document.newPage();

        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.parse(new StringReader(contract.getRemark()));
        // Start a new page
        document.newPage();

        document.add(new Paragraph("Review Board", font18n));
        document.add(new LineSeparator(0.5f, 100, null, 0, -5));

        table = new PdfPTable(3);
        table.setWidthPercentage(100);

        cell = new PdfPCell(new Paragraph("Review Board", font18n));
        cell.setColspan(3);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Version", font12n));
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Date", font12n));
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Review", font12n));
        table.addCell(cell);
        for (HistoryContractEntity history : historyContractService.listing(contract)) {
            cell = new PdfPCell(new Paragraph(history.getVersion().toString(), font8n));
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(history.getStartdate().toString(), font8n));
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(history.getReview(), font8n));
            table.addCell(cell);
        }
        document.add(table);

        document.close();

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();
        response.addHeader("Content-Type", "application/force-download");
        String filename = URLEncoder.encode(contract.getName() + ".pdf", "UTF-8");
        //            response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename);
        response.getOutputStream().write(output.toByteArray());
        response.getOutputStream().flush();
        context.responseComplete();
        context.renderResponse();

    } catch (BadPdfFormatException | IOException ex) {
        Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.aryjr.nheengatu.pdf.PDFDocument.java

License:Open Source License

public void generateFile(final OutputStream out) throws IOException {
    final Document document;
    if (styleSheet == null || styleSheet.getStyles() == null || !styleSheet.getStyles().containsKey("@page")) {
        document = new Document(PageSize.A4);
        document.setMargins(3.0f * PDFDocument.CM_UNIT, 2.0f * PDFDocument.CM_UNIT, 1.0f * PDFDocument.CM_UNIT,
                2.0f * PDFDocument.CM_UNIT);
        System.out.println("Processamento: terminou setMargins");
    } else {/*from   ww w  . j a  va2  s  . c o m*/
        Style style = ((Style) (styleSheet.getStyles().get("@page")));
        if ("landscape".equals(style.getPropertyValue("size"))) {
            document = new Document(PageSize.A4.rotate());
        } else {
            document = new Document(PageSize.A4);
        }
        String s = style.getPropertyValue("margin-left");
        Float f = styleMeasure(s, "3cm");
        document.setMargins(styleMeasure(style.getPropertyValue("margin-left"), "3cm"),
                styleMeasure(style.getPropertyValue("margin-right"), "2cm"),
                styleMeasure(style.getPropertyValue("margin-top"), "1cm"),
                styleMeasure(style.getPropertyValue("margin-bottom"), "2cm"));
        System.out.println("Processamento: terminou setMargins do else");
    }
    try {
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        writer.setPageEvent(new PDFPageBreak(writer, document, headFirstPage, footFirstPage, head, foot));
        document.open();
        final MultiColumnText mct = new MultiColumnText();
        // set up 3 even columns with 10pt space between
        mct.addRegularColumns(document.left(), document.right(), 0f, 1);

        System.out.println("Processamento: prestes a extract visible components");

        // Extracting the document content
        extractVisibleComponents(body, document, mct, null, null);

        document.add(mct);
        document.close();
    } catch (final DocumentException de) {
        System.err.println(de.getMessage());
    }
}

From source file:com.bean.UserBean.java

public void gerarPDF1() {

    try {//from w w w.  j av a  2  s  .c  o m

        Document doc = new Document(PageSize.A4);

        OutputStream os = new FileOutputStream("C:/Users/lprates/Downloads/out.pdf");
        PdfWriter.getInstance(doc, os);
        doc.open();

        Paragraph p = new Paragraph("Meu primeiro arquivo PDF!");

        doc.add(p);

        doc.close();
        os.close();

        //abre pdf usando o PDF Reader instalado na maquina do Usuario
        Desktop.getDesktop().open(new File("C:/Users/lprates/Downloads/out.pdf"));

    } catch (Exception e) {

    }

}

From source file:com.beetle.framework.web.controller.DocumentController.java

License:LGPL

private void createPDF(OutputStream out, IDocument doc, WebInput wi) throws IOException, ServletException {
    this.setContentType("application/pdf");
    ByteArrayOutputStream baos = new ByteArrayOutputStream(INITIAL_SIZE);
    Document document = new Document(PageSize.A4);
    DocInfo di;/*from  w  w w  . j a  v a 2 s.co m*/
    try {
        PdfWriter.getInstance(document, baos);
        di = new DocInfo(IDocument.TYPE_PDF);
        di.setPdfDocument(document);
        // 
        doc.createAttribute(wi, di);
        document.open();
        // 
        doc.createContent(wi, di);
        document.close();
        this.setContentLength(baos.size());
        baos.writeTo(out);
    } catch (Exception ex1) {
        throw new ServletException(ex1);
    } finally {
        if (out != null) {
            out.flush();
            out.close();
            out = null;
        }
        if (baos != null) {
            baos.close();
            baos = null;
        }
        document = null;
        di = null;
        doc = null;
    }
}

From source file:com.benfante.minimark.blo.AssessmentPdfBuilder.java

License:Apache License

/**
 * Build the contatenated PDF for a list of assessments.
 *
 * @param assessments The assessments/*from w w  w  .j  ava 2s. com*/
 * @param baseUrl The base URL for retrieving images and resource. If null it will not be set.
 * @param locale The locale for producing the document. Id null, the default locale will be used.
 * @return The PDF document.
 */
public byte[] buildPdf(List<AssessmentFilling> assessments, String baseUrl, Locale locale) throws Exception {
    ByteArrayOutputStream pdfos = new ByteArrayOutputStream();
    List master = new ArrayList();
    Document document = null;
    PdfCopy writer = null;
    PdfOutline rootOutline = null;
    try {
        int f = 0;
        int pageOffset = 0;
        for (AssessmentFilling assessment : assessments) {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(buildPdf(assessment, baseUrl, locale));
            reader.consolidateNamedDestinations();
            // we retrieve the total number of pages
            int n = reader.getNumberOfPages();
            List bookmarks = SimpleBookmark.getBookmark(reader);
            if (bookmarks != null) {
                if (pageOffset != 0) {
                    SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                }
                master.addAll(bookmarks);
            }
            pageOffset += n;
            if (f == 0) {
                // step 1: creation of a document-object
                document = new Document(reader.getPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                writer = new PdfCopy(document, pdfos);
                writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
                // step 3: we open the document
                document.open();
                // initialize rootOutline for assessment bookmarks creation
                PdfContentByte cb = writer.getDirectContent();
                rootOutline = cb.getRootOutline();
            }
            // step 4: we add content
            new PdfOutline(rootOutline, new PdfDestination(PdfDestination.FIT),
                    assessment.getLastName() + " " + assessment.getFirstName());
            PdfImportedPage page;
            for (int i = 0; i < n;) {
                ++i;
                page = writer.getImportedPage(reader, i);
                writer.addPage(page);
            }
            PRAcroForm form = reader.getAcroForm();
            if (form != null) {
                writer.copyAcroForm(reader);
            }
            f++;
        }
    } finally {
        if (pdfos != null) {
            try {
                pdfos.close();
            } catch (IOException ioe) {
            }
        }
        if (!master.isEmpty()) {
            writer.setOutlines(master);
        }
        // step 5: we close the document
        if (document != null && document.isOpen()) {
            document.close();
        }
    }
    return pdfos.toByteArray();
}

From source file:com.byterefinery.rmbench.export.diagram.PDFDiagramExporter.java

License:Open Source License

protected void doExport(OutputStream out, IFigure figure) {

    Rectangle bounds = getBounds(figure);
    Document document = new Document(new com.lowagie.text.Rectangle(bounds.width, bounds.height));

    PdfWriter pdf;//  www  . j  a v a2  s  .c om
    try {
        pdf = PdfWriter.getInstance(document, out);
        document.open();
        document.add(new Chunk(" "));
    } catch (DocumentException e) {
        ExportPlugin.logError(e);
        return;
    }
    PdfContentByte contentbytes = pdf.getDirectContent();
    PdfTemplate template = contentbytes.createTemplate(bounds.width, bounds.height);
    Graphics2D graphics2d = template.createGraphics(bounds.width, bounds.height);
    try {
        GraphicsToGraphics2DAdaptor graphics = new GraphicsToGraphics2DAdaptor(graphics2d,
                bounds.getTranslated(bounds.getLocation().negate()));
        graphics.translate(bounds.getLocation().negate());
        figure.paint(graphics);
    } finally {
        graphics2d.dispose();
        contentbytes.addTemplate(template, 0, 0);
        document.close();
    }
}

From source file:com.centurylink.mdw.designer.pages.ExportHelper.java

License:Apache License

public void printImagePdf(String filename, DesignerCanvas canvas, Dimension graphsize) {
    try {/*from  w ww  .j  ava2  s  . c om*/
        DefaultFontMapper mapper = new DefaultFontMapper();
        FontFactory.registerDirectories();
        mapper.insertDirectory("c:\\winnt\\fonts");
        // mapper.insertDirectory("c:\\windows\\fonts");
        // we create a template and a Graphics2D object that corresponds
        // with it
        int margin = 72; // 1 inch
        float scale = 0.5f;
        boolean multiple_page = true;
        Rectangle page_size;
        if (multiple_page) {
            page_size = PageSize.LETTER.rotate();
        } else {
            page_size = new Rectangle((int) (graphsize.getWidth() * scale) + margin,
                    (int) (graphsize.getHeight() * scale) + margin);
        }
        Document document = new Document(page_size);
        DocWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        document.setPageSize(page_size);
        int image_w = (int) page_size.getWidth() - margin;
        int image_h = (int) page_size.getHeight() - margin;
        boolean edsave = canvas.editable;
        canvas.editable = false;
        Color bgsave = canvas.getBackground();
        canvas.setBackground(Color.white);
        if (multiple_page) {
            int horizontal_pages = (int) (graphsize.width * scale) / image_w + 1;
            int vertical_pages = (int) (graphsize.height * scale) / image_h + 1;
            for (int i = 0; i < horizontal_pages; i++) {
                for (int j = 0; j < vertical_pages; j++) {
                    Image img;
                    PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
                    PdfTemplate tp = cb.createTemplate(image_w, image_h);
                    Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper);
                    tp.setWidth(image_w);
                    tp.setHeight(image_h);
                    g2.scale(scale, scale);
                    g2.translate(-i * image_w / scale, -j * image_h / scale);
                    canvas.paintComponent(g2);
                    g2.dispose();
                    img = new ImgTemplate(tp);
                    document.add(img);
                }
            }
        } else {
            Image img;
            PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
            PdfTemplate tp = cb.createTemplate(image_w, image_h);
            Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper);
            tp.setWidth(image_w);
            tp.setHeight(image_h);
            g2.scale(scale, scale);
            canvas.paintComponent(g2);
            g2.dispose();
            img = new ImgTemplate(tp);
            document.add(img);
        }
        canvas.setBackground(bgsave);
        canvas.editable = edsave;
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}