Example usage for com.lowagie.text ElementTags ALIGN_MIDDLE

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

Introduction

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

Prototype

String ALIGN_MIDDLE

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

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.  j  a  v a  2s . co  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");
    }

}

From source file:org.apache.maven.doxia.module.itext.ITextSink.java

License:Apache License

/**
 * If the <code>name</code> is a relative link, the internal link will used a System property
 * <code>itext.basedir</code>, or the class loader.
 * {@inheritDoc}/*from  w w w  .j av  a2 s .c om*/
 */
public void figureGraphics(String name) {
    String urlName = null;
    File nameFile = null;
    if ((name.toLowerCase(Locale.ENGLISH).startsWith("http://"))
            || (name.toLowerCase(Locale.ENGLISH).startsWith("https://"))) {
        urlName = name;
    } else {
        if (System.getProperty("itext.basedir") != null) {
            try {
                nameFile = new File(System.getProperty("itext.basedir"), name);
                urlName = nameFile.toURI().toURL().toString();
            } catch (MalformedURLException e) {
                getLog().error("MalformedURLException: " + e.getMessage(), e);
            }
        } else {
            if (getClassLoader() != null) {
                if (getClassLoader().getResource(name) != null) {
                    urlName = getClassLoader().getResource(name).toString();
                }
            } else {
                if (ITextSink.class.getClassLoader().getResource(name) != null) {
                    urlName = ITextSink.class.getClassLoader().getResource(name).toString();
                }
            }
        }
    }

    if (urlName == null) {
        String msg = "No image '" + name
                + "' found in the class loader. Try to call setClassLoader(ClassLoader) before.";
        logMessage("imageNotFound", msg);

        return;
    }

    if (nameFile != null && !nameFile.exists()) {
        String msg = "No image '" + nameFile + "' found in your system, check the path.";
        logMessage("imageNotFound", msg);

        return;
    }

    boolean figureCalled = figureDefined;
    if (!figureCalled) {
        figure();
    }

    float width = 0;
    float height = 0;
    try {
        Image image = Image.getInstance(new URL(urlName));
        image.scaleToFit(ITextUtil.getDefaultPageSize().width() / 2,
                ITextUtil.getDefaultPageSize().height() / 2);
        width = image.plainWidth();
        height = image.plainHeight();
    } catch (BadElementException e) {
        getLog().error("BadElementException: " + e.getMessage(), e);
    } catch (MalformedURLException e) {
        getLog().error("MalformedURLException: " + e.getMessage(), e);
    } catch (IOException e) {
        getLog().error("IOException: " + e.getMessage(), e);
    }

    writeAddAttribute(ElementTags.URL, urlName);
    writeAddAttribute(ElementTags.ALIGN, ElementTags.ALIGN_MIDDLE);
    writeAddAttribute(ElementTags.PLAINWIDTH, String.valueOf(width));
    writeAddAttribute(ElementTags.PLAINHEIGHT, String.valueOf(height));

    actionContext.setAction(SinkActionContext.FIGURE_GRAPHICS);

    if (!figureCalled) {
        figure_();
    }
}