Example usage for com.itextpdf.text.pdf PdfWriter getDirectContent

List of usage examples for com.itextpdf.text.pdf PdfWriter getDirectContent

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfWriter getDirectContent.

Prototype


public PdfContentByte getDirectContent() 

Source Link

Document

Use this method to get the direct content for this document.

Usage

From source file:utils.PrintInvoice.java

public void getDocument() {
    try {//from   w  w  w  .j  a  v  a  2  s  . c om
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("SaleBill#" + salebill.getId() + ".pdf"));

        document.open();
        ////////////////////////////////////////////////////////////////////////////////////
        ///////////////////Start Document Here/////////////////////////////////
        PdfContentByte directContent = writer.getDirectContent();
        Paragraph p1 = new Paragraph("SALE BILL");
        p1.setFont(FONT[4]);
        p1.setAlignment(Element.ALIGN_CENTER);

        document.add(p1);
        //show the company details here.
        Phrase company = new Phrase(new Chunk("BIO PHARMA\nAKOT 444101(M.S)", FONT[3]));
        document.add(company);
        document.add(new Phrase(
                "\nLicense No : 20B : AK-88888\n                     21B : AK-88889\n       Mobile : "
                        + SessionClass.getInstance().getMobileNumber(),
                FONT[2]));

        //            Phrase mobNum  = new Phrase("    Mobile : "+SessionClass.getInstance().getMobileNumber() );
        //            mobNum.setFont(FONT[2]);
        //            ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, mobNum, 35, 710, 0);

        System.out.println(dateFormatter.format(salebill.getBillDate()));
        //show the invoice details
        //  String txt = "Bill No. : " + salebill.getId()+"\nBill Date : " + dateFormatter.format(salebill.getBillDate()) +;
        Phrase invoiceDetails = new Phrase("Bill No. : " + salebill.getId());
        ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, invoiceDetails, 400, 693, 0);
        invoiceDetails = new Phrase("Bill Date : " + dateFormatter2.format(salebill.getBillDate()));
        ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, invoiceDetails, 400, 681, 0);
        invoiceDetails = new Phrase("Mode of Payment : " + salebill.getMode());
        ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, invoiceDetails, 400, 668, 0);

        //show the customer details
        Customer c = salebill.getCustomerId();
        Phrase custDetails = new Phrase("SOLD TO", FONT[3]);
        ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, custDetails, 35, 693, 0);
        custDetails = new Phrase(c.getCompanyName());
        ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, custDetails, 35, 681, 0);
        custDetails = new Phrase(c.getSiteAddress());
        ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, custDetails, 35, 668, 0);
        custDetails = new Phrase("Licence : " + c.getLicenceNo());
        ColumnText.showTextAligned(directContent, Element.ALIGN_LEFT, custDetails, 35, 655, 0);

        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);

        //Item Particulars are shown here
        PdfPTable table = new PdfPTable(7);
        table.setTotalWidth(new float[] { 175, 80, 80, 50, 50, 50, 75 });
        table.setHeaderRows(1);

        //headers
        table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell("Particulars");
        table.addCell("Batch");
        table.addCell("Expiry");
        table.addCell("MRP");
        table.addCell("Rate");
        table.addCell("Qnty");
        table.addCell("SubTotal");
        table.getDefaultCell().setBackgroundColor(null);
        table.setSpacingAfter(5.0f);

        List<SaleBillPharmaItem> items = salebill.getSaleBillPharmaItemList();
        for (int i = 0; i < items.size(); i++) {
            PdfPCell desc = new PdfPCell(new Phrase(items.get(i).getItemName()));

            table.addCell(desc);

            PdfPCell batch = new PdfPCell(new Phrase(items.get(i).getBatch()));

            table.addCell(batch);

            PdfPCell expiry = null;
            Date tDate = null;
            try {
                tDate = dateFormatter2.parse(items.get(i).getExpDate());
            } catch (ParseException ex) {
                Logger.getLogger(PrintInvoice.class.getName()).log(Level.SEVERE, null, ex);
            }

            expiry = new PdfPCell(new Phrase(dateFormatter.format(tDate)));

            table.addCell(expiry);

            PdfPCell mrp = new PdfPCell(new Phrase(items.get(i).getMrp() + ""));
            //                 //mrp.setBorderColor(BaseColor.WHITE);
            //                 mrp.setBorderColorLeft(BaseColor.BLACK);
            //                 mrp.setBorderColorRight(BaseColor.WHITE);
            table.addCell(mrp);
            PdfPCell rate = new PdfPCell(new Phrase(items.get(i).getItemRate() + ""));
            //                 //rate.setBorderColor(BaseColor.WHITE);
            //                 rate.setBorderColorLeft(BaseColor.BLACK);
            //                 rate.setBorderColorRight(BaseColor.WHITE);
            table.addCell(rate);
            PdfPCell quantity = new PdfPCell(new Phrase(items.get(i).getQnty() + ""));
            //                 //quantity.setBorderColor(BaseColor.WHITE);
            //                 quantity.setBorderColorLeft(BaseColor.BLACK);
            //                 quantity.setBorderColorRight(BaseColor.WHITE);
            table.addCell(quantity);
            PdfPCell subtotal = new PdfPCell(new Phrase(items.get(i).getAmt() + ""));
            //                 //subtotal.setBorderColor(BaseColor.WHITE);
            //                 subtotal.setBorderColorLeft(BaseColor.BLACK);
            //                 subtotal.setBorderColorRight(BaseColor.WHITE);
            table.addCell(subtotal);

        }

        //now show the sub details
        //PdfPCell finalCell = new PdfPCell(new Phrase("Total VAT Amt : Rs " + salebill.getTotalVat() + "                     Total Amount : Rs "));
        //Todo change code here to show vat amount when there is vat number
        PdfPCell finalCell = new PdfPCell(
                new Phrase("Total VAT Amt : Rs " + salebill.getTotalVat() + "           Total Amount : Rs "));
        finalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        finalCell.setColspan(6);
        table.addCell(finalCell);
        table.addCell("" + salebill.getTotalAmt());

        PdfPCell cdCell = new PdfPCell(new Phrase("Cash Discount (2 %) : (-) Rs"));
        cdCell.setColspan(6);
        cdCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cdCell);
        table.addCell("" + salebill.getDiscount());

        PdfPCell finalAmtCell = new PdfPCell(new Phrase("Final Amount : Rs"));
        finalAmtCell.setColspan(6);
        finalAmtCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(finalAmtCell);
        table.addCell("" + salebill.getFinalAmt());

        document.add(table);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);

        Paragraph sign = new Paragraph(new Chunk("Authorized signatory\n(BIO PHARMA)"));
        sign.setAlignment(Element.ALIGN_RIGHT);
        document.add(sign);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);

        document.add(Chunk.NEWLINE);

        Paragraph p = new Paragraph("THANK YOU FOR YOUR BUSINESS");
        p.setFont(FONT[4]);
        p.setAlignment(Element.ALIGN_CENTER);
        document.add(p);

        ///////////////////End Documnet here//////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////
        document.close(); // no need to close PDFwriter?

    } catch (DocumentException | FileNotFoundException e) {
        //LOGGER
        e.printStackTrace();
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Printing Error");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.setScene(new Scene(VBoxBuilder.create()
                .children(new Text(
                        "The file to be printed is already open \n. Please close the file and Print Again"))
                .alignment(Pos.CENTER).padding(new Insets(50)).build()));
        dialogStage.show();
    }

}

From source file:uy.gub.imm.sae.web.mbean.reserva.PasoFinalMBean.java

License:Open Source License

public String imprimirTicket() {
    try {// w w  w.ja  v a  2  s . c  o  m

        BaseColor colorBlack = new BaseColor(0, 0, 0);

        BaseFont times = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        BaseFont helveticaBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252,
                BaseFont.NOT_EMBEDDED);
        BaseFont symbol = BaseFont.createFont(BaseFont.SYMBOL, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        SimpleDateFormat sdfHr = new SimpleDateFormat("HH:mm");
        SimpleDateFormat sdfFecha = new SimpleDateFormat("dd/MM/yyyy");

        Rectangle pageSize = new Rectangle(210, 210);

        Document document = new Document(pageSize);
        document.addTitle(getI18N().getText("etiqueta.reserva.title"));

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
        document.open();

        PdfContentByte pdfContent = pdfWriter.getDirectContent();

        InputStream is = PasoFinalMBean.class.getResourceAsStream(SAEProfile.getInstance().getProperties()
                .getProperty(SAEProfile.PROFILE_UI_TEMPLATES_IMAGES_LOGO_TICKET_KEY));

        byte[] arrImage = new byte[4096];
        is.read(arrImage);

        Image img = Image.getInstance(arrImage);

        img.scaleAbsolute(100, 30);
        img.setAbsolutePosition(55, 170);
        document.add(img);

        //Dibujo primer lnea
        LineSeparator line = new LineSeparator();
        line.setAlignment(LineSeparator.ALIGN_CENTER);
        line.setLineColor(colorBlack);
        line.setLineWidth(0.5f);

        line.drawLine(pdfContent, 10, 200, 170);

        //Etiqueta RESERVA
        pdfContent.beginText();
        pdfContent.setFontAndSize(helveticaBold, 15);
        pdfContent.setTextMatrix(45, 150);
        pdfContent.showText(getI18N().getText("etiqueta.reserva.showText"));
        pdfContent.endText();

        //Fecha de la reserva
        String fecha_reserva = sdfFecha.format(sesionMBean.getDisponibilidad().getHoraInicio());

        pdfContent.beginText();
        pdfContent.setFontAndSize(symbol, 16);
        pdfContent.setTextMatrix(130, 150);
        pdfContent.showText(fecha_reserva);
        pdfContent.endText();

        //Dibujo segunda lnea
        line.drawLine(pdfWriter.getDirectContent(), 10, 200, 140);

        int etiqHoraTamanio = 25;
        int etiqHoraX = 15;
        int etiqHoraY = 85;

        int valorHoraTamanio = 40;
        int valorHoraX = 105;
        int valorHoraY = 80;

        String serie = sesionMBean.getRecurso().getSerie();
        boolean conSerie = (serie != null) && (serie.length() >= 1);

        if (sesionMBean.getRecurso().getMostrarNumeroEnTicket()) {

            if (!conSerie) {
                //Ajusto valor y etiqueta hora
                etiqHoraTamanio = 20;
                etiqHoraX = 15;
                etiqHoraY = 110;

                valorHoraTamanio = 30;
                valorHoraX = 120;
                valorHoraY = 107;

                //Etiqueta NUMERO
                pdfContent.beginText();
                pdfContent.setFontAndSize(helveticaBold, 20);
                pdfContent.setTextMatrix(15, 65);
                pdfContent.showText(getI18N().getText("etiqueta.numero.numero"));
                pdfContent.endText();

                //Numero de la reserva
                String nro = sesionMBean.getReservaConfirmada().getNumero().toString();
                int nro_pos = 135;

                if (nro.length() == 1) {
                    nro_pos = 135;
                } else if (nro.length() == 2) {
                    nro_pos = 125;
                } else {
                    nro_pos = 105;
                }

                pdfContent.beginText();
                pdfContent.setFontAndSize(symbol, 60);
                pdfContent.setTextMatrix(nro_pos, 55);
                pdfContent.showText(nro);
                pdfContent.endText();
            } else {
                //<<<<<< Agregado >>>>>>
                //Ajusto valor y etiqueta hora
                etiqHoraTamanio = 20;
                etiqHoraX = 15;
                etiqHoraY = 123;

                valorHoraTamanio = 20;
                valorHoraX = 120;
                valorHoraY = 122;

                //Etiqueta SERIE
                pdfContent.beginText();
                pdfContent.setFontAndSize(helveticaBold, 20);
                pdfContent.setTextMatrix(15, 87);
                pdfContent.showText(getI18N().getText("etiqueta.numero.serie"));
                pdfContent.endText();

                pdfContent.beginText();
                pdfContent.setFontAndSize(helveticaBold, 20);
                pdfContent.setTextMatrix(120, 87);
                pdfContent.showText(serie);
                pdfContent.endText();

                //Etiqueta NUMERO
                pdfContent.beginText();
                pdfContent.setFontAndSize(helveticaBold, 20);
                pdfContent.setTextMatrix(15, 50);
                pdfContent.showText(getI18N().getText("etiqueta.numero.numero"));
                pdfContent.endText();

                //Numero de la reserva
                String nro = sesionMBean.getReservaConfirmada().getNumero().toString();
                int nro_pos = 135;

                if (nro.length() == 1) {
                    nro_pos = 135;
                } else if (nro.length() == 2) {
                    nro_pos = 125;
                } else {
                    nro_pos = 105;
                }

                pdfContent.beginText();
                pdfContent.setFontAndSize(symbol, 40);
                pdfContent.setTextMatrix(nro_pos, 47);
                pdfContent.showText(nro);
                pdfContent.endText();
            }
        }

        //Etiqueta HORA
        pdfContent.beginText();
        pdfContent.setFontAndSize(helveticaBold, etiqHoraTamanio);
        pdfContent.setTextMatrix(etiqHoraX, etiqHoraY);
        pdfContent.showText(getI18N().getText("etiqueta.hora.hora"));
        pdfContent.endText();

        //Hora de la reserva
        pdfContent.beginText();
        pdfContent.setFontAndSize(symbol, valorHoraTamanio);
        pdfContent.setTextMatrix(valorHoraX, valorHoraY);
        pdfContent.showText(sdfHr.format(sesionMBean.getDisponibilidad().getHoraInicio()));
        pdfContent.endText();

        //Dibujo tercer lnea
        line.drawLine(pdfWriter.getDirectContent(), 10, 200, 45);

        String ticketEtiqUno = sesionMBean.getRecurso().getTextoRecurso().getTicketEtiquetaUno();
        String ticketEtiqDos = sesionMBean.getRecurso().getTextoRecurso().getTicketEtiquetaDos();
        int largoEtiqUno = 0;
        int largoEtiqDos = 0;
        int xValores = 0;

        if (ticketEtiqUno != null) {
            largoEtiqUno = ticketEtiqUno.length();
        }

        if (ticketEtiqDos != null) {
            largoEtiqDos = ticketEtiqDos.length();
        }

        if (largoEtiqUno > largoEtiqDos) {
            xValores = 8 * (largoEtiqUno + 1);
        } else {
            xValores = 8 * (largoEtiqDos + 1);
        }

        //Etiqueta uno
        if (ticketEtiqUno != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(helveticaBold, 10);
            pdfContent.setTextMatrix(10, 30);
            pdfContent.showText(ticketEtiqUno + ":");
            pdfContent.endText();
        }

        //Valor etiqueta uno
        String valorEtiqUno = sesionMBean.getRecurso().getTextoRecurso().getValorEtiquetaUno();
        if (valorEtiqUno != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(times, 10);
            pdfContent.setTextMatrix(xValores, 30);
            pdfContent.showText(valorEtiqUno);
            pdfContent.endText();
        }

        //Etiqueta dos

        if (ticketEtiqDos != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(helveticaBold, 10);
            pdfContent.setTextMatrix(10, 15);
            pdfContent.showText(ticketEtiqDos + ":");
            pdfContent.endText();
        }

        //Valor etiqueta dos
        String valorEtiqDos = sesionMBean.getRecurso().getTextoRecurso().getValorEtiquetaDos();
        if (valorEtiqDos != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(times, 10);
            pdfContent.setTextMatrix(xValores, 15);
            pdfContent.showText(valorEtiqDos);
            pdfContent.endText();
        }

        pdfWriter.addJavaScript("this.print({bUI: true, bSilent: true, bShrinkToFit: true});", false);
        pdfWriter.addJavaScript("this.closeDoc(true);");

        document.close();

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.setContentType("application/pdf");

        os.writeTo(response.getOutputStream());

        response.getOutputStream().flush();
        response.getOutputStream().close();
        facesContext.responseComplete();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:uy.gub.imm.sae.web.mbean.reserva.PasoFinalMBean.java

License:Open Source License

public String guardarTicket() {
    try {//from www. j a v  a  2s.com

        BaseColor colorBlack = new BaseColor(0, 0, 0);

        BaseFont times = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        BaseFont helveticaBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252,
                BaseFont.NOT_EMBEDDED);
        BaseFont symbol = BaseFont.createFont(BaseFont.SYMBOL, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        SimpleDateFormat sdfHr = new SimpleDateFormat("HH:mm");
        SimpleDateFormat sdfFecha = new SimpleDateFormat("dd/MM/yyyy");

        Rectangle pageSize = new Rectangle(210, 210);

        Document document = new Document(pageSize);
        document.addTitle(getI18N().getText("etiqueta.reserva.title"));

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
        document.open();

        PdfContentByte pdfContent = pdfWriter.getDirectContent();

        InputStream is = PasoFinalMBean.class.getResourceAsStream(SAEProfile.getInstance().getProperties()
                .getProperty(SAEProfile.PROFILE_UI_TEMPLATES_IMAGES_LOGO_TICKET_KEY));

        byte[] arrImage = new byte[4096];
        is.read(arrImage);

        Image img = Image.getInstance(arrImage);

        img.scaleAbsolute(100, 30);
        img.setAbsolutePosition(55, 170);
        document.add(img);

        //Dibujo primer lnea
        LineSeparator line = new LineSeparator();
        line.setAlignment(LineSeparator.ALIGN_CENTER);
        line.setLineColor(colorBlack);
        line.setLineWidth(0.5f);

        line.drawLine(pdfContent, 10, 200, 170);

        //Etiqueta RESERVA
        pdfContent.beginText();
        pdfContent.setFontAndSize(helveticaBold, 15);
        pdfContent.setTextMatrix(45, 150);
        pdfContent.showText(getI18N().getText("etiqueta.reserva.showText"));
        pdfContent.endText();

        //Fecha de la reserva
        String fecha_reserva = sdfFecha.format(sesionMBean.getDisponibilidad().getHoraInicio());

        pdfContent.beginText();
        pdfContent.setFontAndSize(symbol, 16);
        pdfContent.setTextMatrix(130, 150);
        pdfContent.showText(fecha_reserva);
        pdfContent.endText();

        //Dibujo segunda lnea
        line.drawLine(pdfWriter.getDirectContent(), 10, 200, 140);

        int etiqHoraTamanio = 25;
        int etiqHoraX = 15;
        int etiqHoraY = 85;

        int valorHoraTamanio = 40;
        int valorHoraX = 105;
        int valorHoraY = 80;

        String serie = sesionMBean.getRecurso().getSerie();
        boolean conSerie = (serie != null) && (serie.length() >= 1);

        if (sesionMBean.getRecurso().getMostrarNumeroEnTicket()) {

            if (!conSerie) {
                //Ajusto valor y etiqueta hora
                etiqHoraTamanio = 20;
                etiqHoraX = 15;
                etiqHoraY = 110;

                valorHoraTamanio = 30;
                valorHoraX = 120;
                valorHoraY = 107;

                //Etiqueta NUMERO
                pdfContent.beginText();
                pdfContent.setFontAndSize(helveticaBold, 20);
                pdfContent.setTextMatrix(15, 65);
                pdfContent.showText(getI18N().getText("etiqueta.numero.numero"));
                pdfContent.endText();

                //Numero de la reserva
                String nro = sesionMBean.getReservaConfirmada().getNumero().toString();
                int nro_pos = 135;

                if (nro.length() == 1) {
                    nro_pos = 135;
                } else if (nro.length() == 2) {
                    nro_pos = 125;
                } else {
                    nro_pos = 105;
                }

                pdfContent.beginText();
                pdfContent.setFontAndSize(symbol, 60);
                pdfContent.setTextMatrix(nro_pos, 55);
                pdfContent.showText(nro);
                pdfContent.endText();
            } else {

                //Ajusto valor y etiqueta hora
                etiqHoraTamanio = 20;
                etiqHoraX = 15;
                etiqHoraY = 123;

                valorHoraTamanio = 20;
                valorHoraX = 120;
                valorHoraY = 122;

                //Etiqueta SERIE
                pdfContent.beginText();
                pdfContent.setFontAndSize(helveticaBold, 20);
                pdfContent.setTextMatrix(15, 87);
                pdfContent.showText(getI18N().getText("etiqueta.numero.serie"));
                pdfContent.endText();

                pdfContent.beginText();
                pdfContent.setFontAndSize(symbol, 20);
                pdfContent.setTextMatrix(120, 87);
                pdfContent.showText(serie);
                pdfContent.endText();

                //Etiqueta NUMERO
                pdfContent.beginText();
                pdfContent.setFontAndSize(helveticaBold, 20);
                pdfContent.setTextMatrix(15, 50);
                pdfContent.showText(getI18N().getText("etiqueta.numero.numero"));
                pdfContent.endText();

                //Numero de la reserva
                String nro = sesionMBean.getReservaConfirmada().getNumero().toString();
                int nro_pos = 135;

                if (nro.length() == 1) {
                    nro_pos = 135;
                } else if (nro.length() == 2) {
                    nro_pos = 125;
                } else {
                    nro_pos = 105;
                }

                pdfContent.beginText();
                pdfContent.setFontAndSize(symbol, 40);
                pdfContent.setTextMatrix(nro_pos, 47);
                pdfContent.showText(nro);
                pdfContent.endText();
            }
        }

        //Etiqueta HORA
        pdfContent.beginText();
        pdfContent.setFontAndSize(helveticaBold, etiqHoraTamanio);
        pdfContent.setTextMatrix(etiqHoraX, etiqHoraY);
        pdfContent.showText(getI18N().getText("etiqueta.hora.hora"));
        pdfContent.endText();

        //Hora de la reserva
        pdfContent.beginText();
        pdfContent.setFontAndSize(symbol, valorHoraTamanio);
        pdfContent.setTextMatrix(valorHoraX, valorHoraY);
        pdfContent.showText(sdfHr.format(sesionMBean.getDisponibilidad().getHoraInicio()));
        pdfContent.endText();

        //Dibujo tercer lnea
        line.drawLine(pdfWriter.getDirectContent(), 10, 200, 45);

        String ticketEtiqUno = sesionMBean.getRecurso().getTextoRecurso().getTicketEtiquetaUno();
        String ticketEtiqDos = sesionMBean.getRecurso().getTextoRecurso().getTicketEtiquetaDos();
        int largoEtiqUno = 0;
        int largoEtiqDos = 0;
        int xValores = 0;

        if (ticketEtiqUno != null) {
            largoEtiqUno = ticketEtiqUno.length();
        }

        if (ticketEtiqDos != null) {
            largoEtiqDos = ticketEtiqDos.length();
        }

        if (largoEtiqUno > largoEtiqDos) {
            xValores = 8 * (largoEtiqUno + 1);
        } else {
            xValores = 8 * (largoEtiqDos + 1);
        }

        //Etiqueta uno
        if (ticketEtiqUno != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(helveticaBold, 10);
            pdfContent.setTextMatrix(10, 30);
            pdfContent.showText(ticketEtiqUno + ":");
            pdfContent.endText();
        }

        //Valor etiqueta uno
        String valorEtiqUno = sesionMBean.getRecurso().getTextoRecurso().getValorEtiquetaUno();
        if (valorEtiqUno != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(times, 10);
            pdfContent.setTextMatrix(xValores, 30);
            pdfContent.showText(valorEtiqUno);
            pdfContent.endText();
        }

        //Etiqueta dos
        if (ticketEtiqDos != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(helveticaBold, 10);
            pdfContent.setTextMatrix(10, 15);
            pdfContent.showText(ticketEtiqDos + ":");
            pdfContent.endText();
        }

        //Valor etiqueta dos
        String valorEtiqDos = sesionMBean.getRecurso().getTextoRecurso().getValorEtiquetaDos();
        if (valorEtiqDos != null) {
            pdfContent.beginText();
            pdfContent.setFontAndSize(times, 10);
            pdfContent.setTextMatrix(xValores, 15);
            pdfContent.showText(valorEtiqDos);
            pdfContent.endText();
        }

        document.close();

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachment; filename=" + "Ticket de confirmacin.pdf");

        os.writeTo(response.getOutputStream());

        response.getOutputStream().flush();
        response.getOutputStream().close();
        facesContext.responseComplete();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:valstreamtools.ValStrSplitPage.java

private void splitDocument(String[] args) throws IOException, DocumentException {
    /**//  w w  w .  j a v  a2  s. co m
     * int splitNo = 4; String inputFileName =
     * "C:\\tmp\\valuestream_export_input_2015-03-03-22-18-37.pdf"; String
     * outputFileName = "C:\\tmp\\valuestream_export_op_2015-03-03-22-18-37.pdf";*
     */
    int splitNo = Integer.parseInt(args[0]);
    String inputFileName = args[1];
    if (splitNo > 1) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String splitOutFileName = "/var/tmp/PDF_SPLIT_" + sdf.format(new Date()) + ".pdf";
        PdfReader reader = new PdfReader(inputFileName);
        Rectangle pagesize = reader.getPageSize(1);
        float pageHeight = pagesize.getHeight();
        float newpagewidth = (pagesize.getWidth() / splitNo);
        Rectangle newPapeSize = new Rectangle(0, 0, newpagewidth, pageHeight);
        Document document = new Document(newPapeSize);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(splitOutFileName));
        document.open();
        int pageNos = reader.getNumberOfPages();
        PdfContentByte content = writer.getDirectContent();
        for (int i = 1; i <= pageNos; i++) {
            PdfImportedPage page = writer.getImportedPage(reader, i);
            if (i > 1) {// In case of a new inpit page, need to create this.
                document.newPage();
            }
            for (int j = 0; j < splitNo; j++) {
                if (j == 0) {//This condition is used to skip the adding of new page.
                    content.addTemplate(page, 0, 0);
                } else {
                    document.newPage();
                    content.addTemplate(page, (-1 * j * newpagewidth), 0);
                }
            }
        }
        document.close();
        reader.close();
        createPageNo(splitOutFileName, args[2], pageHeight);
        File f = new File(splitOutFileName);
        if (f.exists()) {
            f.delete();
        }
    } else {
        PdfReader reader = new PdfReader(inputFileName);
        float pageHeight = reader.getPageSize(1).getHeight();
        reader.close();
        createPageNo(inputFileName, args[2], pageHeight);
    }
}

From source file:WeeklyReport.Sections.Bookings.java

public Image bookingsByPODImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Bookings().bookingsByPODChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();//from ww w .ja  v a 2  s  . c o  m

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Bookings.java

public Image bookingsByTradelaneImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Bookings().bookingsByTradelaneChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();//from   w ww  .ja v  a  2  s  .c o m

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Commodities.java

public Image commodityChartImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = commodityChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(525f, 475f);
    Graphics2D graphics2d = template.createGraphics(525f, 475f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(0, 0, 525f, 475f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();/*from   w  w w  .ja v  a 2  s  .  c  om*/

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Declines.java

public Image declinesByReasonImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = declinesByReasonChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();/*from  w ww  . j  a  va 2s.co  m*/

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Declines.java

public Image declinesByCommodityImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Declines().declinesByCommodityClassChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createAppearance(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();/*from   w  w  w  .j  a  va 2s .co m*/

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.WeeklyPDF.java

public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
    PdfWriter writer = null;
    Document document = new Document();
    try {/*  w w  w.ja  va2  s  .  co  m*/
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        document.add(new Paragraph("Here is the recommendation"));
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(5, -100, width, height);
        chart.draw(graphics2d, rectangle2d);
        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);
    } catch (FileNotFoundException | DocumentException ex) {
        ex.getMessage();
    }
    document.close();
}