Example usage for com.lowagie.text.pdf PdfWriter getInstance

List of usage examples for com.lowagie.text.pdf PdfWriter getInstance

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter getInstance.

Prototype


public static PdfWriter getInstance(Document document, OutputStream os) throws DocumentException 

Source Link

Document

Use this method to get an instance of the PdfWriter.

Usage

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

public void createReport_OperEquipmentPdf(String filename, String opername, String contactname,
        String contactphone) throws DocumentException, IOException {
    if (impReport.getTable() == null) {
        fileName = null;//from w w  w  .  j  a  va  2s .  c  o m
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        PdfWriter.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);
        //         Paragraph title = new Paragraph(room + "", titleFont);
        // 
        //         title.setAlignment(Element.ALIGN_CENTER);
        //         // title.setFont(titleFont);
        //         document.add(title);
        List equipmentlist = (List) reportHash.get("equipmentlist");

        //         Paragraph context = new Paragraph();
        //         // 
        //         context.setAlignment(Element.ALIGN_LEFT);
        ////         context.setFont(contextFont);
        //         // 
        //         context.setSpacingBefore(5);
        //         // 
        //         context.setFirstLineIndent(6);
        //         document.add(context);
        //         document.add(new Paragraph("\n"));
        PdfPTable aTable = new PdfPTable(7);
        PdfPCell cell = null;
        cell = new PdfPCell(new Phrase(opername + "", titleFont));
        cell.setColspan(7);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);
        cell = new PdfPCell(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 PdfPCell(new Phrase(operCabinet.getRoomname(), contextFont));

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

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

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

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

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

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

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new PdfPCell(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.allinfinance.system.util.PdfUtil.java

License:Open Source License

public static void create(String mchtId, String selMchtId, String path, List<Object[]> list,
        LinkedHashMap<String, List<Object[]>> map, Set<String> set) throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ////from  www  .  j  ava 2  s  .c  o  m
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font9 = new Font(bfChinese, 9, Font.NORMAL);
    Font font9Bold = new Font(bfChinese, 9, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.35f, 0.1f, 0.1f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph(mchtId, font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph(InformationUtil.getMchtName(mchtId), font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    String point = InformationUtil.getCurPaperPoint(selMchtId);
    PdfPCell cellPoint;
    cellPoint = new PdfPCell(new Paragraph(point, font8Red));
    cellPoint.setBorder(PdfPCell.NO_BORDER);
    cellPoint.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellLevel;
    String level = InformationUtil.getCurPaperLevel(selMchtId);
    if (Integer.valueOf(point) >= 60) {
        cellLevel = new PdfPCell(new Paragraph(level, font8Green));
    } else {
        cellLevel = new PdfPCell(new Paragraph(level, font8Red));
    }
    cellLevel.setBorder(PdfPCell.NO_BORDER);
    cellLevel.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(5);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(5);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellPoint);

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellLevel);

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font9Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            if (set.contains(o[0])) {
                opt += "? ";
                opt += o[1].toString();
                opt += "         ";
            } else {
                opt += " ";
                opt += o[1].toString();
                opt += "         ";
            }
        }
        c.addElement(new Paragraph(opt.trim(), font9));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");

}

From source file:com.allinfinance.system.util.PdfUtil.java

License:Open Source License

public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map)
        throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ////from  w ww. j av a 2  s.c  o  m
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font10 = new Font(bfChinese, 10, Font.NORMAL);
    Font font10Bold = new Font(bfChinese, 10, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.55f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(3);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(3);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            opt += " ";
            opt += o[1].toString();
            opt += "         ";
        }
        c.addElement(new Paragraph(opt.trim(), font10));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");

}

From source file:com.amphisoft.epub2pdf.Converter.java

License:Open Source License

public void convert(String epubPath) throws IOException, DocumentException {
    File epubFile = new File(epubPath);
    if (!(epubFile.canRead())) {
        throw new IOException("Could not read " + epubPath);
    } else {//  w w w  .  jav  a2  s .c  om
        System.err.println("Converting " + epubFile.getAbsolutePath());
    }
    String epubFilename = epubFile.getName();
    String epubFilenameBase = epubFilename.substring(0, epubFilename.length() - 5);
    String pdfFilename = epubFilenameBase + ".pdf";

    File outputFile = new File(outputDir.getAbsolutePath() + File.separator + pdfFilename);

    epubIn = Epub.fromFile(epubPath);
    XhtmlHandler.setSourceEpub(epubIn);

    Opf opf = epubIn.getOpf();
    List<String> contentPaths = opf.spineHrefs();
    List<File> contentFiles = new ArrayList<File>();
    for (String path : contentPaths) {
        contentFiles.add(new File(epubIn.getContentRoot(), path));
    }
    Ncx ncx = epubIn.getNcx();

    List<NavPoint> ncxToc = new ArrayList<NavPoint>();
    if (ncx != null) {
        ncxToc.addAll(ncx.getNavPointsFlat());
    }

    Tree<TocTreeNode> tocTree = TocTreeNode.buildTocTree(ncx);
    XhtmlHandler.setTocTree(tocTree);

    Document doc = new Document();
    boolean pageSizeOK = doc.setPageSize(pageSize);
    boolean marginsOK = doc.setMargins(marginLeftPt, marginRightPt, marginTopPt, marginBottomPt);

    System.err.println("Writing PDF to " + outputFile.getAbsolutePath());
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outputFile));
    writer.setStrictImageSequence(true);
    PdfOutline bookmarkRoot = null;

    if (!(pageSizeOK && marginsOK)) {
        throw new RuntimeException("Failed to set PDF page size a/o margins");
    }

    int fileCount = contentFiles.size();
    printlnerr("Processing " + fileCount + " HTML file(s): ");
    int currentFile = 0;

    for (File file : contentFiles) {
        currentFile++;

        char progressChar;

        int mod10 = currentFile % 10;
        if (mod10 == 5)
            progressChar = '5';
        else if (mod10 == 0)
            progressChar = '0';
        else
            progressChar = '.';

        printerr(progressChar);
        if (!(doc.isOpen())) {
            doc.open();
            doc.newPage();
            bookmarkRoot = writer.getRootOutline();
            XhtmlHandler.setBookmarkRoot(bookmarkRoot);
        }
        NavPoint fileLevelNP = Ncx.findNavPoint(ncxToc, file.getName());
        TreeNode<TocTreeNode> npNode = TocTreeNode.findInTreeByNavPoint(tocTree, fileLevelNP);

        if (fileLevelNP != null) {
            doc.newPage();
            PdfOutline pdfOutlineParent = bookmarkRoot;
            if (npNode != null) {
                TreeNode<TocTreeNode> parent = npNode.getParent();
                if (parent != null) {
                    TocTreeNode parentTTN = parent.getValue();
                    if (parentTTN != null && parentTTN.getPdfOutline() != null) {
                        pdfOutlineParent = parentTTN.getPdfOutline();
                    }
                }
            }

            PdfDestination here = new PdfDestination(PdfDestination.FIT);
            PdfOutline pdfTocEntry = new PdfOutline(pdfOutlineParent, here, fileLevelNP.getNavLabelText());
            if (npNode != null) {
                npNode.getValue().setPdfDestination(here);
                npNode.getValue().setPdfOutline(pdfTocEntry);
            }
        }
        XhtmlHandler.process(file.getCanonicalPath(), doc);
    }
    printlnerr();

    doc.close();
    System.err.println("PDF written to " + outputFile.getAbsolutePath());
    epubIn.cleanup();
}

From source file:com.anevis.jfreechartsamplespring.chart.ChartServiceImpl.java

private byte[] writeChartsToDocument(JFreeChart... charts) {
    try {//from  w  w  w .  j a v  a2 s. co m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        float width = PageSize.A4.getWidth();
        float height = PageSize.A4.getHeight() / 2;
        int index = 0;

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();

        for (JFreeChart chart : charts) {

            PdfTemplate template = contentByte.createTemplate(width, height);
            Graphics2D graphics2D = template.createGraphics(width, height);
            Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);

            chart.draw(graphics2D, rectangle2D);

            graphics2D.dispose();
            contentByte.addTemplate(template, 0, height - (height * index));
            index++;
        }

        writer.flush();
        document.close();

        return baos.toByteArray();
    } catch (DocumentException ex) {
        Logger.getLogger(ChartService.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

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

public void generarArchivoPdf(String xml) {
    try {// w  ww  . ja  v  a2  s  . co m
        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);//  w  w w . j  a v  a2s  .  c  om

    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 {/*www  .j  a va 2 s.c om*/
        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.aryjr.nheengatu.testes.AbsolutePosition.java

License:Open Source License

public static void main(final String[] args) {
    //      System.out.println("My First PdfPTable");
    final Document document = new Document();
    try {/*from  w  ww  . ja v a 2s.  c  o m*/
        final PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("/home/aryjr/MyFirstTable.pdf"));
        document.open();
        // PdfPTable table = createTable();
        // document.add(table);
        // table.writeSelectedRows(0, -1, 50, 200,
        // writer.getDirectContent());
        final PdfContentByte cb = writer.getDirectContent();
        cb.concatCTM(1f, 0f, 0f, -1f, 0f, 0f);
        // Paragraph text = new Paragraph("Ary Junior",
        // FontFactory.getFont(Style.DEFAULT_FONT_FAMILY,
        // Style.DEFAULT_FONT_SIZE, Font.NORMAL, Style.DEFAULT_FONT_COLOR));
        final BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showText("Ary Junior");
        cb.endText();
        Runtime.getRuntime().exec("acroread /home/aryjr/MyFirstTable.pdf");
    } catch (final DocumentException de) {
        System.err.println(de.getMessage());
    } catch (final IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:com.aurel.track.util.PdfUtils.java

License:Open Source License

public static void createPdfFromText(StringBuilder text, File pdfFile) {
    Document output = null;/*  www. j  a  v  a2 s  .  c  o  m*/
    try {
        BufferedReader input = new BufferedReader(new StringReader(text.toString()));
        // Size DIN A4
        //    see com.lowagie.text.PageSize for a complete list of page-size constants.
        output = new Document(PageSize.A4, 40, 40, 40, 40);
        float fntSize, lineSpacing;
        fntSize = 9f;
        lineSpacing = 11f;
        Font font1 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        Font font2 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        font2.setColor(Color.BLUE);
        Font font3 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        font3.setColor(Color.RED);

        PdfWriter.getInstance(output, new FileOutputStream(pdfFile));

        output.open();
        output.addAuthor("Steinbeis");
        output.addSubject("Debug Info");
        output.addTitle(pdfFile.getName());

        String line = "";
        while (null != (line = input.readLine())) {
            Font ft = font1;
            if (line.startsWith("%")) {
                ft = font2;
            }
            if (line.startsWith("% ^^^") || line.startsWith("% vvv")) {
                ft = font3;
            }
            Paragraph p = new Paragraph(lineSpacing, line, ft);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            output.add(p);
        }
        output.close();
        input.close();
    } catch (Exception e) {
        LOGGER.debug("Problem creating debug info pdf file", e);
    }
}