Example usage for com.lowagie.text.pdf PdfContentByte addImage

List of usage examples for com.lowagie.text.pdf PdfContentByte addImage

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte addImage.

Prototype

public void addImage(Image image, float a, float b, float c, float d, float e, float f)
        throws DocumentException 

Source Link

Document

Adds an Image to the page.

Usage

From source file:hornet.framework.export.fdf.FDF.java

License:CeCILL license

/**
 * Fusion d'un champ FDF./*from  w  ww . j a v  a2  s . co m*/
 *
 * @param data
 *            the data
 * @param stamper
 *            the stamper
 * @param res
 *            the res
 * @param form
 *            the form
 * @param nomField
 *            the nom field
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws DocumentException
 *             the document exception
 */
private static void fusionChamp(final Object data, final PdfStamper stamper, final FDFRes res,
        final AcroFields form, final Object nomField) throws IOException, DocumentException {

    // utilisation du ":" comme sparateur d'accs.
    // le "." tant remplac par "_" par openoffice lors
    // de la conversion PDF.
    final String nomFieldStr = nomField.toString().replace(':', '.');

    Object value = null;
    try {
        value = PropertyUtils.getProperty(data, nomFieldStr);
    } catch (final Exception ex) {
        res.getUnmerged().add(nomFieldStr);
    }

    String valueStr;

    if (value == null) {
        valueStr = ""; // itext n'accepte pas les valeurs
        // nulles
        form.setField(nomField.toString(), valueStr);
    } else if (value instanceof FDFImage) {
        final FDFImage imValue = (FDFImage) value;
        final float[] positions = form.getFieldPositions(nomField.toString());
        final PdfContentByte content = stamper.getOverContent(1);
        final Image im = Image.getInstance(imValue.getData());
        if (imValue.isFit()) {
            content.addImage(im,
                    positions[FieldBoxPositions.URX.ordinal()] - positions[FieldBoxPositions.LLX.ordinal()], 0,
                    0, positions[FieldBoxPositions.URY.ordinal()] - positions[FieldBoxPositions.LLY.ordinal()],
                    positions[FieldBoxPositions.LLX.ordinal()], positions[FieldBoxPositions.LLY.ordinal()]);
        } else {
            content.addImage(im, im.getWidth(), 0, 0, im.getHeight(), positions[1], positions[2]);
        }
    } else if (value instanceof Date) {
        // format par dfaut date
        valueStr = DateFormat.getDateInstance(DateFormat.SHORT).format(value);
        form.setField(nomField.toString(), valueStr);
    } else if (value instanceof Boolean) {
        // format par spcial pour Checkbox
        if (Boolean.TRUE.equals(value)) {
            valueStr = "Yes";
        } else {
            valueStr = "No";
        }
        form.setField(nomField.toString(), valueStr);
    } else {
        // format par dfaut
        valueStr = value.toString();
        form.setField(nomField.toString(), valueStr);
    }
}

From source file:net.sourceforge.fenixedu.util.report.ReportsUtils.java

License:Open Source License

static public byte[] stampPdfAt(byte[] originalPdf, byte[] toStampPdf, int positionX, int positionY) {
    try {//from w ww. j  a v  a 2s  .c o  m
        PdfReader originalPdfReader = new PdfReader(originalPdf);
        PdfReader toStampPdfReader = new PdfReader(toStampPdf);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(originalPdfReader, stream);

        PdfImportedPage importedPage = stamper.getImportedPage(toStampPdfReader, 1);

        PdfContentByte overContent = stamper.getOverContent(1);

        Rectangle pageSizeWithRotation = originalPdfReader.getPageSizeWithRotation(1);
        Rectangle pageSizeWithRotationStamper = toStampPdfReader.getPageSizeWithRotation(1);

        logger.info(
                String.format("[ %s, %s]", pageSizeWithRotation.getWidth(), pageSizeWithRotation.getHeight()));
        logger.info(String.format("[ %s, %s]", pageSizeWithRotationStamper.getWidth(),
                pageSizeWithRotationStamper.getHeight()));

        Image image = Image.getInstance(importedPage);

        overContent.addImage(image, image.getWidth(), 0f, 0f, image.getHeight(), positionX, positionY);

        stamper.close();

        originalPdfReader.close();
        toStampPdfReader.close();

        return stream.toByteArray();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

From source file:nl.knaw.dans.common.lang.pdf.PdfPageLayouter.java

License:Apache License

public void onStartPage(final PdfWriter writer, final Document document) {
    if (headerImage == null)
        return;/*  w  w w .  j  av a  2 s.c o m*/

    final float top = document.top();
    final PdfContentByte canvas = writer.getDirectContent();
    float resizeFactor = // TODO make calculation logic clearer
            headerImage.getHeight() / (MARGIN_TOP - HEADER_PADDING);
    final float width = headerImage.getWidth() / resizeFactor;
    final float height = headerImage.getHeight() / resizeFactor;

    try {
        canvas.addImage(headerImage, width, 0, 0, height, MARGIN_LEFT, top - HEADER_POSITION);
    } catch (final Exception cause) {
        logger.error("can't add header image to PDF page " + document.getPageNumber(), cause);
    }

}

From source file:org.sakaiproject.evaluation.tool.reporting.EvalPDFReportBuilder.java

License:Educational Community License

public void addTitlePage(String evaltitle, String groupNames, String startDate, String endDate,
        String responseInformation, byte[] bannerImageBytes, String evalSystemTitle, String informationTitle) {
    try {//from   w  w  w.  j a va  2  s  .  c o m
        float pagefooter = paragraphFont.getSize();

        PdfContentByte cb = pdfWriter.getDirectContent();

        float docMiddle = (document.right() - document.left()) / 2 + document.leftMargin();

        Paragraph emptyPara = new Paragraph(" ");
        emptyPara.setSpacingAfter(100.0f);

        // Title
        Paragraph titlePara = new Paragraph("\n\n\n" + evaltitle, frontTitleFont);
        titlePara.setAlignment(Element.ALIGN_CENTER);
        document.add(titlePara);

        // Groups

        Paragraph groupPara = new Paragraph(groupNames, frontAuthorFont);
        groupPara.setSpacingBefore(25.0f);
        groupPara.setAlignment(Element.ALIGN_CENTER);
        document.add(groupPara);

        // Little info area? I don't know, it was on the mockup though
        Paragraph infoPara = new Paragraph(informationTitle, frontInfoFont);
        infoPara.setAlignment(Element.ALIGN_CENTER);
        infoPara.setSpacingBefore(90.0f);
        document.add(infoPara);

        // Started on
        Paragraph startedPara = new Paragraph(startDate, frontInfoFont);
        startedPara.setAlignment(Element.ALIGN_CENTER);
        startedPara.setSpacingBefore(25.0f);
        document.add(startedPara);

        // Ended on
        Paragraph endedPara = new Paragraph(endDate, frontInfoFont);
        endedPara.setAlignment(Element.ALIGN_CENTER);
        endedPara.setSpacingBefore(25.0f);
        document.add(endedPara);

        // Reply Rate
        Paragraph replyRatePara = new Paragraph(responseInformation, frontInfoFont);
        replyRatePara.setAlignment(Element.ALIGN_CENTER);
        replyRatePara.setSpacingBefore(25.0f);
        document.add(replyRatePara);

        // Logo and Tagline
        cb.beginText();
        cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 12);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, evalSystemTitle, docMiddle, document.bottom() + 20, 0);
        cb.endText();

        if (bannerImageBytes != null) {
            Image banner = Image.getInstance(bannerImageBytes);
            cb.addImage(banner, banner.getWidth(), 0, 0, banner.getHeight(),
                    docMiddle - (banner.getWidth() / 2), document.bottom() + 35);
        }

        document.newPage();

        responseArea = new ColumnText(cb);
        responseArea.setSimpleColumn(document.left(), document.top(), document.right() / 2,
                document.bottom() + pagefooter);
        responseArea.go();
    } catch (DocumentException | IOException de) {
        throw UniversalRuntimeException.accumulate(de, "Unable to create title page");
    }
}

From source file:questions.images.TransparentEllipse2.java

public static void main(String[] args) {

    Document document = new Document(PageSize.POSTCARD);
    try {//from   w  ww .j  av  a2s . com
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        // clipped image
        cb.ellipse(1, 1, PageSize.POSTCARD.getWidth() - 2, PageSize.POSTCARD.getHeight() - 2);
        cb.clip();
        cb.newPath();
        Image img = Image.getInstance(RESOURCE);
        img.scaleToFit(PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
        cb.addImage(img, PageSize.POSTCARD.getWidth(), 0, 0, PageSize.POSTCARD.getHeight(), 0, 0);

        //Prepare gradation list
        int gradationStep = 40;
        float[] gradationRatioList = new float[gradationStep];
        for (int i = 0; i < gradationStep; i++) {
            gradationRatioList[i] = 1 - (float) Math.sin(Math.toRadians(90.0f / gradationStep * (i + 1)));
        }

        //Create template
        PdfTemplate template = cb.createTemplate(PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());

        //Prepare transparent group
        PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
        transGroup.put(PdfName.CS, PdfName.DEVICEGRAY);
        transGroup.setIsolated(true);
        transGroup.setKnockout(false);
        template.setGroup(transGroup);

        //Prepare graphic state
        PdfGState gState = new PdfGState();
        PdfDictionary maskDict = new PdfDictionary();
        maskDict.put(PdfName.TYPE, PdfName.MASK);
        maskDict.put(PdfName.S, new PdfName("Luminosity"));
        maskDict.put(new PdfName("G"), template.getIndirectReference());
        gState.put(PdfName.SMASK, maskDict);
        cb.setGState(gState);

        //Create gradation for mask
        for (int i = 1; i < gradationStep + 1; i++) {
            template.setLineWidth(gradationStep + 1 - i);
            template.setGrayStroke(gradationRatioList[gradationStep - i]);
            template.ellipse(0, 0, PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
            template.stroke();
        }

        //Place template
        cb.addTemplate(template, 0, 0);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    document.close();
}

From source file:uk.org.rbc1b.roms.controller.volunteer.VolunteerBadgePdfView.java

License:Open Source License

/**
 * Adds the volunteer's image to the badge.
 *
 * @param content add content/*from w w w .ja  v a2  s  . c  om*/
 * @param img the image
 */
private static void addImage(PdfContentByte content, Image img) throws DocumentException {
    content.addImage(img, 75, 0, 0, 88, 338, 383);
}

From source file:uk.org.rbc1b.roms.controller.volunteer.VolunteerBadgePdfView.java

License:Open Source License

/**
 * Adds a Barcode39 of the id number of a Volunteer to the Badge.
 *
 * @param content to be added//  ww  w . j  a  v  a  2 s  .  c om
 * @param id volunteer id
 * @throws DocumentException
 * @throws IOException
 */
private static void addBarcode(PdfContentByte contentByte, Integer id) throws IOException, DocumentException {
    Barcode39 barcode = new Barcode39();
    BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
    barcode.setFont(baseFont);
    barcode.setSize(6);
    barcode.setCode(id.toString());
    contentByte.setColorFill(Color.BLACK);
    Image baecodeImg = barcode.createImageWithBarcode(contentByte, null, null);
    contentByte.addImage(baecodeImg, 75, 0, 0, 35, 338, 344);
}