Example usage for com.itextpdf.text Rectangle getWidth

List of usage examples for com.itextpdf.text Rectangle getWidth

Introduction

In this page you can find the example usage for com.itextpdf.text Rectangle getWidth.

Prototype

public float getWidth() 

Source Link

Document

Returns the width of the rectangle.

Usage

From source file:at.laborg.briss.ClusterManager.java

License:Open Source License

public static void clusterPages(ClusterJob clusterJob) throws IOException {
    PdfReader reader = new PdfReader(clusterJob.getSource().getAbsolutePath());

    ClusterCollection clusters = clusterJob.getClusterCollection();
    for (int page = 1; page <= reader.getNumberOfPages(); page++) {
        Rectangle layoutBox = reader.getBoxSize(page, "crop");

        if (layoutBox == null) {
            layoutBox = reader.getBoxSize(page, "media");
        }//  w  w  w  . jav  a  2 s  . co  m

        // create Cluster
        // if the pagenumber should be excluded then use it as a
        // discriminating parameter, else use default value

        int pageNumber = -1;
        if (clusterJob.getExcludedPageSet() != null && clusterJob.getExcludedPageSet().contains(page)) {
            pageNumber = page;
        }

        SingleCluster tmpCluster = new SingleCluster(page % 2 == 0, (int) layoutBox.getWidth(),
                (int) layoutBox.getHeight(), pageNumber);

        clusters.addPageToCluster(tmpCluster, page);
    }

    // for every cluster create a set of pages on which the preview will
    // be based
    for (SingleCluster cluster : clusters.getClusterToPagesMapping().keySet()) {
        cluster.choosePagesToMerge(clusters.getClusterToPagesMapping().get(cluster));
    }
    reader.close();
}

From source file:at.laborg.briss.CropManager.java

License:Open Source License

private static Rectangle calculateScaledRectangle(List<Rectangle> boxes, Float[] ratios, int rotation) {
    if (ratios == null || boxes.size() == 0)
        return null;
    Rectangle smallestBox = null;
    // find smallest box
    float smallestSquare = Float.MAX_VALUE;
    for (Rectangle box : boxes) {
        if (box != null) {
            if (smallestBox == null) {
                smallestBox = box;//from   w  ww .java 2s. c om
            }
            if (smallestSquare > box.getWidth() * box.getHeight()) {
                // set new smallest box
                smallestSquare = box.getWidth() * box.getHeight();
                smallestBox = box;
            }
        }
    }
    if (smallestBox == null)
        return null; // no useable box was found

    // rotate the ratios according to the rotation of the page
    float[] rotRatios = rotateRatios(ratios, rotation);

    // use smallest box as basis for calculation
    Rectangle scaledBox = new Rectangle(smallestBox);

    scaledBox.setLeft(smallestBox.getLeft() + (smallestBox.getWidth() * rotRatios[0]));
    scaledBox.setBottom(smallestBox.getBottom() + (smallestBox.getHeight() * rotRatios[1]));
    scaledBox.setRight(smallestBox.getLeft() + (smallestBox.getWidth() * (1 - rotRatios[2])));
    scaledBox.setTop(smallestBox.getBottom() + (smallestBox.getHeight() * (1 - rotRatios[3])));

    return scaledBox;
}

From source file:at.laborg.briss.utils.ClusterCreator.java

License:Open Source License

public static ClusterDefinition clusterPages(final File source, final PageExcludes pageExcludes)
        throws IOException {
    PdfReader reader = new PdfReader(source.getAbsolutePath());

    ClusterDefinition clusters = new ClusterDefinition();

    for (int page = 1; page <= reader.getNumberOfPages(); page++) {

        Rectangle layoutBox = getLayoutBox(reader, page);

        // create Cluster
        // if the pagenumber should be excluded then use it as a
        // discriminating parameter, else use default value

        boolean excluded = checkExclusionAndGetPageNumber(pageExcludes, page);

        PageCluster tmpCluster = new PageCluster(page % 2 == 0, (int) layoutBox.getWidth(),
                (int) layoutBox.getHeight(), excluded, page);

        clusters.addOrMergeCluster(tmpCluster);
    }/*  www. j a va 2 s. c o  m*/
    reader.close();
    clusters.selectAndSetPagesForMerging();
    return clusters;
}

From source file:ca.sqlpower.wabit.report.LayoutToPDF.java

License:Open Source License

public void writePDF() throws DocumentException, FileNotFoundException, PrinterException {
    monitorableHelper.setStarted(true);//www  . j a v a2 s .com
    int pageNum = 0;

    int numPages = layout.getNumberOfPages();
    monitorableHelper.setJobSize(numPages);
    Page page = layout.getPage();
    OutputStream out = fileOS;
    Rectangle pageSize;
    pageSize = new Rectangle(page.getWidth(), page.getHeight());

    Document pdfDoc = new Document(pageSize, 0f, 0f, 0f, 0f);

    PdfWriter pdfOut = PdfWriter.getInstance(pdfDoc, out);
    pdfDoc.open();
    pdfDoc.addCreator("Wabit " + WabitVersion.VERSION);
    PdfContentByte pdfContent = pdfOut.getDirectContent();
    Graphics2D pdfGraphics = null;
    try {
        while (pageNum < numPages) {
            monitorableHelper.checkCancelled();
            monitorableHelper.setProgress(pageNum);
            pdfGraphics = pdfContent.createGraphics(pageSize.getWidth(), pageSize.getHeight());
            int flag = layout.print(pdfGraphics, layout.getPageFormat(pageNum), pageNum);

            if (watermarker != null) {
                java.awt.Rectangle watermarkSize = new java.awt.Rectangle();
                watermarkSize.setSize(Math.round(pageSize.getWidth()), Math.round(pageSize.getHeight()));
                watermarker.watermark(pdfGraphics, watermarkSize);
            }

            pdfGraphics.dispose();
            pdfGraphics = null;

            if (flag == Printable.NO_SUCH_PAGE)
                break;

            pdfDoc.newPage();

            pageNum++;
        }
    } finally {
        if (pdfGraphics != null)
            pdfGraphics.dispose();
        if (pdfDoc != null)
            pdfDoc.close();
        monitorableHelper.setFinished(true);
    }
}

From source file:com.bluexml.side.Framework.alfresco.signature.repo.action.executer.BasePDFStampActionExecuter.java

License:Open Source License

/**
 * Gets the X value for centering the watermark image
 * @param r//  w ww. j  av a  2  s. c  om
 * @param img
 * @return
 */
protected float getCenterX(Rectangle r, Image img) {
    float x = 0;
    float pdfwidth = r.getWidth();
    float imgwidth = img.getWidth();

    x = (pdfwidth - imgwidth) / 2;

    return x;
}

From source file:com.centurylink.mdw.pdf.PdfExportHelper.java

License:Apache License

private void printGraph(DocWriter writer, ProcessCanvas canvas, Process process, Rectangle pageSize,
        Chapter chapter) throws Exception {
    Dimension graphsize = getGraphSize(process);
    // we create a template and a Graphics2D object that corresponds with it
    int w;//from  ww w .  java  2 s .  c  om
    int h;
    float scale;
    if ((float) graphsize.width < pageSize.getWidth() * 0.8
            && (float) graphsize.height < pageSize.getHeight() * 0.8) {
        w = graphsize.width + 36;
        h = graphsize.height + 36;
        scale = -1f;
    } else {
        scale = pageSize.getWidth() * 0.8f / (float) graphsize.width;
        if (scale > pageSize.getHeight() * 0.8f / (float) graphsize.height)
            scale = pageSize.getHeight() * 0.8f / (float) graphsize.height;
        w = (int) (graphsize.width * scale) + 36;
        h = (int) (graphsize.height * scale) + 36;
    }
    Image img;
    canvas.setBackground(Color.white);
    PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
    PdfTemplate tp = cb.createTemplate(w, h);
    Graphics2D g2 = tp.createGraphics(w, h);
    if (scale > 0)
        g2.scale(scale, scale);
    tp.setWidth(w);
    tp.setHeight(h);
    canvas.paintComponent(g2);
    g2.dispose();
    img = new ImgTemplate(tp);
    chapter.add(img);
}

From source file:com.chaschev.itext.CanvasBuilder.java

License:Apache License

public CanvasBuilder drawGrayRectangle(Rectangle rectangle, BaseColor color) {
    return saveState()
            .rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight())
            .setColorStroke(color).setLineWidth(0.1f).stroke().restoreState();
}

From source file:com.education.freelancer.MainActivity.java

private void setSignatureStamp(PdfStamper stamper, AcroFields acroFields, String signId) {
    String DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/DigitSign/";
    String StoredPath = DIRECTORY + signId + ".png";
    AcroFields.FieldPosition f = acroFields.getFieldPositions(signId).get(0);
    int page = f.page;
    Rectangle rect = f.position;
    File imgFile = new File(StoredPath);
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    if (myBitmap == null) {
        return;/*ww w. j  av a2 s.  com*/
    }
    // myBitmap=getResizedBitmap(myBitmap,160);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    Image img = null;
    try {
        img = Image.getInstance(stream.toByteArray());
        img.scaleToFit(rect.getWidth(), rect.getHeight());
        img.setAbsolutePosition(rect.getLeft(), rect.getBottom());
        stamper.getOverContent(page).addImage(img);
        imgFile.delete();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.github.albfernandez.joinpdf.JoinPdf.java

License:Open Source License

private void writePageNumber(final PdfContentByte cb) throws DocumentException, IOException {
    if (isPrintPageNumbers()) {
        Rectangle size = cb.getPdfDocument().getPageSize();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        this.actualPage++;
        cb.beginText();//w  w w . j a  v a  2  s  .c om
        cb.setFontAndSize(bf, 9);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, this.actualPage + " of " + this.totalPages,
                size.getWidth() / 2, 10, 0);
        cb.endText();
    }
}

From source file:com.masscustsoft.service.ToPdf.java

License:Open Source License

private void getDirectContent(PdfContentByte cb, Rectangle ps, Map it) throws Exception {
    BaseColor color = getColor(it, "fillColor");
    if (color != null)
        cb.setColorFill(color);/*w ww .  j  a  v a 2  s .  c om*/

    float x = MapUtil.getFloat(it, "x", 0f);
    float y = MapUtil.getFloat(it, "y", 0f);
    float w = MapUtil.getFloat(it, "w", 0f);
    float h = MapUtil.getFloat(it, "h", 0f);

    float xPer = MapUtil.getFloat(it, "xPer", 0f);
    float yPer = MapUtil.getFloat(it, "yPer", 0f);
    float wPer = MapUtil.getFloat(it, "wPer", 0f);
    float hPer = MapUtil.getFloat(it, "hPer", 0f);

    String pos = MapUtil.getStr(it, "position", "bottom");
    switch (pos) {
    case "top":
        y += ps.getHeight();
        break;
    case "right":
        x += ps.getWidth();
        break;
    }

    float xx = x + ps.getWidth() * xPer / 100f;
    float yy = y + ps.getWidth() * yPer / 100f;
    float ww = ps.getWidth() * wPer / 100f + w;
    float hh = ps.getHeight() * hPer / 100f + h;

    int font = MapUtil.getInt(it, "fontSize", 8);
    cb.setFontAndSize(getDefaultFont(), font);

    cb.beginText();

    String cls = MapUtil.getStr(it, "cls", "");

    if (cls.equals("image")) {
        Image img = getImage(it);
        cb.addImage(img, img.getWidth(), 0, 0, img.getHeight(), xx, yy);
    } else {
        String text = LightUtil.macro(MapUtil.getStr(it, "text", ""), '$').toString();
        float degree = MapUtil.getFloat(it, "rotateDegree", 0f);
        boolean kerned = MapUtil.getBool(it, "kerned", false);
        int align = getAlignment(it, "alignment");
        x = xx;
        y = yy;
        switch (align) {
        case Element.ALIGN_CENTER:
            x = xx + ww / 2;
            break;
        case Element.ALIGN_RIGHT:
            x = xx + ww;
            break;
        default:
            align = Element.ALIGN_LEFT;
            break;
        }
        if (kerned)
            cb.showTextAlignedKerned(align, text, x, y, degree);
        else
            cb.showTextAligned(align, text, x, y, degree);
    }

    cb.endText();
}