Example usage for com.lowagie.text Image setAbsolutePosition

List of usage examples for com.lowagie.text Image setAbsolutePosition

Introduction

In this page you can find the example usage for com.lowagie.text Image setAbsolutePosition.

Prototype


public void setAbsolutePosition(float absoluteX, float absoluteY) 

Source Link

Document

Sets the absolute position of the Image.

Usage

From source file:org.sonar.report.pdf.DefaultPDFReporter.java

License:Open Source License

@Override
protected void printFrontPage(Document frontPageDocument, PdfWriter frontPageWriter)
        throws org.dom4j.DocumentException, ReportException {
    try {/*  w  ww  . ja  v a 2s . c o m*/
        URL largeLogo;
        if (super.getConfigProperty("front.page.logo").startsWith("http://")) {
            largeLogo = new URL(super.getConfigProperty("front.page.logo"));
        } else {
            largeLogo = this.getClass().getClassLoader()
                    .getResource(super.getConfigProperty("front.page.logo"));
        }
        Image logoImage = Image.getInstance(largeLogo);
        Rectangle pageSize = frontPageDocument.getPageSize();
        float positionX = pageSize.getWidth() / 2f - logoImage.getWidth() / 2f;
        logoImage.setAbsolutePosition(positionX, pageSize.getHeight() - logoImage.getHeight() - 100);
        frontPageDocument.add(logoImage);

        PdfPTable title = new PdfPTable(1);
        title.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        title.getDefaultCell().setBorder(Rectangle.NO_BORDER);

        String projectRow = super.getTextProperty("general.project") + ": " + super.getProject().getName();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateRow = df.format(super.getProject().getMeasures().getDate());
        String descriptionRow = super.getProject().getDescription();

        title.addCell(new Phrase(projectRow, Style.FRONTPAGE_FONT_1));
        title.addCell(new Phrase(descriptionRow, Style.FRONTPAGE_FONT_2));
        title.addCell(new Phrase(dateRow, Style.FRONTPAGE_FONT_3));
        title.setTotalWidth(
                pageSize.getWidth() - frontPageDocument.leftMargin() - frontPageDocument.rightMargin());
        title.writeSelectedRows(0, -1, frontPageDocument.leftMargin(),
                pageSize.getHeight() - logoImage.getHeight() - 150, frontPageWriter.getDirectContent());

    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:org.sonar.report.pdf.ExecutivePDFReporter.java

License:Open Source License

@Override
protected void printFrontPage(Document frontPageDocument, PdfWriter frontPageWriter)
        throws org.dom4j.DocumentException, ReportException {
    try {/* ww  w . j a  v  a  2s. c  o m*/
        URL largeLogo;
        if (super.getConfigProperty("front.page.logo").startsWith("http://")) {
            largeLogo = new URL(super.getConfigProperty("front.page.logo"));
        } else {
            largeLogo = this.getClass().getClassLoader()
                    .getResource(super.getConfigProperty("front.page.logo"));
        }
        Image logoImage = Image.getInstance(largeLogo);
        logoImage.scaleAbsolute(360, 200);
        Rectangle pageSize = frontPageDocument.getPageSize();
        logoImage.setAbsolutePosition(Style.FRONTPAGE_LOGO_POSITION_X, Style.FRONTPAGE_LOGO_POSITION_Y);
        frontPageDocument.add(logoImage);

        PdfPTable title = new PdfPTable(1);
        title.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        title.getDefaultCell().setBorder(Rectangle.NO_BORDER);

        String projectRow = super.getProject().getName();
        String versionRow = super.getProject().getMeasures().getVersion();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateRow = df.format(super.getProject().getMeasures().getDate());
        String descriptionRow = super.getProject().getDescription();

        title.addCell(new Phrase(projectRow, Style.FRONTPAGE_FONT_1));
        title.addCell(new Phrase(versionRow, Style.FRONTPAGE_FONT_1));
        title.addCell(new Phrase(descriptionRow, Style.FRONTPAGE_FONT_2));
        title.addCell(new Phrase(super.getProject().getMeasure(MetricKeys.PROFILE).getDataValue(),
                Style.FRONTPAGE_FONT_3));
        title.addCell(new Phrase(dateRow, Style.FRONTPAGE_FONT_3));
        title.setTotalWidth(
                pageSize.getWidth() - frontPageDocument.leftMargin() - frontPageDocument.rightMargin());
        title.writeSelectedRows(0, -1, frontPageDocument.leftMargin(), Style.FRONTPAGE_LOGO_POSITION_Y - 150,
                frontPageWriter.getDirectContent());

    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:org.viafirma.util.QRCodeUtil.java

License:Apache License

/**
 * Genera un nuevo pdf con la imagen firmada y el pie de firma.
 * @param url Url desde la que se puede descargar el documeto
 *///w  w  w . j  ava2 s. co m
public void generarImagenPdf(byte[] input, String texto, String url, String textoQR, String codFirma,
        OutputStream out) throws ExcepcionErrorInterno {
    try {
        // Nuevo Documento PDF
        Document document = new Document();

        // Obtenemos el tamao de la pgina
        Rectangle pageSize = document.getPageSize();

        PdfWriter.getInstance(document, out);
        document.open();
        addContent(texto, url, textoQR, codFirma, document, pageSize, true, false);

        // Aadimos imagen original
        Image imagen = Image.getInstance(input);
        imagen.setBorder(10);
        imagen.scaleToFit(500, 500);
        imagen.setAbsolutePosition(50, pageSize.getHeight() - 400);
        document.add(imagen);

        // Cerramos el documento
        document.close();

    } catch (Exception e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    }

}

From source file:org.viafirma.util.QRCodeUtil.java

License:Apache License

/**
 * @param texto//from  w w w. java2s .com
 *            Texto a colocar
 * @param textoQR
 *            Imagen QR a colocar
 * @param codFirma
 *            Cdigo de Firma a generar.
 * @param url Url del servicio de verificacin desde donde se puede descargar el documento.
 * @param document
 *            Documeto destino
 * @param pageSize
 *            Tamao de la pgina
 * @return 
 * @throws BadElementException
 * @throws MalformedURLException
 * @throws IOException
 * @throws DocumentException
 * @throws ExcepcionErrorInterno
 */
private static float addContent(String texto, String url, String textoQR, String codFirma, Document document,
        Rectangle pageSize, boolean completo, boolean isSellado) throws BadElementException,
        MalformedURLException, IOException, DocumentException, ExcepcionErrorInterno {

    Image imagenCabezera = null;
    float widthCabecera = 0;
    float heightCabecera = 0;
    if (!isSellado) {
        // Recuperamos la imagen de cabecera
        imagenCabezera = Image.getInstance(QRCodeUtil.class.getResource(IMAGE_CABECERA));
        // Colocamos la imagen en la zona superior de la pgina
        imagenCabezera.setAbsolutePosition(0, pageSize.getHeight() - imagenCabezera.getHeight());
        heightCabecera = imagenCabezera.getHeight();
        widthCabecera = imagenCabezera.getWidth();
    }

    // Recuperamos el pie de firma
    Image imagenQR = Image.getInstance(QRCodeUtil.getInstance().generate(texto, url, textoQR, codFirma));

    float rescalado = pageSize.getWidth() / (widthCabecera + document.leftMargin() + document.rightMargin());
    float sizeCabecera = 0;
    if (rescalado < 1f && !isSellado) {
        // Requiere rescalado, la imagen es mayor que la pgina.
        imagenCabezera.scalePercent(rescalado * 100);
        sizeCabecera = rescalado * imagenCabezera.getHeight();
    }

    float sizeCabecerayPie = HEIGHT_QR_CODE_PDF + sizeCabecera + document.bottomMargin() + document.topMargin()
            + SPACE_SEPARACION;
    // float escaleQR = HEIGHT_QR_CODE_PDF / (imagenQR.getHeight() + 5);
    float escaleQR = (pageSize.getWidth() - document.leftMargin() - document.rightMargin())
            / (imagenQR.getWidth() + 6);

    // imagen.setSpacingAfter(120);
    if (!isSellado) {
        document.add(imagenCabezera);
    }
    // Aadimos una caja de texto si estamos mostrando el contenido del
    // fichero firmado.
    if (completo) {
        // Aadimos el espacio ocupado por la imagen
        Paragraph p = new Paragraph("");
        p.setSpacingAfter(heightCabecera * rescalado);
        document.add(p);

        // Aadimos una tabla con borde donde colocar el documento.
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);
        table.getDefaultCell().setBorderWidth(10);
        PdfPCell celda = new PdfPCell();
        celda.setFixedHeight(pageSize.getHeight() - sizeCabecerayPie);
        table.addCell(celda);
        table.setSpacingAfter(SPACE_SEPARACION);
        document.add(table);
    }

    if (completo) {
        // La imagen la colocamos justo debajo
        imagenQR.setAbsolutePosition(document.leftMargin(),
                escaleQR * HEIGHT_QR_CODE_PDF - SPACE_SEPARACION * 2);
    } else {
        imagenQR.setAbsolutePosition(document.leftMargin(), pageSize.getHeight() - sizeCabecerayPie);
    }
    imagenQR.scalePercent(escaleQR * 100);
    document.add(imagenQR);

    return sizeCabecerayPie;
}

From source file:oscar.oscarEncounter.oscarConsultationRequest.pageUtil.ImagePDFCreator.java

License:Open Source License

/**
 * Prints the consultation request.//from  ww  w . ja  va  2 s  .  c  om
 * @throws IOException when an error with the output stream occurs
 * @throws DocumentException when an error in document construction occurs
 */
public void printPdf() throws IOException, DocumentException {

    Image image;
    try {
        image = Image.getInstance((String) request.getAttribute("imagePath"));
    } catch (Exception e) {
        logger.error("Unexpected error:", e);
        throw new DocumentException(e);
    }

    // Create the document we are going to write to
    document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, os);

    document.setPageSize(PageSize.LETTER);
    ResourceBundle.getBundle("oscarResources", request.getLocale())
            .getString("oscarEncounter.oscarConsultationRequest.consultationFormPrint.msgImage");
    document.addCreator("OSCAR");
    document.open();

    int type = image.getOriginalType();
    if (type == Image.ORIGINAL_TIFF) {
        // The following is composed of code from com.lowagie.tools.plugins.Tiff2Pdf modified to create the 
        // PDF in memory instead of on disk
        RandomAccessFileOrArray ra = new RandomAccessFileOrArray((String) request.getAttribute("imagePath"));
        int comps = TiffImage.getNumberOfPages(ra);
        boolean adjustSize = false;
        PdfContentByte cb = writer.getDirectContent();
        for (int c = 0; c < comps; ++c) {
            Image img = TiffImage.getTiffImage(ra, c + 1);
            if (img != null) {
                if (adjustSize) {
                    document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
                    document.newPage();
                    img.setAbsolutePosition(0, 0);
                } else {
                    if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) {
                        img.scaleToFit(500, 700);
                    }
                    img.setAbsolutePosition(20, 20);
                    document.newPage();
                    document.add(
                            new Paragraph((String) request.getAttribute("imageTitle") + " - page " + (c + 1)));
                }
                cb.addImage(img);

            }
        }
        ra.close();
    } else {
        PdfContentByte cb = writer.getDirectContent();
        if (image.getScaledWidth() > 500 || image.getScaledHeight() > 700) {
            image.scaleToFit(500, 700);
        }
        image.setAbsolutePosition(20, 20);
        cb.addImage(image);
    }
    document.close();
}

From source file:questions.graphics2D.SplitCanvas.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*from w  w w .j a  v  a2 s  . c  o m*/
        document.setPageSize(new Rectangle(100, 100));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        // create the canvas for the complete drawing:
        PdfContentByte directContent = writer.getDirectContentUnder();
        PdfTemplate canvas = directContent.createTemplate(200, 200);
        Graphics2D g2d = canvas.createGraphicsShapes(200, 200);
        // draw to the complete drawing to the canvas:
        g2d.setPaint(new Color(150, 150, 255));
        g2d.setStroke(new BasicStroke(10.0f));
        g2d.drawArc(50, 50, 100, 100, 0, 360);
        g2d.dispose();
        // wrap the canvas inside an image:
        Image img = Image.getInstance(canvas);
        // distribute the image over 4 pages:
        img.setAbsolutePosition(0, -100);
        document.add(img);
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(25, 25, 25, 100);
        g2d.drawLine(25, 25, 100, 25);
        g2d.dispose();
        document.newPage();
        img.setAbsolutePosition(-100, -100);
        document.add(img);
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(0, 25, 75, 25);
        g2d.drawLine(75, 25, 75, 100);
        g2d.dispose();
        document.newPage();
        img.setAbsolutePosition(0, 0);
        document.add(img);
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(25, 0, 25, 75);
        g2d.drawLine(25, 75, 100, 75);
        g2d.dispose();
        document.newPage();
        img.setAbsolutePosition(-100, 0);
        document.add(img);
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(0, 75, 75, 75);
        g2d.drawLine(75, 0, 75, 75);
        g2d.dispose();
    } catch (DocumentException de) {
        de.printStackTrace();
        return;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return;
    }
    document.close();
}

From source file:questions.graphics2D.SplitCanvasEasy.java

public static void main(String[] args) {
    Document document = new Document();
    try {//  ww  w  .j a v  a2 s .com
        document.setPageSize(new Rectangle(100, 100));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        // create the canvas for the complete drawing:
        PdfContentByte directContent = writer.getDirectContentUnder();
        PdfTemplate canvas = directContent.createTemplate(200, 200);
        Graphics2D g2d = canvas.createGraphicsShapes(200, 200);
        // draw to the complete drawing to the canvas:
        g2d.setPaint(new Color(150, 150, 255));
        g2d.setStroke(new BasicStroke(10.0f));
        g2d.drawArc(50, 50, 100, 100, 0, 360);
        g2d.dispose();
        // wrap the canvas inside an image:
        Image img = Image.getInstance(canvas);
        // distribute the image over 4 pages:
        img.setAbsolutePosition(0, -100);
        document.add(img);
        document.newPage();
        img.setAbsolutePosition(-100, -100);
        document.add(img);
        document.newPage();
        img.setAbsolutePosition(0, 0);
        document.add(img);
        document.newPage();
        img.setAbsolutePosition(-100, 0);
        document.add(img);
    } catch (DocumentException de) {
        de.printStackTrace();
        return;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return;
    }
    document.close();
}

From source file:questions.images.MakingImageTransparent.java

public static void main(String args[]) {
    try {//  w ww  .  j av  a  2 s .  com
        Rectangle rect;
        // GIF Image
        Image gif = Image.getInstance(GIF);
        gif.setAbsolutePosition(0, 0);
        gif.setTransparency(new int[] { 0x5B, 0x5D });
        rect = new Rectangle(gif.getScaledWidth(), gif.getScaledHeight());
        rect.setBackgroundColor(Color.YELLOW);

        Document document = new Document(rect);
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        document.add(gif);

        // JPEG Image
        java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(JPG);
        Image jpg = Image.getInstance(awtImage, null, true);
        jpg.setTransparency(new int[] { 0xF0, 0xFF });
        jpg.setAbsolutePosition(0, 0);
        rect = new Rectangle(jpg.getScaledWidth(), jpg.getScaledHeight());
        rect.setBackgroundColor(Color.YELLOW);

        document.setPageSize(rect);
        document.newPage();
        document.add(jpg);
        document.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.images.OverlappingImages.java

public static void main(String[] args) {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.POSTCARD);
    try {/* w w  w . jav  a 2s  .co  m*/
        // step 2:
        // we create a writer
        PdfWriter.getInstance(
                // that listens to the document
                document,
                // and directs a PDF-stream to a file
                new FileOutputStream(RESULT));
        // step 3: we open the document
        document.open();
        // step 4: we add a paragraph to the document
        Image img1 = Image.getInstance(RESOURCE1);
        img1.scaleToFit(PageSize.POSTCARD.getWidth(), 10000);
        img1.setAbsolutePosition(0, 0);
        document.add(img1);
        Image img2 = Image.getInstance(RESOURCE2);
        img2.setTransparency(new int[] { 0x00, 0x10 });
        img2.setAbsolutePosition(PageSize.POSTCARD.getWidth() - img2.getScaledWidth(),
                PageSize.POSTCARD.getHeight() - img2.getScaledHeight());
        document.add(img2);
        Image img3 = Image.getInstance(RESOURCE3);
        img3.setTransparency(new int[] { 0xF0, 0xFF });
        img3.setAbsolutePosition((PageSize.POSTCARD.getWidth() - img3.getScaledWidth()) / 2,
                (PageSize.POSTCARD.getHeight() - img3.getScaledHeight()) / 2);
        document.add(img3);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

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

}

From source file:questions.images.PostCard.java

public static void main(String[] args) {
    // step 1: creation of a document-object
    Document document = new Document(PageSize.POSTCARD);
    try {//from w w  w.j av  a2 s. c o m
        // step 2:
        // we create a writer
        PdfWriter.getInstance(
                // that listens to the document
                document,
                // and directs a PDF-stream to a file
                new FileOutputStream(RESULT));
        // step 3: we open the document
        document.open();
        // step 4: we add a paragraph to the document
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), 10000);
        img.setAbsolutePosition(0, 0);
        document.add(img);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

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