Example usage for com.itextpdf.text Image setSpacingBefore

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

Introduction

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

Prototype


public void setSpacingBefore(final float spacing) 

Source Link

Document

Sets the spacing before this image.

Usage

From source file:com.ideationdesignservices.txtbook.pdf.TxtBookPdf.java

public float addConversationPart(ColumnText ct, int column, String dateString, String senderString,
        String contentString, Bitmap contentBitmap, Boolean isVideo, Boolean isMe)
        throws DocumentException, MalformedURLException, IOException {
    float messageWidth = 196.0f;
    Chunk dateChunk = new Chunk(new StringBuilder(String.valueOf(dateString))
            .append(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR).toString(), this.sansFont6Gray);
    float dateWidth = dateChunk.getWidthPoint();
    Paragraph contentParagraph = new Paragraph();
    if (contentString.length() > 0) {
        Element contentChunk = new Chunk(contentString, this.sansFont9);
        messageWidth = contentChunk.getWidthPoint();
        contentParagraph.add(contentChunk);
    }//from w w  w  .j a  va 2 s .c o m
    if (messageWidth < dateWidth) {
        messageWidth = dateWidth;
    }
    if (messageWidth > MAX_COLUMN_CONTENT_WIDTH) {
        messageWidth = MAX_COLUMN_CONTENT_WIDTH;
        dateWidth += 7.0f;
    }
    Paragraph dateParagraph = new Paragraph(dateChunk);
    if (isMe.booleanValue()) {
        dateParagraph.setAlignment(0);
        dateParagraph
                .setIndentationLeft((((BUBBLE_L_WIDTH + messageWidth) + BUBBLE_R_WIDTH) + 7.0f) - dateWidth);
    } else {
        dateParagraph.setAlignment(2);
        dateParagraph
                .setIndentationRight((((BUBBLE_L_WIDTH + messageWidth) + BUBBLE_R_WIDTH) + 7.0f) - dateWidth);
    }
    ct.addElement(dateParagraph);
    contentParagraph.setExtraParagraphSpace(10.0f);
    if (contentString.length() > 0) {
        contentParagraph.setAlignment(0);
        if (isMe.booleanValue()) {
            contentParagraph.setIndentationLeft(8.6f);
            contentParagraph.setIndentationRight(BUBBLE_TEXT_INDENT_ALTERNATE);
        } else {
            contentParagraph.setIndentationRight(8.6f);
            float indentLeft = COLUMN_WIDTH - (BUBBLE_L_WIDTH + messageWidth);
            if (messageWidth == MAX_COLUMN_CONTENT_WIDTH) {
                indentLeft += BUBBLE_TEXT_INDENT_ALTERNATE;
            }
            contentParagraph.setIndentationLeft(indentLeft);
        }
        ct.addElement(contentParagraph);
    } else if (contentBitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if (isVideo.booleanValue()) {
            contentBitmap.compress(CompressFormat.PNG, 50, stream);
        } else {
            contentBitmap.compress(CompressFormat.JPEG, 50, stream);
        }
        Image contentImage = Image.getInstance(stream.toByteArray());
        contentImage.scaleToFit(198.0f, 198.0f);
        if (isVideo.booleanValue()) {
            contentImage.setCompressionLevel(this.settings.compressionLevel);
        }
        contentImage.setSpacingBefore(10.0f);
        contentImage.setSpacingAfter(10.0f);
        if (isMe.booleanValue()) {
            contentImage.setAlignment(1);
        } else {
            contentImage.setAlignment(1);
        }
        ct.addElement(contentImage);
    }
    Paragraph senderParagraph = new Paragraph(new Chunk(senderString, this.sansFont9Gray));
    if (!isMe.booleanValue()) {
        senderParagraph.setAlignment(2);
    }
    senderParagraph.setSpacingAfter(BUBBLE_TEXT_INDENT_ALTERNATE);
    ct.addElement(senderParagraph);
    return messageWidth;
}

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   www .  j a  v a2s  .  c om
    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;
}