Example usage for com.itextpdf.text BaseColor BaseColor

List of usage examples for com.itextpdf.text BaseColor BaseColor

Introduction

In this page you can find the example usage for com.itextpdf.text BaseColor BaseColor.

Prototype

public BaseColor(final float red, final float green, final float blue) 

Source Link

Document

Construct a BaseColor with float values.

Usage

From source file:Tables.Printer.java

public void staffSalaryPayments(TableView supIssTab, TableColumn name, TableColumn monthlySalary,
        TableColumn salaryPaid, TableColumn payDate, long total) {
    String fileName = "staffPayment";
    String title = "Staff Payment History";

    try {// ww  w . j av  a2  s.  co  m
        //            Image im = Image.getInstance("src/njeiaccount/njei_img/accountcreation-icon.png");
        //            im.scaleAbsolute(150f, 150f);
        //            im.setAbsolutePosition(25,50);
        //            im.setBackgroundColor(BaseColor.PINK);

        dNow = new Date();
        String app = this.datForFile.format(dNow);
        String file = "\\Documents\\NjieAB_receipts\\staff\\pay " + app + ".pdf";

        System.out.println("file is " + file);
        fs = new FileOutputStream(System.getProperties().getProperty("user.home") + file); //this is for windows. since am developing on linux, i will use the next line. But for production version, i will enable the first optionand comment the second option as most users will be on the windows system. Or, will detect the OS and decide where to keept the files. Better idea
        //fs = new FileOutputStream(System.getProperties().getProperty("user.home")+"root/Desktop/reciept.pdf");

        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, fs);
        //specify column widths    
        float[] columnWidths = { 6f, 4f, 4f, 4f };
        PdfPTable table = new PdfPTable(columnWidths);

        //special font sizes    
        Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(FontFamily.TIMES_ROMAN, 10);
        // set table width a percentage of the page width    
        table.setWidthPercentage(90f);

        PdfPCell cell = new PdfPCell(new Phrase("\nRECIEPT FOR "));
        cell.setColspan(6);
        cell.setHorizontalAlignment(2);
        doc.open();
        //doc.add(im);

        header(doc, title);

        doc.add(cell);
        System.out.println(doc.leftMargin());

        //insert column headings    

        insertTableHeading(table, "Staff Name ", Element.ALIGN_MIDDLE, 1, bfBold12);
        insertTableHeading(table, "Monthly Salary ", Element.ALIGN_MIDDLE, 1, bfBold12);
        insertTableHeading(table, "Salary Paid", Element.ALIGN_MIDDLE, 1, bfBold12);
        insertTableHeading(table, "Payment Date", Element.ALIGN_MIDDLE, 1, bfBold12);

        table.setHeaderRows(1);

        /*Now i have to fill the table  */

        for (int i = 0; i < supIssTab.getItems().size(); i++) {
            insertCell(table, name.getCellData(i).toString(), Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, monthlySalary.getCellData(i).toString(), Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, salaryPaid.getCellData(i).toString(), Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, payDate.getCellData(i).toString(), Element.ALIGN_LEFT, 1, bf12);

        }

        insertCell(table, "", Element.ALIGN_RIGHT, 1, bfBold12);
        insertCell(table, "Total ", Element.ALIGN_RIGHT, 1, bfBold12);
        insertCell(table, String.valueOf(total), Element.ALIGN_LEFT, 1, bfBold12);
        doc.add(table);

        footer(doc); // this is rgw footer of the page

        doc.addCreationDate();
        doc.close();
        System.out.println("finished the printing job");

        try {
            Desktop.getDesktop().open(new File(System.getProperties().getProperty("user.home") + file));
        } catch (IOException iOException) {
            System.out.println("failed opening the file");
        }
    } catch (Exception sd) {
        Dialogs.create().title("Printer Error").masthead("Errro In Printing File").message(
                sd.getMessage() + " : " + sd.getCause() + " Caused By " + sd.getCause() + "--" + sd.toString())
                .showError();
    } finally {
    }

}

From source file:Tables.Printer.java

public void supCust_PayDebt(TableView supPayTab, TableColumn name, TableColumn debtPayment,
        TableColumn paymtDate, long total, String title, String fileName) {

    try {/*from   w  w  w.j  a  va 2  s .  co m*/
        //            Image im = Image.getInstance("src/njeiaccount/njei_img/accountcreation-icon.png");
        //            im.scaleAbsolute(150f, 150f);
        //            im.setAbsolutePosition(25,50);
        //            im.setBackgroundColor(BaseColor.PINK);

        dNow = new Date();
        String app = this.datForFile.format(dNow);

        String file = "\\Documents\\NjieAB_receipts\\default" + app + ".pdf";
        if (fileName == "SupplierDebtPayment") {
            file = "\\Documents\\NjieAB_receipts\\supplier\\pay " + app + ".pdf";
        } else if (fileName == "CustumerDebtPayment") {
            file = "\\Documents\\NjieAB_receipts\\custumer\\pay " + app + ".pdf";
        } else if (fileName == "StaffBalSalaryt") {
            file = "\\Documents\\NjieAB_receipts\\staff\\balance " + app + ".pdf";
        }

        System.out.println("file is " + file);
        fs = new FileOutputStream(System.getProperties().getProperty("user.home") + file); //this is for windows. since am developing on linux, i will use the next line. But for production version, i will enable the first optionand comment the second option as most users will be on the windows system. Or, will detect the OS and decide where to keept the files. Better idea
        //fs = new FileOutputStream(System.getProperties().getProperty("user.home")+"root/Desktop/reciept.pdf");

        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, fs);
        //specify column widths    
        float[] columnWidths = { 4f, 5f, 3f };
        PdfPTable table = new PdfPTable(columnWidths);

        //special font sizes    
        Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);
        // set table width a percentage of the page width    
        table.setWidthPercentage(90f);

        PdfPCell cell = new PdfPCell(new Phrase("\nRECIEPT FOR "));
        cell.setColspan(6);
        cell.setHorizontalAlignment(2);
        doc.open();
        //doc.add(im);

        header(doc, title);

        doc.add(cell);
        System.out.println(doc.leftMargin());

        //insert column headings
        if (fileName == "SupplierDebtPayment") {
            insertTableHeading(table, "Supplier Name ", Element.ALIGN_MIDDLE, 1, bfBold12);
            insertTableHeading(table, "Amount Owing", Element.ALIGN_MIDDLE, 1, bfBold12);
            insertTableHeading(table, "Date Issued", Element.ALIGN_MIDDLE, 1, bfBold12);
        } else if (fileName == "CustumerDebtPayment") {
            insertTableHeading(table, "Custumer Name ", Element.ALIGN_MIDDLE, 1, bfBold12);
            insertTableHeading(table, "Amount Owing", Element.ALIGN_MIDDLE, 1, bfBold12);
            insertTableHeading(table, "Date Issued", Element.ALIGN_MIDDLE, 1, bfBold12);
        } else if (fileName == "StaffBalSalaryt") {
            insertTableHeading(table, "Staff Name ", Element.ALIGN_MIDDLE, 1, bfBold12);
            insertTableHeading(table, "Branch", Element.ALIGN_MIDDLE, 1, bfBold12);
            insertTableHeading(table, "Balance Amount", Element.ALIGN_MIDDLE, 1, bfBold12);
        }
        table.setHeaderRows(1);

        /*Now i have to fill the table  */

        for (int i = 0; i < supPayTab.getItems().size(); i++) {
            insertCell(table, name.getCellData(i).toString(), Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, debtPayment.getCellData(i).toString(), Element.ALIGN_LEFT, 1, bf12);
            insertCell(table, paymtDate.getCellData(i).toString(), Element.ALIGN_LEFT, 1, bf12);
        }

        insertCell(table, "Total ", Element.ALIGN_RIGHT, 1, bfBold12);
        insertCell(table, String.valueOf(total), Element.ALIGN_LEFT, 2, bfBold12);
        insertCell(table, "", Element.ALIGN_RIGHT, 3, bfBold12);
        doc.add(table);

        footer(doc); // this is rgw footer of the page

        doc.addCreationDate();
        doc.close();
        System.out.println("finished the printing job");

        //Desktop.getDesktop().open(new File(System.getProperties().getProperty("user.home")+"root/Desktop/reciept.pdf"));
        try {
            Desktop.getDesktop().open(new File(System.getProperties().getProperty("user.home") + file));
        } catch (IOException iOException) {
            System.out.println("failed opening the file");
        }
    } catch (Exception sd) {
        Dialogs.create().title("Printer Error").masthead("Errro In Printing File").message(
                sd.getMessage() + " : " + sd.getCause() + " Caused By " + sd.getCause() + "--" + sd.toString())
                .showError();
    } finally {
    }

}

From source file:Tables.PrinterClass.java

public PrinterClass() {
    /****default values for the printer */
    System.out.println("instantiating the printer class");

    companyName = "Unity Systems";
    companyLocation = "Malingo, HIAMS building, Buea";
    companyTel = "+237 679574561";
    companyPoBox = "PB 1001, Buea";

    smallFooter = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.ITALIC, new BaseColor(157, 175, 157));
    this.companyNameFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD, new BaseColor(0, 0, 128));
    this.companySubInfoFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD, new BaseColor(51, 102, 13));
    this.tHeadingFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, new BaseColor(51, 51, 0));
    this.tBodyFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(51, 51, 51));

    this.saveDir = "\\";

    dateOnly = new SimpleDateFormat("yyyy-MM-dd");
    timeOnly = new SimpleDateFormat("hh:mm:ss");
    dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    datForFile = new SimpleDateFormat("yyyy-MM-dd __ hh mm ss");
}

From source file:util.ImageExample.java

public ImageExample(Calendar date, int settings, ArrayList<String> courses, String time, String eventAddress,
        int totalPrice, String comments, boolean kunde) throws Exception {
    this.date = date;
    this.settings = settings;
    this.courses = courses;
    this.time = time;
    this.eventAddress = eventAddress;
    this.totalPrice = totalPrice;
    this.comments = comments;
    this.fileName = "";
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.getAllFonts();//from ww w . j a v  a 2 s. c o m
    FontFactory.register("C:/Windows/Fonts/ARLRDBD.TTF", "Arial Rounded");
    Font font = FontFactory.getFont("Arial Rounded", 22, Font.NORMAL, new BaseColor(51, 102, 102));
    Document document = new Document();

    System.out.println("Pdf creation startet");
    try {
        if (kunde) {
            fileName = "grill" + new SimpleDateFormat("dd. MMMMM yyyy hhmm").format(date.getTime()) + ".pdf";
        } else {
            fileName = "grill" + new SimpleDateFormat("dd. MMMMM yyyy hhmm").format(date.getTime())
                    + "Prisliste.pdf";
        }

        FileOutputStream file = new FileOutputStream(new File("C:/Users/Mark/Desktop", fileName));
        PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();

        Image img = Image.getInstance("src/pictures/cirkles.png");
        img.scaleToFit(277, 277);
        img.setAbsolutePosition(40, PageSize.A4.getHeight() - img.getHeight());
        document.add(img);
        Chunk chunk = new Chunk("Grillmester 'Frankie'", font);
        chunk.setCharacterSpacing(3);
        Paragraph header = new Paragraph(chunk);
        header.setAlignment(Element.ALIGN_RIGHT);
        header.setSpacingBefore(15);
        document.add(header);
        Paragraph title = new Paragraph(
                "Tilbud angende d. " + new SimpleDateFormat("dd. MMMMM yyyy").format(date.getTime()) + ".",
                new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD));
        title.setAlignment(Element.ALIGN_LEFT);
        title.setIndentationLeft(235);
        title.setSpacingBefore(100);
        title.setLeading(17);
        document.add(title);
        Paragraph subtitle = new Paragraph("(Arrangement til " + settings + " personer).",
                new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.ITALIC));
        subtitle.setAlignment(Element.ALIGN_LEFT);
        subtitle.setIndentationLeft(235);
        subtitle.setLeading(17);
        document.add(subtitle);
        Paragraph body = new Paragraph("\n\nDer er aftalt:\n\n", new Font(Font.FontFamily.TIMES_ROMAN, 18));
        body.setAlignment(Element.ALIGN_LEFT);
        body.setIndentationLeft(235);
        body.setLeading(17);
        for (String course : courses) {
            body.add(course + "\n\n");
        }
        body.add("\nServeres klokken " + time + " i " + eventAddress + ".");
        document.add(body);
        Paragraph ending = new Paragraph("\n\n\nPris: " + totalPrice + ",-kr.",
                new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));
        ending.setAlignment(Element.ALIGN_LEFT);
        ending.setIndentationLeft(235);
        ending.setLeading(17);
        document.add(ending);
        if (!comments.equals("null")) {
            if (!comments.equals("")) {
                Paragraph comment = new Paragraph("\n\nKommentarer\n" + comments,
                        new Font(Font.FontFamily.TIMES_ROMAN, 18));
                comment.setAlignment(Element.ALIGN_LEFT);
                comment.setIndentationLeft(235);
                comment.setLeading(17);
                document.add(comment);
            }
        }

        Image footerImg = Image.getInstance("src/pictures/grillmester.png");
        footerImg.scaleToFit(210, 210);
        footerImg.setAbsolutePosition(40, 40);
        document.add(footerImg);

        Image img2 = Image.getInstance("src/pictures/Contact.png");
        img2.scaleToFit(250, 250);
        img2.setAbsolutePosition(20, PageSize.A4.getHeight() - img.getHeight() - 250);
        document.add(img2);

        document.close();

        System.out.println("Pdf finish");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getLocalizedMessage());
    }
}

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

License:Open Source License

public String imprimirTicket() {
    try {//from  ww  w . j a  va2  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  av 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 {

                //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:voedselbank.OverzichtScherm.java

private void exporteerKnopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exporteerKnopActionPerformed

    try {//from  w  w  w.j a v a  2 s .c  om
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Kies een locatie");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            Document doc = new Document();

            Rectangle rect = new Rectangle(PageSize.A4.rotate());
            PdfWriter.getInstance(doc, new FileOutputStream(chooser.getSelectedFile() + "/overzicht.pdf"));

            doc.setPageSize(rect);
            doc.open();

            PdfPTable pdfTable = new PdfPTable(overzichtTabel.getColumnCount());
            pdfTable.setWidthPercentage(108);
            pdfTable.setHorizontalAlignment(Element.ALIGN_CENTER);

            //adding table headers
            for (int i = 0; i < overzichtTabel.getColumnCount(); i++) {
                PdfPCell head = new PdfPCell(new Paragraph(overzichtTabel.getColumnName(i)));
                head.setHorizontalAlignment(Element.ALIGN_CENTER);
                head.setBackgroundColor(new BaseColor(236, 116, 4));
                pdfTable.addCell(head);
            }

            //extracting data from the JTable and inserting it to PdfPTable
            for (int rows = 0; rows < overzichtTabel.getRowCount(); rows++) {
                for (int cols = 0; cols < overzichtTabel.getColumnCount(); cols++) {
                    pdfTable.addCell(overzichtTabel.getModel().getValueAt(rows, cols).toString());
                }
            }

            doc.add(pdfTable);
            doc.close();
            System.out.println("Done");
        } else {
            System.out.println("U heeft niks geselecteerd.");
        }
    } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
    }
}