Example usage for com.itextpdf.text Rectangle getHeight

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

Introduction

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

Prototype

public float getHeight() 

Source Link

Document

Returns the height of the rectangle.

Usage

From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java

License:Open Source License

private void insertFormXObj(PdfContentByte canvas, PdfDictionary pageDict, PdfStream formXObj,
        List<Rectangle> clippingRects, Rectangle annotRect) throws IOException {
    PdfName xobjName = generateNameForXObj(pageDict);
    canvas.saveState();/*from w  ww.  j av  a 2s  .  c  o m*/

    for (Rectangle rect : clippingRects) {
        canvas.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
    }

    canvas.clip();
    canvas.newPath();

    canvas.addFormXObj(formXObj, xobjName, 1, 0, 0, 1, annotRect.getLeft(), annotRect.getBottom());

    canvas.restoreState();
}

From source file:org.alfresco.extension.countersign.action.executer.AbstractSignatureActionExecuter.java

License:Open Source License

/**
 * Get the signature block position, using the provided JSON for the box coordinates
 * and the selected page//from  w  w w .  ja v a 2  s .  c om
 * 
 * @param pageRect
 * @param box
 * @return
 */
protected Rectangle positionBlock(Rectangle pageRect, JSONObject box) {
    float startX = Float.parseFloat(String.valueOf(box.get("startX")));
    float startY = Float.parseFloat(String.valueOf(box.get("startY")));
    float endX = Float.parseFloat(String.valueOf(box.get("endX")));
    float endY = Float.parseFloat(String.valueOf(box.get("endY")));

    // make sure that the ll and ur coordinates match iText's expectations
    startY = pageRect.getHeight() - startY;
    endY = pageRect.getHeight() - endY;

    // create the rectangle to contain the signature from the corrected coordinates
    Rectangle r = new Rectangle(startX, startY, endX, endY);

    return r;
}

From source file:org.alfresco.extension.countersign.action.executer.AbstractSignatureActionExecuter.java

License:Open Source License

/**
 * Create a rectangle for the visible stamp using the selected position and block size
 * /*  w  w w  .j a  v a 2  s.  c o  m*/
 * @param position
 * @param width
 * @param height
 * @return
 */
protected Rectangle positionBlock(String position, Rectangle pageRect, int width, int height) {

    float pageHeight = pageRect.getHeight();
    float pageWidth = pageRect.getWidth();

    Rectangle r = null;
    //Rectangle constructor(float llx, float lly, float urx, float ury)
    if (position.equals(POSITION_BOTTOMLEFT)) {
        r = new Rectangle(0, height, width, 0);
    } else if (position.equals(POSITION_BOTTOMRIGHT)) {
        r = new Rectangle(pageWidth - width, height, pageWidth, 0);
    } else if (position.equals(POSITION_TOPLEFT)) {
        r = new Rectangle(0, pageHeight, width, pageHeight - height);
    } else if (position.equals(POSITION_TOPRIGHT)) {
        r = new Rectangle(pageWidth - width, pageHeight, pageWidth, pageHeight - height);
    } else if (position.equals(POSITION_CENTER)) {
        r = new Rectangle((pageWidth / 2) - (width / 2), (pageHeight / 2) - (height / 2),
                (pageWidth / 2) + (width / 2), (pageHeight / 2) + (height / 2));
    }

    return r;
}

From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFSignatureActionExecuter.java

License:Apache License

/**
 * Create a rectangle for the visible signature using the selected position and signature size
 * //from  w ww .j  a va2s . co  m
 * @param position
 * @param width
 * @param height
 * @return
 */
private Rectangle positionSignature(String position, Rectangle pageRect, int width, int height) {

    float pageHeight = pageRect.getHeight();
    float pageWidth = pageRect.getWidth();

    Rectangle r = null;

    if (position.equals(POSITION_BOTTOMLEFT)) {
        r = new Rectangle(0, height, width, 0);
    } else if (position.equals(POSITION_BOTTOMRIGHT)) {
        r = new Rectangle(pageWidth - width, pageHeight, pageWidth, pageHeight - height);
    } else if (position.equals(POSITION_TOPLEFT)) {
        r = new Rectangle(0, pageHeight, width, pageHeight - height);
    } else if (position.equals(POSITION_TOPRIGHT)) {
        r = new Rectangle(pageWidth - width, height, pageWidth, 0);
    } else if (position.equals(POSITION_CENTER)) {
        r = new Rectangle((pageWidth / 2) - (width / 2), (pageHeight / 2) - (height / 2),
                (pageWidth / 2) + (width / 2), (pageHeight / 2) + (height / 2));
    }

    return r;
}

From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java

License:Apache License

/**
 * Applies an image watermark//w ww  . jav a  2s. c o m
 * 
 * @param reader
 * @param writer
 * @param options
 * @throws Exception
 */
private void imageAction(Action ruleAction, NodeRef actionedUponNodeRef, NodeRef watermarkNodeRef,
        ContentReader actionedUponContentReader, ContentReader watermarkContentReader,
        Map<String, Object> options) {

    PdfStamper stamp = null;
    File tempDir = null;
    ContentWriter writer = null;

    try {
        // get a temp file to stash the watermarked PDF in before moving to
        // repo
        File alfTempDir = TempFileProvider.getTempDir();
        tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId());
        tempDir.mkdir();
        File file = new File(tempDir,
                serviceRegistry.getFileFolderService().getFileInfo(actionedUponNodeRef).getName());

        // get the PDF input stream and create a reader for iText
        PdfReader reader = new PdfReader(actionedUponContentReader.getContentInputStream());
        stamp = new PdfStamper(reader, new FileOutputStream(file));
        PdfContentByte pcb;

        // get a com.itextpdf.text.Image object via java.imageio.ImageIO
        Image img = Image.getInstance(ImageIO.read(watermarkContentReader.getContentInputStream()), null);

        // get the PDF pages and position
        String pages = (String) options.get(PARAM_PAGE);
        String position = (String) options.get(PARAM_POSITION);
        String depth = (String) options.get(PARAM_WATERMARK_DEPTH);

        // image requires absolute positioning or an exception will be
        // thrown
        // set image position according to parameter. Use
        // PdfReader.getPageSizeWithRotation
        // to get the canvas size for alignment.
        img.setAbsolutePosition(100f, 100f);

        // stamp each page
        int numpages = reader.getNumberOfPages();
        for (int i = 1; i <= numpages; i++) {
            Rectangle r = reader.getPageSizeWithRotation(i);
            // set stamp position
            if (position.equals(POSITION_BOTTOMLEFT)) {
                img.setAbsolutePosition(0, 0);
            } else if (position.equals(POSITION_BOTTOMRIGHT)) {
                img.setAbsolutePosition(r.getWidth() - img.getWidth(), 0);
            } else if (position.equals(POSITION_TOPLEFT)) {
                img.setAbsolutePosition(0, r.getHeight() - img.getHeight());
            } else if (position.equals(POSITION_TOPRIGHT)) {
                img.setAbsolutePosition(r.getWidth() - img.getWidth(), r.getHeight() - img.getHeight());
            } else if (position.equals(POSITION_CENTER)) {
                img.setAbsolutePosition(getCenterX(r, img), getCenterY(r, img));
            }

            // if this is an under-text stamp, use getUnderContent.
            // if this is an over-text stamp, usse getOverContent.
            if (depth.equals(DEPTH_OVER)) {
                pcb = stamp.getOverContent(i);
            } else {
                pcb = stamp.getUnderContent(i);
            }

            // only apply stamp to requested pages
            if (checkPage(pages, i, numpages)) {
                pcb.addImage(img);
            }
        }

        stamp.close();

        // Get a writer and prep it for putting it back into the repo
        //can't use BasePDFActionExecuter.getWriter here need the nodeRef of the destination
        NodeRef destinationNode = createDestinationNode(file.getName(),
                (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER), actionedUponNodeRef);
        writer = serviceRegistry.getContentService().getWriter(destinationNode, ContentModel.PROP_CONTENT,
                true);

        writer.setEncoding(actionedUponContentReader.getEncoding());
        writer.setMimetype(FILE_MIMETYPE);

        // Put it in the repo
        writer.putContent(file);

        // delete the temp file
        file.delete();
    } catch (IOException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } catch (DocumentException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } finally {
        if (tempDir != null) {
            try {
                tempDir.delete();
            } catch (Exception ex) {
                throw new AlfrescoRuntimeException(ex.getMessage(), ex);
            }
        }

        if (stamp != null) {
            try {
                stamp.close();
            } catch (Exception ex) {
                throw new AlfrescoRuntimeException(ex.getMessage(), ex);
            }
        }
    }
}

From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java

License:Apache License

/**
 * Writes text watermark to one of the 5 preconfigured locations
 * /*from  w ww  .j a va 2s  .c  o  m*/
 * @param pcb
 * @param r
 * @param tokens
 * @param size
 * @param position
 */
private void writeAlignedText(PdfContentByte pcb, Rectangle r, Vector<String> tokens, float size,
        String position) {
    // get the dimensions of our 'rectangle' for text
    float height = size * tokens.size();
    float width = 0;
    float centerX = 0, startY = 0;
    for (int i = 0; i < tokens.size(); i++) {
        if (pcb.getEffectiveStringWidth(tokens.get(i), false) > width) {
            width = pcb.getEffectiveStringWidth(tokens.get(i), false);
        }
    }

    // now that we have the width and height, we can calculate the center
    // position for
    // the rectangle that will contain our text.
    if (position.equals(POSITION_BOTTOMLEFT)) {
        centerX = width / 2 + PAD;
        startY = 0 + PAD + height;
    } else if (position.equals(POSITION_BOTTOMRIGHT)) {
        centerX = r.getWidth() - (width / 2) - PAD;
        startY = 0 + PAD + height;
    } else if (position.equals(POSITION_TOPLEFT)) {
        centerX = width / 2 + PAD;
        startY = r.getHeight() - (PAD * 2);
    } else if (position.equals(POSITION_TOPRIGHT)) {
        centerX = r.getWidth() - (width / 2) - PAD;
        startY = r.getHeight() - (PAD * 2);
    } else if (position.equals(POSITION_CENTER)) {
        centerX = r.getWidth() / 2;
        startY = (r.getHeight() / 2) + (height / 2);
    }

    // apply text to PDF
    pcb.beginText();

    for (int t = 0; t < tokens.size(); t++) {
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, tokens.get(t), centerX, startY - (size * t), 0);
    }

    pcb.endText();

}

From source file:org.ganttproject.impex.htmlpdf.itext.ChartWriter.java

License:GNU General Public License

private Graphics2D getGraphics(ChartDimensions d) {
    if (myGraphics == null) {
        myTemplate = myWriter.getDirectContent().createTemplate(d.getFullWidth(), d.getChartHeight());
        Rectangle page = myDoc.getPageSize();
        final float width = page.getWidth() - myDoc.leftMargin() - myDoc.rightMargin();
        final float height = page.getHeight() - myDoc.bottomMargin() - myDoc.topMargin();

        final float xscale = width / d.getFullWidth();
        final float yscale = height / d.getChartHeight();
        myScale = Math.min(xscale, yscale);
        myYShift = height - d.getChartHeight() * myScale + myDoc.bottomMargin();
        myGraphics = myTemplate.createGraphics(d.getFullWidth(), d.getChartHeight(),
                myFontCache.getFontMapper(mySubstitutions, myCharset));
        myGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }/*from   w  w w  .  j  a va 2  s  .  co  m*/
    return myGraphics;
}

From source file:org.ganttproject.impex.htmlpdf.itext.ThemeImpl.java

License:GNU General Public License

private void writeColontitle(String topLeft, String topRight, String bottomLeft, String bottomRight) {
    final Document document = myDoc;
    final PdfWriter writer = myWriter;
    Rectangle page = document.getPageSize();
    PdfPTable colontitleTable = createColontitleTable(topLeft, topRight, bottomLeft, bottomRight);
    colontitleTable.writeSelectedRows(0, -1, document.leftMargin(),
            page.getHeight() - document.topMargin() + colontitleTable.getTotalHeight(),
            writer.getDirectContent());/*from w  ww  .  ja v  a  2  s . co m*/

}

From source file:org.gmdev.pdftrick.engine.MergeFiles.java

License:Open Source License

/**
 * Materially multiple pdf files are written merged file on a disk 
 * @param list//from   ww w  .  j  a v a  2s.  co  m
 * @param outputStream
 * @throws DocumentException
 * @throws IOException
 */
private void doMerge(List<StreamPwdContainer> list, OutputStream outputStream)
        throws DocumentException, IOException {
    HashMap<Integer, String> rotationFromPages = factory.getRotationFromPages();
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    int z = 0;
    for (StreamPwdContainer boom : list) {

        InputStream in = boom.getIn();
        PdfReader reader = null;
        if (!boom.getPwd().equalsIgnoreCase("")) {
            reader = new PdfReader(in, boom.getPwd().getBytes());
        } else {
            reader = new PdfReader(in);
        }

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            z++;
            int rotation = reader.getPageRotation(i);

            //set size
            Rectangle pageSize_ = reader.getPageSize(i);
            Rectangle pageSize = null;
            if (rotation == 270 || rotation == 90) {
                pageSize = new Rectangle(pageSize_.getHeight(), pageSize_.getWidth());
            } else {
                pageSize = pageSize_;
            }

            document.setPageSize(pageSize);
            writer.setCropBoxSize(pageSize);

            document.newPage();

            // import the page from source pdf
            PdfImportedPage page = writer.getImportedPage(reader, i);

            // add the page to the destination pdf
            if (rotation == 270) {
                cb.addTemplate(page, 0, 1.0f, -1.0f, 0, reader.getPageSizeWithRotation(i).getWidth(), 0);
                rotationFromPages.put(z, "" + rotation);
            } else if (rotation == 180) {
                cb.addTemplate(page, -1f, 0, 0, -1f, 0, 0);
                rotationFromPages.put(z, "" + rotation);
            } else if (rotation == 90) {
                cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
                rotationFromPages.put(z, "" + rotation);
            } else {
                cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
            }
        }
        in.close();
    }
    outputStream.flush();
    document.close();
    outputStream.close();
}

From source file:org.imos.abos.plot.JfreeChartDemo.java

License:Open Source License

protected void createPDF(String filename) {
    try {/*w w w  . j  a  v  a 2 s.c o  m*/
        Rectangle page = PageSize.A4.rotate();

        // step 1
        Document document = new Document(page);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        float width = page.getWidth();
        float height = page.getHeight();
        // add chart
        PdfTemplate pie = cb.createTemplate(width, height);
        Graphics2D g2d1 = new PdfGraphics2D(pie, width, height);
        Rectangle2D r2d1 = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2d1, r2d1);
        g2d1.dispose();
        cb.addTemplate(pie, 0, 0);
        // step 5
        document.close();
    } catch (DocumentException ex) {
        Logger.getLogger(JfreeChartDemo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(JfreeChartDemo.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println("PDF finsihed");
}