Example usage for com.itextpdf.text Image setWidthPercentage

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

Introduction

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

Prototype

public void setWidthPercentage(final float widthPercentage) 

Source Link

Document

Setter for property widthPercentage.

Usage

From source file:com.masscustsoft.service.ToPdf.java

License:Open Source License

@Override
public Image createImage(String src, final Map<String, String> attrs, final ChainedProperties chain,
        final DocListener document, final ImageProvider img_provider, final HashMap<String, Image> img_store,
        final String img_baseurl) throws DocumentException, IOException {
    Image img = null;
    // getting the image using an image provider
    if (img_provider != null)
        img = img_provider.getImage(src, attrs, chain, document);
    // getting the image from an image store
    if (img == null && img_store != null) {
        Image tim = img_store.get(src);
        if (tim != null)
            img = Image.getInstance(tim);
    }//from  ww  w.ja  va 2  s.c  o  m
    if (img != null)
        return img;
    ////if src start with data: it's dataUri and parse it imme.
    if (src.startsWith("remote?")) {
        BeanFactory bf = BeanFactory.getBeanFactory();
        String pp = src.substring(7);
        String[] ss = pp.split("\\&");
        try {
            String id = "~", fsId = LightUtil.getRepository().getFsId();
            for (String s : ss) {
                String[] sss = s.split("=");
                if (sss[0].equals("id"))
                    id = sss[1];
                if (sss[0].equals("fsId"))
                    fsId = sss[1];
            }
            IRepository fs = bf.getRepository(fsId);

            InputStream is = fs.getResource(id);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            StreamUtil.copyStream(is, os, 0);
            is.close();
            os.close();
            img = Image.getInstance(os.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (src.startsWith("data:")) {
        int i = src.indexOf(",");
        byte[] bits = Base64.decode(src.substring(i + 1));
        img = Image.getInstance(bits);
    } else {
        ////
        // introducing a base url
        // relative src references only
        if (!src.startsWith("http") && img_baseurl != null) {
            src = img_baseurl + src;
        } else if (img == null && !src.startsWith("http")) {
            String path = chain.getProperty(HtmlTags.IMAGEPATH);
            if (path == null)
                path = "";
            src = new File(path, src).getPath();
        }
        img = Image.getInstance(src);
    }

    if (img == null)
        return null;

    float actualFontSize = HtmlUtilities.parseLength(chain.getProperty(HtmlTags.SIZE),
            HtmlUtilities.DEFAULT_FONT_SIZE);
    if (actualFontSize <= 0f)
        actualFontSize = HtmlUtilities.DEFAULT_FONT_SIZE;
    String width = attrs.get(HtmlTags.WIDTH);
    float widthInPoints = HtmlUtilities.parseLength(width, actualFontSize);
    String height = attrs.get(HtmlTags.HEIGHT);

    float heightInPoints = HtmlUtilities.parseLength(height, actualFontSize);

    if (widthInPoints == 0 && heightInPoints == 0) {
        Document doc = (Document) document;
        widthInPoints = doc.getPageSize().getWidth();
    }

    if (widthInPoints > 0 && heightInPoints > 0) {
        img.scaleAbsolute(widthInPoints, heightInPoints);
    } else if (widthInPoints > 0) {
        heightInPoints = img.getHeight() * widthInPoints / img.getWidth();
        img.scaleAbsolute(widthInPoints, heightInPoints);
    } else if (heightInPoints > 0) {
        widthInPoints = img.getWidth() * heightInPoints / img.getHeight();
        img.scaleAbsolute(widthInPoints, heightInPoints);
    }

    String before = chain.getProperty(HtmlTags.BEFORE);
    if (before != null)
        img.setSpacingBefore(Float.parseFloat(before));
    String after = chain.getProperty(HtmlTags.AFTER);
    if (after != null)
        img.setSpacingAfter(Float.parseFloat(after));
    img.setWidthPercentage(0);
    return img;
}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportPdfCommand.java

License:Apache License

private void createBody(PdfPTable table, VisionVO vision) throws Exception {
    Map<String, String> managementMap = BscKpiCode.getManagementMap(false);
    //Map<String, String> calculationMap = BscKpiCode.getCalculationMap(false);      
    PdfPCell cell = null;/* w ww . j a v  a  2 s  .  c o  m*/
    for (PerspectiveVO perspective : vision.getPerspectives()) {

        Image pImage = Image.getInstance(BscReportSupportUtils.getByteIconBase("PERSPECTIVES",
                perspective.getTarget(), perspective.getMin(), perspective.getScore(), "", "", 0));
        pImage.setWidthPercentage(10f);

        String content = this.getItemsContent(perspective.getName(), perspective.getScore(),
                perspective.getWeight(), perspective.getTarget(), perspective.getMin());
        cell = new PdfPCell();
        cell.addElement(pImage);
        cell.addElement(new Phrase("\n" + content, this.getFont(perspective.getFontColor(), false)));
        this.setCellBackgroundColor(cell, perspective.getBgColor());
        cell.setRowspan(perspective.getRow());
        table.addCell(cell);

        for (ObjectiveVO objective : perspective.getObjectives()) {

            Image oImage = Image.getInstance(BscReportSupportUtils.getByteIconBase("OBJECTIVES",
                    objective.getTarget(), objective.getMin(), objective.getScore(), "", "", 0));
            oImage.setWidthPercentage(10f);

            content = this.getItemsContent(objective.getName(), objective.getScore(), objective.getWeight(),
                    objective.getTarget(), objective.getMin());
            cell = new PdfPCell();
            cell.addElement(oImage);
            cell.addElement(new Phrase("\n" + content, this.getFont(objective.getFontColor(), false)));
            this.setCellBackgroundColor(cell, objective.getBgColor());
            cell.setRowspan(objective.getRow());
            table.addCell(cell);

            for (KpiVO kpi : objective.getKpis()) {
                /*
                content = this.getKpisContent(
                      kpi, 
                      managementMap, 
                      calculationMap);   
                */

                Image kImage = Image.getInstance(BscReportSupportUtils.getByteIconBase("KPI", kpi.getTarget(),
                        kpi.getMin(), kpi.getScore(), kpi.getCompareType(), kpi.getManagement(),
                        kpi.getQuasiRange()));
                kImage.setWidthPercentage(10f);

                content = this.getKpisContent(kpi, managementMap);
                cell = new PdfPCell();
                cell.addElement(kImage);
                cell.addElement(new Phrase("\n" + content, this.getFont(kpi.getFontColor(), false)));
                this.setCellBackgroundColor(cell, kpi.getBgColor());
                table.addCell(cell);
            }

        }

    }

}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportPdfCommand.java

License:Apache License

private void createDateRange(PdfPTable table, VisionVO vision, Context context, int maxRows) throws Exception {
    String frequency = (String) context.get("frequency");
    String startYearDate = StringUtils.defaultString((String) context.get("startYearDate")).trim();
    String endYearDate = StringUtils.defaultString((String) context.get("endYearDate")).trim();
    String startDate = StringUtils.defaultString((String) context.get("startDate")).trim();
    String endDate = StringUtils.defaultString((String) context.get("endDate")).trim();
    String date1 = startDate;//from w w  w  . j  a v  a  2s  . c  o m
    String date2 = endDate;
    if (BscMeasureDataFrequency.FREQUENCY_QUARTER.equals(frequency)
            || BscMeasureDataFrequency.FREQUENCY_HALF_OF_YEAR.equals(frequency)
            || BscMeasureDataFrequency.FREQUENCY_YEAR.equals(frequency)) {
        date1 = startYearDate + "/01/01";
        date2 = endYearDate + "/12/" + SimpleUtils.getMaxDayOfMonth(Integer.parseInt(endYearDate), 12);
    }
    Map<String, Object> headContentMap = new HashMap<String, Object>();
    this.fillHeadContent(context, headContentMap);
    String content = "Frequency: " + BscMeasureDataFrequency.getFrequencyMap(false).get(frequency)
            + " Date range: " + date1 + " ~ " + date2 + "\n"
            + StringUtils.defaultString((String) headContentMap.get("headContent"));

    PdfPCell cell = null;

    cell = new PdfPCell();
    cell.addElement(new Phrase(content, this.getFont(BscReportPropertyUtils.getFontColor(), false)));
    this.setCellBackgroundColor(cell, BscReportPropertyUtils.getBackgroundColor());
    cell.setColspan(maxRows);
    table.addCell(cell);

    for (PerspectiveVO perspective : vision.getPerspectives()) {
        for (ObjectiveVO objective : perspective.getObjectives()) {
            for (KpiVO kpi : objective.getKpis()) {
                cell = new PdfPCell();
                cell.addElement(new Phrase(kpi.getName(), this.getFont(kpi.getFontColor(), false)));
                this.setCellBackgroundColor(cell, kpi.getBgColor());
                cell.setColspan(4);
                cell.setRowspan(2);
                table.addCell(cell);

                for (DateRangeScoreVO dateScore : kpi.getDateRangeScores()) {
                    cell = new PdfPCell();
                    cell.addElement(
                            new Phrase(dateScore.getDate(), this.getFont(dateScore.getFontColor(), false)));
                    this.setCellBackgroundColor(cell, dateScore.getBgColor());
                    table.addCell(cell);
                }
                for (DateRangeScoreVO dateScore : kpi.getDateRangeScores()) {
                    Image image = Image
                            .getInstance(BscReportSupportUtils.getByteIcon(kpi, dateScore.getScore()));
                    image.setWidthPercentage(20f);
                    cell = new PdfPCell();
                    cell.addElement(new Phrase(BscReportSupportUtils.parse2(dateScore.getScore()),
                            this.getFont(dateScore.getFontColor(), false)));
                    cell.addElement(image);
                    this.setCellBackgroundColor(cell, dateScore.getBgColor());
                    table.addCell(cell);
                }

            }
        }
    }

}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportPdfCommand.java

License:Apache License

private void putCharts(PdfPTable table, Context context) throws Exception {
    String pieBase64Content = SimpleUtils.getPNGBase64Content((String) context.get("pieCanvasToData"));
    String barBase64Content = SimpleUtils.getPNGBase64Content((String) context.get("barCanvasToData"));
    BufferedImage pieImage = SimpleUtils.decodeToImage(pieBase64Content);
    BufferedImage barImage = SimpleUtils.decodeToImage(barBase64Content);
    ByteArrayOutputStream pieBos = new ByteArrayOutputStream();
    ImageIO.write(pieImage, "png", pieBos);
    pieBos.flush();/*  w ww .j a  v a 2s.  c om*/
    ByteArrayOutputStream barBos = new ByteArrayOutputStream();
    ImageIO.write(barImage, "png", barBos);
    barBos.flush();

    PdfPCell cell = null;

    Image pieImgObj = Image.getInstance(pieBos.toByteArray());
    pieImgObj.setWidthPercentage(100f);
    cell = new PdfPCell();
    cell.setBorder(Rectangle.NO_BORDER);
    cell.addElement(pieImgObj);
    table.addCell(cell);

    Image barImgObj = Image.getInstance(barBos.toByteArray());
    barImgObj.setWidthPercentage(100f);
    cell = new PdfPCell();
    cell.setBorder(Rectangle.NO_BORDER);
    cell.addElement(barImgObj);
    table.addCell(cell);

}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportPdfCommand.java

License:Apache License

private void putSignature(PdfPTable table, Context context) throws Exception {
    String uploadOid = (String) context.get("uploadSignatureOid");
    if (StringUtils.isBlank(uploadOid)) {
        return;/*  www  . j ava 2 s.c om*/
    }
    byte[] imageBytes = UploadSupportUtils.getDataBytes(uploadOid);
    if (null == imageBytes) {
        return;
    }
    Image signatureImgObj = Image.getInstance(imageBytes);
    signatureImgObj.setWidthPercentage(40f);
    PdfPCell cell = new PdfPCell();
    cell.setBorder(Rectangle.NO_BORDER);
    cell.addElement(signatureImgObj);
    table.addCell(cell);
}