Example usage for com.itextpdf.text.html.simpleparser ChainedProperties getProperty

List of usage examples for com.itextpdf.text.html.simpleparser ChainedProperties getProperty

Introduction

In this page you can find the example usage for com.itextpdf.text.html.simpleparser ChainedProperties getProperty.

Prototype

public String getProperty(String key) 

Source Link

Document

Walks through the hierarchy (bottom-up) looking for a property key.

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;/*from w ww.  j av a 2 s .c  o m*/
    // 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);
    }
    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;
}