Example usage for com.itextpdf.text Image getInstance

List of usage examples for com.itextpdf.text Image getInstance

Introduction

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

Prototype

public static Image getInstance(final Image image) 

Source Link

Document

gets an instance of an Image

Usage

From source file:biblioteca.reportes.PdfCreator.java

License:Open Source License

/**
 * createPdf es una funcin estatica que funciona como plantilla para generar
 * reportes dinamicos, segn la necesidad del usuario.
 * @param path La direccin del archivo donde se guardar el pdf
 * @param titulo El Ttulo que llevar el pdf
 * @param encabezado Un texto que se mostrar bajo el ttulo
 * @param tabla La tabla de resultados que mostrar el pdf
 *///  w w  w .  j a  va2  s .co m
static public void createPdf(String path, String titulo, String encabezado, PdfPTable tabla) {
    Document document = new Document();
    // step 2
    try {
        PdfWriter.getInstance(document, new FileOutputStream(path));
        Font myFontTitle = new Font();
        myFontTitle.setFamily("Arial");
        myFontTitle.setStyle(Font.BOLD);
        myFontTitle.setSize(14);
        Font Univallef = new Font();
        Univallef.setColor(BaseColor.RED);
        Univallef.setFamily("Arial");
        Univallef.setSize(18);
        Image header = Image
                .getInstance(PdfCreator.class.getResource("/biblioteca/gui/resources/minilogo.png"));
        header.setAlignment(Image.ALIGN_CENTER);
        header.scaleToFit(50, 75);
        Paragraph Univalle = new Paragraph("Universidad del Valle", Univallef);
        Univalle.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph pTitulo = new Paragraph(titulo, myFontTitle);
        pTitulo.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph biblioteca = new Paragraph("Biblioteca Digital EISC", myFontTitle);
        biblioteca.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph developers = new Paragraph(
                "Desarrollado por:\n Mara Cristina Bustos \n Alejandro Valds Villada", myFontTitle);
        developers.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph Fecha = new Paragraph("Fecha y Hora del Reporte: " + fecha, myFontTitle);
        document.open();
        // step 4
        document.add(header);
        document.add(new Paragraph("\r\n"));
        document.add(Univalle);
        document.add(new Paragraph("\r\n"));
        document.add(pTitulo);
        document.add(new Paragraph("\r\n"));
        document.add(biblioteca);
        document.add(new Paragraph("\r\n"));
        document.add(developers);
        document.add(new Paragraph("\r\n"));
        document.add(Fecha);
        document.add(new Paragraph("\r\n"));
        document.add(new Paragraph(encabezado + (tabla.getRows().size() - 1)));
        document.add(new Paragraph("\r\n"));
        document.add(tabla);
        // step 5
        document.close();
    } catch (DocumentException de) {
        System.err.println(de);
    } catch (IOException ioex) {
        System.err.println(ioex);
    }
}

From source file:biblioteca.reportes.PdfCreator.java

License:Open Source License

static public void createArrayListPdf(String path, String titulo, String encabezado,
        ArrayList<PdfPTable> tablas) {
    Document document = new Document();
    ArrayList<String> NombreTablas = new DaoReportesEstadisticas().getNombreTablas();
    //step 2//ww w  .j a v  a  2 s.  c  o m
    try {
        PdfWriter.getInstance(document, new FileOutputStream(path));
        Font myFontTitle = new Font();
        myFontTitle.setFamily("Arial");
        myFontTitle.setStyle(Font.BOLD);
        myFontTitle.setSize(12);
        Font Univallef = new Font();
        Univallef.setColor(BaseColor.RED);
        Univallef.setFamily("Arial");
        Univallef.setSize(18);
        Image header = Image
                .getInstance(PdfCreator.class.getResource("/biblioteca/gui/resources/minilogo.png"));
        header.setAlignment(Image.ALIGN_CENTER);
        header.scaleToFit(50, 75);
        Paragraph Univalle = new Paragraph("Universidad del Valle", Univallef);
        Univalle.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph pTitulo = new Paragraph(titulo, myFontTitle);
        pTitulo.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph biblioteca = new Paragraph("Biblioteca Digital EISC", myFontTitle);
        biblioteca.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph developers = new Paragraph(
                "Desarrollado por:\n Mara Cristina Bustos \n Alejandro Valds Villada", myFontTitle);
        developers.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph Fecha = new Paragraph("Fecha y Hora de Reporte: " + fecha, myFontTitle);
        document.open();
        // step 4
        document.add(header);
        document.add(new Paragraph("\r\n"));
        document.add(Univalle);
        document.add(new Paragraph("\r\n"));
        document.add(pTitulo);
        document.add(new Paragraph("\r\n"));
        document.add(biblioteca);
        document.add(new Paragraph("\r\n"));
        document.add(developers);
        document.add(new Paragraph("\r\n"));
        document.add(Fecha);
        document.add(new Paragraph("\r\n"));
        for (int i = 0; i < tablas.size(); i++) {
            document.add(new Paragraph(NombreTablas.get(i)));
            document.add(new Paragraph(encabezado + (tablas.get(i).getRows().size() - 1)));
            document.add(new Paragraph("\r\n"));
            document.add(tablas.get(i));
        }
        // step 5
        document.close();
    } catch (DocumentException de) {
        System.err.println(de);
    } catch (IOException ioex) {
        System.err.println(ioex);
    }
}

From source file:biblioteca.reportes.PdfCreator.java

License:Open Source License

static public void createDinamicPdf(String path, String titulo, String encabezado,
        ArrayList<Element> contenido) {
    Document document = new Document();
    //step 2//from  www. j  a va 2  s .  c o  m
    try {
        PdfWriter.getInstance(document, new FileOutputStream(path));
        Font myFontTitle = new Font();
        myFontTitle.setFamily("Arial");
        myFontTitle.setStyle(Font.BOLD);
        myFontTitle.setSize(12);
        Font Univallef = new Font();
        Univallef.setColor(BaseColor.RED);
        Univallef.setFamily("Arial");
        Univallef.setSize(18);
        Image header = Image
                .getInstance(PdfCreator.class.getResource("/biblioteca/gui/resources/minilogo.png"));
        header.setAlignment(Image.ALIGN_CENTER);
        header.scaleToFit(50, 75);
        Paragraph Univalle = new Paragraph("Universidad del Valle", Univallef);
        Univalle.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph pTitulo = new Paragraph(titulo, myFontTitle);
        pTitulo.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph biblioteca = new Paragraph("Biblioteca Digital EISC", myFontTitle);
        biblioteca.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph developers = new Paragraph(
                "Desarrollado por:\n Mara Cristina Bustos \n Alejandro Valds Villada", myFontTitle);
        developers.setAlignment(Paragraph.ALIGN_CENTER);
        Paragraph Fecha = new Paragraph("Fecha y Hora de Reporte: " + fecha, myFontTitle);
        //Paragraph Introduccion = new Paragraph("Reporte de usuarios registrados");
        document.open();
        // step 4
        document.add(header);
        document.add(new Paragraph("\r\n"));
        document.add(Univalle);
        document.add(new Paragraph("\r\n"));
        document.add(pTitulo);
        document.add(new Paragraph("\r\n"));
        document.add(biblioteca);
        document.add(new Paragraph("\r\n"));
        document.add(developers);
        document.add(new Paragraph("\r\n"));
        document.add(Fecha);
        document.add(new Paragraph("\r\n"));
        // document.add(Introduccion);
        document.add(new Paragraph("\r\n"));
        for (int i = 0; i < contenido.size(); i++) {
            document.add(contenido.get(i).getClass().equals(new PdfPTable(2).getClass())
                    ? (PdfPTable) contenido.get(i)
                    : contenido.get(i));
        }
        // step 5
        document.close();
    } catch (DocumentException de) {
        System.err.println(de);
    } catch (IOException ioex) {
        System.err.println(ioex);
    }
}

From source file:bicimotos3hermanos.MainFrom.java

public void toPFD(String out, String in) {
    try {//from  ww w . j  a v a2 s .  co  m
        //Create Document Object
        Document convertJpgToPdf = new Document();
        //Create PdfWriter for Document to hold physical file
        PdfWriter.getInstance(convertJpgToPdf, new FileOutputStream(out));//"c:\\java\\ConvertImagetoPDF.pdf"
        convertJpgToPdf.open();
        //Get the input image to Convert to PDF
        Image convertJpg = Image.getInstance(in);
        convertJpg.setAbsolutePosition(0f, 260f);//posicion en x,y

        convertJpg.scalePercent(72, 85);//porcentaje en escala 
        //Add image to Document
        convertJpgToPdf.add(convertJpg);
        //Close Document
        convertJpgToPdf.close();
        System.out.println("Successfully Converted JPG to PDF in iText");
    } catch (Exception i1) {
        i1.printStackTrace();
    }
}

From source file:book.pdftemplates.FillTemplateHelper.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte canvas = writer.getDirectContentUnder();
    // background
    canvas.addTemplate(background, 0, 0);
    try {//from  w w  w.j  a  v  a  2 s. c o m
        // date
        ColumnText ct = new ColumnText(canvas);
        ct.setSimpleColumn(date);
        ct.addText(new Chunk(today, font));
        ct.go();
        // footer (page X of Y)
        ct.setSimpleColumn(footer);
        ct.addText(new Chunk("page " + writer.getPageNumber(), font));
        ct.addText(new Chunk(Image.getInstance(total), 0, 0));
        ct.go();
        // from address
        ct.setSimpleColumn(from);
        ct.addElement(new Paragraph(sender, font));
        ct.go();
        // to address
        ct.setSimpleColumn(to);
        ct.addElement(new Paragraph(receiver, font));
        ct.go();
    } catch (DocumentException e) {
        // can never happen, but if it does, we want to know!
        throw new ExceptionConverter(e);
    }
}

From source file:bouttime.report.bracketsheet.BracketSheetReport.java

License:Open Source License

public static boolean generateReport(Dao dao, List<Group> list, String outputFile, boolean doBoutNumbers,
        boolean doTimestamp) {

    if (list.isEmpty()) {
        return false;
    }/*from  ww w.j ava2 s . c  o  m*/

    // step 1: creation of a document-object
    Document document = new Document();

    try {

        // step 2: creation of the writer
        FileOutputStream fos = createOutputFile(outputFile);
        if (fos == null) {
            return false;
        }
        PdfWriter writer = PdfWriter.getInstance(document, fos);

        // step 3: we open the document
        document.open();

        // step 4: we grab the ContentByte and do some stuff with it
        PdfContentByte cb = writer.getDirectContent();

        String timestamp = "";
        if (doTimestamp) {
            timestamp = DateFormat.getInstance().format(new Date());
        }

        int rv;
        int i = 0;
        int size = list.size();
        for (Group g : list) {
            rv = addBracket(cb, dao, g, doBoutNumbers);
            if (rv != PAGE_ERROR) {
                // Print the watermark, if necessary
                boolean doWatermark = false;
                String gClass = g.getClassification();
                String wmValues = dao.getBracketsheetWatermarkValues();
                if ((wmValues != null) && !wmValues.isEmpty()) {
                    String[] tokens = wmValues.split(",");
                    for (String s : tokens) {
                        if (s.trim().equalsIgnoreCase(gClass)) {
                            doWatermark = true;
                            break;
                        }
                    }
                }

                int rotation = (rv == PAGE_ROUNDROBIN) ? 45 : 135;

                if (doWatermark) {
                    PdfContentByte ucb = writer.getDirectContentUnder();
                    BaseFont helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
                    ucb.saveState();
                    ucb.setColorFill(BaseColor.LIGHT_GRAY);
                    ucb.beginText();
                    ucb.setFontAndSize(helv, 86);
                    ucb.showTextAligned(Element.ALIGN_CENTER, gClass, document.getPageSize().getWidth() / 2,
                            document.getPageSize().getHeight() / 2, rotation);
                    ucb.endText();
                    ucb.restoreState();
                }

                if (doTimestamp) {
                    rotation -= 45;
                    float width = cb.getPdfWriter().getPageSize().getWidth();
                    int x = (rv == PAGE_ROUNDROBIN) ? 15 : (int) (width - 15);
                    int y = 15;
                    BracketSheetUtil.drawTimestamp(cb, null, x, y, 10, timestamp, rotation);
                }

                // If not doing bout numbers, this is an 'award' type of
                // bracket.  So print an image/logo, if configured.
                if (!doBoutNumbers && (dao.getBracketsheetAwardImage() != null)
                        && !dao.getBracketsheetAwardImage().isEmpty()) {
                    Image image = Image.getInstance(Image.getInstance(dao.getBracketsheetAwardImage()));
                    image.setRotationDegrees((rv == PAGE_ROUNDROBIN) ? 0 : 90);
                    PositionOnPage positionOnPage = dao.getBracketsheetAwardImagePosition();
                    if (PositionOnPage.UPPER_RIGHT == positionOnPage) {
                        float x = (rv == PAGE_ROUNDROBIN)
                                ? document.getPageSize().getWidth() - 10 - image.getWidth()
                                : 10;
                        float y = document.getPageSize().getHeight() - 10 - image.getHeight();
                        image.setAbsolutePosition(x, y);
                        cb.addImage(image);
                    } else if (PositionOnPage.CENTER == positionOnPage) {
                        // put the image in the background, in the middle of the page
                        PdfContentByte ucb = writer.getDirectContentUnder();
                        float pageX = document.getPageSize().getWidth() / 2;
                        float pageY = document.getPageSize().getHeight() / 2;
                        float imageX = image.getWidth() / 2;
                        float imageY = image.getHeight() / 2;
                        image.setAbsolutePosition(pageX - imageX, pageY - imageY);
                        ucb.addImage(image);
                    }
                }

                if (++i < size) {
                    document.newPage();
                }
            }
        }

    } catch (DocumentException de) {
        logger.error("Document Exception", de);
        return false;
    } catch (IOException ioe) {
        logger.error("IO Exception", ioe);
        return false;
    }

    // step 5: we close the document
    document.close();

    return true;
}

From source file:br.com.bikefood.model.PdfGenerator.java

public File menuGenerator(File a, Bikefood bike) throws BadElementException, IOException {
    try {//w  ww. j a  v  a 2 s  .  c  o m
        Document doc = new Document();
        PdfWriter.getInstance(doc, new FileOutputStream(a.getAbsolutePath()));

        doc.open();

        doc.add(new Paragraph("Cardpio do Bike Food: " + bike.getName()));
        doc.add(new Paragraph("Especialidade em: " + bike.getType().getType()));
        doc.add(new Paragraph("\n"));
        doc.add(new Paragraph("\n"));

        Dal dal = new Dal();

        List<Product> cardapio = dal.getProducts((int) bike.getId());

        for (int x = 0; x < cardapio.size(); x++) {

            String image;

            if (cardapio.get(x).getImg().contains("br/com/bikefood")) {
                image = "C:\\Users\\Aluno\\Documents\\NetBeansProjects\\Bikefood\\src\\br\\com\\bikefood\\image\\product.png";

            } else {
                image = cardapio.get(x).getImg();

            }

            Image img = Image.getInstance(image);
            img.scaleAbsolute(125, 125);
            doc.add(img);

            doc.add(new Paragraph("Nome do Prato: " + cardapio.get(x).getName()));
            doc.add(new Paragraph("Preo do Prato: " + cardapio.get(x).getPrice()));
            doc.add(new Paragraph("Ingredientes: " + cardapio.get(x).getIngredients()));
            doc.add(new Paragraph("\n"));
            System.out.println(cardapio.get(x).getImg());
        }

        doc.close();

        return a;

    } catch (FileNotFoundException ex) {
        Logger.getLogger(PdfGenerator.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ee) {
        Logger.getLogger(PdfGenerator.class.getName()).log(Level.SEVERE, null, ee);
    }

    return a;
}

From source file:br.com.primetestes.TableBean.java

License:Apache License

public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
    Document pdf = (Document) document;
    pdf.open();/* w  w  w.ja v a  2 s .  com*/
    pdf.setPageSize(PageSize.A4);

    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();
    String logo = servletContext.getRealPath("") + File.separator + "images" + File.separator
            + "prime_logo.png";

    pdf.add(Image.getInstance(logo));
}

From source file:BUS.ExportPDF.java

public boolean ExportPN(ArrayList<String[]> al) throws BadElementException, IOException, DocumentException {

    // To i tng ti liu
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);

    File fontFile = new File("src\\Helper\\arialuni.ttf");
    BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font f = new Font(unicode, 12);

    try {/*from   w ww.  j  av  a  2s .c  om*/
        // To i tng PdfWriter
        Date d = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");

        String s = "PhieuNhap_" + ft.format(d) + ".pdf";
        PdfWriter.getInstance(document, new FileOutputStream(s));

        // M file  thc hin ghi
        document.open();

        Paragraph title1 = new Paragraph("CO's BAKERY", FontFactory.getFont(FontFactory.HELVETICA, 18,
                Font.BOLDITALIC, new CMYKColor(0, 255, 255, 17)));
        document.add(title1);

        //Logo Group
        Image image1 = Image.getInstance("src\\Library\\cao.png");
        document.add(new Paragraph());
        document.add(image1);

        //Data

        PdfPTable t = new PdfPTable(7);
        t.setSpacingBefore(25);
        t.setSpacingAfter(25);

        PdfPCell c1 = new PdfPCell(new Phrase("STT"));
        t.addCell(c1);
        PdfPCell c2 = new PdfPCell(new Phrase("M phiu nhp", f));
        t.addCell(c2);
        PdfPCell c3 = new PdfPCell(new Phrase("M Nhn Vin", f));
        t.addCell(c3);
        PdfPCell c4 = new PdfPCell(new Phrase("Tn Nhn Vin", f));
        t.addCell(c4);
        PdfPCell c5 = new PdfPCell(new Phrase("Ngy lp", f));
        t.addCell(c5);
        PdfPCell c6 = new PdfPCell(new Phrase("Nh cung cp", f));
        t.addCell(c6);
        PdfPCell c7 = new PdfPCell(new Phrase("Tng ti?n", f));
        t.addCell(c7);

        for (int i = 0; i < al.size(); i++) {
            Object ob = i + 1;
            t.addCell(ob.toString());
            t.addCell(al.get(i)[0]);
            t.addCell(al.get(i)[1]);
            t.addCell(new Phrase(al.get(i)[2], f));
            t.addCell(al.get(i)[3]);
            t.addCell(new Phrase(al.get(i)[4], f));
            t.addCell(al.get(i)[5]);
        }
        document.add(t);
        // ?ng File
        document.close();
    } catch (FileNotFoundException | DocumentException e) {
        return false;
    }
    return true;
}

From source file:BUS.ExportPDF.java

public boolean ExportPX(ArrayList<String[]> al) throws BadElementException, IOException, DocumentException {

    // To i tng ti liu
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);

    File fontFile = new File("src\\Helper\\arialuni.ttf");
    BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font f = new Font(unicode, 12);

    try {//from  ww  w.  j  ava2  s  . c  om
        // To i tng PdfWriter
        Date d = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");

        String s = "PhieuXuat_" + ft.format(d) + ".pdf";
        PdfWriter.getInstance(document, new FileOutputStream(s));

        // M file  thc hin ghi
        document.open();

        Paragraph title1 = new Paragraph("CO's BAKERY", FontFactory.getFont(FontFactory.HELVETICA, 18,
                Font.BOLDITALIC, new CMYKColor(0, 255, 255, 17)));
        document.add(title1);

        //Logo Group
        Image image1 = Image.getInstance("src\\Library\\cao.png");
        document.add(new Paragraph());
        document.add(image1);

        //Data
        PdfPTable t = new PdfPTable(5);
        t.setSpacingBefore(25);
        t.setSpacingAfter(25);

        PdfPCell c1 = new PdfPCell(new Phrase("STT"));
        t.addCell(c1);
        PdfPCell c2 = new PdfPCell(new Phrase("M phiu xut", f));
        t.addCell(c2);
        PdfPCell c3 = new PdfPCell(new Phrase("M Nhn Vin", f));
        t.addCell(c3);
        PdfPCell c4 = new PdfPCell(new Phrase("Tn Nhn Vin", f));
        t.addCell(c4);
        PdfPCell c5 = new PdfPCell(new Phrase("Ngy lp", f));
        t.addCell(c5);

        for (int i = 0; i < al.size(); i++) {
            Object ob = i + 1;
            t.addCell(ob.toString());
            t.addCell(al.get(i)[0]);
            t.addCell(al.get(i)[1]);
            t.addCell(new Phrase(al.get(i)[2], f));
            t.addCell(al.get(i)[3]);
        }
        document.add(t);

        // ?ng File
        document.close();
    } catch (FileNotFoundException | DocumentException e) {
        return false;
    }
    return true;
}