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:be.ugent.maf.cellmissy.gui.controller.analysis.doseresponse.generic.GenericDRResultsController.java

@Override
protected void createPdfFile(FileOutputStream outputStream) {
    document = null;//from  w w  w  .j  a va  2  s  . c o  m
    writer = null;
    try {
        // get new instances
        // the Document is the base layout element
        document = new Document();
        // the pdfWriter is actually creating the file
        writer = PdfWriter.getInstance(document, outputStream);
        //open document
        document.open();
        // add content to document
        addContent();
        //dispose resources
        document.close();
        document = null;
        writer.close();
        writer = null;
    } catch (DocumentException ex) {
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:be.ugent.maf.cellmissy.gui.controller.analysis.singlecell.AngleDirectController.java

/**
 * @param outputStream//w  w w .  j  a  v  a2 s.c  o m
 */
private void createPdfFile(FileOutputStream outputStream, JFreeChart chart) {
    document = null;
    writer = null;
    try {
        // get new instances
        // the Document is the base layout element
        document = new Document();
        // the pdfWriter is actually creating the file
        writer = PdfWriter.getInstance(document, outputStream);
        //open document
        document.open();
        // add content to document
        addChart(chart);
        //dispose resources
        document.close();
        document = null;
        writer.close();
        writer = null;
    } catch (DocumentException ex) {
        LOG.error(ex.getMessage(), ex);
    }
}

From source file:beans.ManagedBeanProducto.java

License:Open Source License

public String CreatePdf() throws IOException, DocumentException {
    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
    //System.out.println(" test: "+extContext);
    //System.out.println(" esta es la ruta" + extContext.getRealPath("//pdfs//"));
    // step 1//from w w  w  . j  a  v a 2 s . com
    String ruta_pdfs = extContext.getRealPath("//pdfs//");
    Document document = new Document(PageSize.A4);
    document.setMargins(5, 5, 25, 25);
    document.setMarginMirroring(true);

    // step 2
    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(ruta_pdfs + "//CB" + Producto.getIdProducto() + ".pdf"));
    // step 3
    document.open();
    // step 4
    PdfContentByte cb = writer.getDirectContent();

    Paragraph Titulo = new Paragraph(
            "PRODUCTO : " + Producto.getNombreProducto().substring(14).toUpperCase() + "\n\n");
    Titulo.setAlignment(Paragraph.ALIGN_CENTER);
    document.add(Titulo);

    // EAN 13
    // document.add(new Paragraph("Barcode EAN.UCC-13"));
    BarcodeEAN codeEAN = new BarcodeEAN();

    codeEAN.setCode(CodigoBarrasFinal());
    String nombre_producto = "";
    if (Producto.getNombreProducto().length() >= 41) {
        nombre_producto = Producto.getNombreProducto().substring(14, 41);
    } else {
        nombre_producto = Producto.getNombreProducto().substring(14);
    }
    // document.add(new Paragraph(nombre_producto,new Font(Font.COURIER, 5, Font.NORMAL)));
    // document.add(codeEAN.createImageWithBarcode(cb,Color.BLUE , Color.BLUE));
    // codeEAN.setGuardBars(false);

    // document.add(new Paragraph(nombre_producto,new Font(Font.COURIER, 5, Font.NORMAL)));
    // codeEAN.setGuardBars(false);
    Image imagen = codeEAN.createImageWithBarcode(cb, null, null);
    imagen.scaleAbsolute(87, 45);
    //document.add(imagen);

    PdfPTable table = new PdfPTable(5);
    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    // table.setTotalWidth(1800);
    PdfPCell cell;
    Phrase nombre = new Phrase(nombre_producto.toUpperCase(),
            new Font(Font.COURIER, 5, Font.BOLD, Color.BLACK));

    cell = new PdfPCell();
    cell.addElement(nombre);
    //cell.addElement(new Chunk("\n"));
    cell.addElement(imagen);
    //cell.addElement(new Chunk("\n"));

    table.addCell(cell);
    //table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);

    table.addCell(cell);
    //table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);

    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    table.addCell(cell);
    document.add(table);

    // EAN 8 "6987";
    // String inicio ="345";
    //  int intermedio =1000+Producto.getIdProducto();
    //  String fin ="0";
    // document.add(new Paragraph(Producto.getNombreProducto(),new Font(Font.COURIER, 4, Font.NORMAL)));
    // codeEAN.setCodeType(Barcode.EAN8);
    // codeEAN.setBarHeight(codeEAN.getSize() * 1.5f);
    // codeEAN.setCode(inicio.concat(intermedio+fin));
    // document.add(codeEAN.createImageWithBarcode(cb, null, null));
    document.close();

    return "codigo_barras_productos";
}

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

License:Open Source License

protected MemoryFileDTO generateReportFile(BaseReportDto reportData, String fileName) {
    Document document = new Document(PageSize.A4);
    MemoryFileDTO report = new MemoryFileDTO();
    report.setFileName(fileName);/*from   www  .java 2s  .  c o  m*/
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(this);
        writer.setFullCompression();
        document.open();
        generateReportBody(document, reportData);
        writer.flush();
        document.close();
        report.setFileData(baos.toByteArray());
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return null;
    }
    return report;
}

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  a  va 2s.c  o  m
        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  w w w  .  j ava2 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.PDFExporter.java

License:Open Source License

@Override
public void export(ResultSet resultSet, String label, OutputStream outputStream) throws ExportException {
    try {/*  w  w w.j a v  a 2 s  .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 ava2 s  . c  o  m*/
        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  .j a  v a2 s  . co  m
    * 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.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 {//  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";

}