corner.orm.tapestry.pdf.components.PdfImage.java Source code

Java tutorial

Introduction

Here is the source code for corner.orm.tapestry.pdf.components.PdfImage.java

Source

// Copyright 2007 the original author or authors.
// site: http://www.bjmaxinfo.com
// file: $Id: PdfImage.java 3678 2007-11-14 04:43:52Z jcai $
// created at:2007-07-31
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package corner.orm.tapestry.pdf.components;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.hivemind.util.Defense;
import org.apache.tapestry.annotations.Parameter;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;

import corner.orm.exception.CornerSystemException;
import corner.orm.tapestry.pdf.PdfWriterDelegate;
import corner.util.Constants;

/**
 * ??Pdf
 * 
 * @author <a href=mailto:Ghostbb@bjmaxinfo.com>Ghostbb</a>
 * @version $Revision: 3678 $
 * @since 2.3.7
 */
public abstract class PdfImage extends AbstractPdfComponent {

    /**
     * ?Imagebyte[]
     * 
     */
    @Parameter(required = true)
    public abstract byte[] getValue();

    /**
     * ?PDF true: false:?
     * 
     * @return boolean??
     */
    @Parameter(defaultValue = "true")
    public abstract boolean getAutoSize();

    /**
     * 
     * 
     * @return String?? percent  border,
     */
    @Parameter(defaultValue = "literal:percent")
    public abstract String getScaleType();

    /**
     * @see corner.orm.tapestry.pdf.components.AbstractPdfComponent#renderPdf(corner.orm.tapestry.pdf.PdfWriterDelegate, com.lowagie.text.Document)
     */
    @Override
    public void renderPdf(PdfWriterDelegate writer, Document doc) {
        Defense.notNull(getRectangle(), "TextField?");
        String[] p = getRectangle().split(",");
        try {
            Image image = Image.getInstance(getValue());
            image.setAbsolutePosition(Float.valueOf(p[0]), Float.valueOf(p[1]));// ?
            if (getAutoSize()) {
                if (Constants.PDFIMAGE_COMPONENT_SCALE_TYPE_BORDER.equals(getScaleType())) {
                    image.scaleAbsolute((Float.valueOf(p[2]) - Float.valueOf(p[0])),
                            (Float.valueOf(p[3]) - Float.valueOf(p[1])));// ?
                } else {
                    float picHeight = image.plainHeight();// 
                    float picWeight = image.plainWidth();// 
                    float borderHeight = Float.valueOf(p[3]) - Float.valueOf(p[1]);// 
                    float borderWeight = Float.valueOf(p[2]) - Float.valueOf(p[0]);// 

                    float heightPer = borderHeight / picHeight * 100;
                    float widthPer = borderWeight / picWeight * 100;

                    // ????
                    if (heightPer > widthPer) {
                        image.scalePercent(widthPer);
                    } else {
                        image.scalePercent(heightPer);
                    }
                }
            }
            doc.add(image);
        } catch (BadElementException e) {
            throw new CornerSystemException(e);
        } catch (MalformedURLException e) {
            throw new CornerSystemException(e);
        } catch (IOException e) {
            throw new CornerSystemException(e);
        } catch (DocumentException e) {
            throw new CornerSystemException(e);
        }
    }
}