Example usage for com.lowagie.text ElementTags ALIGN_RIGHT

List of usage examples for com.lowagie.text ElementTags ALIGN_RIGHT

Introduction

In this page you can find the example usage for com.lowagie.text ElementTags ALIGN_RIGHT.

Prototype

String ALIGN_RIGHT

To view the source code for com.lowagie.text ElementTags ALIGN_RIGHT.

Click Source Link

Document

the possible value of an alignment attribute

Usage

From source file:com.amphisoft.epub2pdf.content.XhtmlHandler.java

License:Open Source License

private void handleImage(String url, String alt, String borderWidth, String alignment)
        throws DocumentException {
    float marginTopPt = document.topMargin();
    float marginBottomPt = document.bottomMargin();
    float marginLeftPt = document.leftMargin();
    float marginRightPt = document.rightMargin();

    if (url == null)
        return;//  w w w  .  ja  v  a  2s. c o m
    Image img = null;
    String imgSimpleName = "";
    try {
        File imgFile = new File(xhtmlDir, url);
        String imgPath = imgFile.getCanonicalPath();
        imgSimpleName = imgFile.getName();
        img = Image.getInstance(imgPath);
        if (alt == null) {
            alt = imgSimpleName;
        }
        img.setAlt(alt);
    } catch (Exception e) {
        printlnerr("epub2pdf: problem adding image " + imgSimpleName + ": " + e.getMessage());
        return;
    }
    if (borderWidth != null) {
        int border = Integer.parseInt(borderWidth);
        if (border == 0) {
            img.setBorder(Image.NO_BORDER);
        } else {
            img.setBorder(Image.BOX);
            img.setBorderWidth(border);
        }
    }

    if (alignment != null) {
        int align = Image.DEFAULT;
        if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(alignment))
            align = Image.LEFT;
        else if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(alignment))
            align = Image.RIGHT;
        else if (ElementTags.ALIGN_MIDDLE.equalsIgnoreCase(alignment))
            align = Image.MIDDLE;
        img.setAlignment(align | Image.TEXTWRAP);
    } else {
        img.setAlignment(Image.MIDDLE);
    }

    Rectangle pageRect = document.getPageSize();
    float verticalMarginTotal = marginTopPt + marginBottomPt;
    float horizontalMarginTotal = marginLeftPt + marginRightPt;

    float imgMaxWidth = pageRect.getWidth() - horizontalMarginTotal - 2;
    float imgMaxHeight = pageRect.getHeight() - verticalMarginTotal - 2;

    float imgOrigWidth = img.getWidth();
    float imgOrigHeight = img.getHeight();

    if (imgOrigHeight > imgMaxHeight || imgOrigWidth > imgMaxWidth) {
        img.scaleToFit(imgMaxWidth, imgMaxHeight);
    }
    boolean addOK = addToDocument(img);
    if (!addOK) {
        System.err.println("** addToDocument(" + img.getUrl() + ") returned false");
    }

}