Example usage for com.itextpdf.text Annotation Annotation

List of usage examples for com.itextpdf.text Annotation Annotation

Introduction

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

Prototype

public Annotation(final float llx, final float lly, final float urx, final float ury, final String file,
        final int page) 

Source Link

Document

Constructs an Annotation.

Usage

From source file:EplanPrinter.PDFPrint.java

License:Open Source License

public String insertComment(String sx, String sy, String id, String deptValue, String userInit, String comment,
        int pageNum, int masterHeight, int masterWidth, int pinned, int customFontSize)
        throws DocumentException, IOException {
    float ratio = getRatio(masterHeight, masterWidth, pageNum);
    float x = Float.parseFloat(sx);
    float y = Float.parseFloat(sy);
    float[] f = commentTrans(x, y, masterHeight, masterWidth, pageNum);
    PdfGState gs1 = new PdfGState();
    gs1.setFillOpacity(1);//w  w  w  .  j a  va  2 s  .co m
    if (customFontSize > 0)
        fontSize = customFontSize;

    PdfContentByte fg = pds.getOverContent(pageNum);
    fg.setGState(gs1);
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    float[] trans = translate(x, y, r[pageNum - 1].getHeight(), r[pageNum - 1].getWidth(), masterHeight,
            masterWidth, pageNum);
    // comment tag image (width=35pts, height=8pts)
    float[] scalar = scale(trans[0], trans[1], 35, 8, masterHeight, masterWidth, pageNum);

    /* Addition. ftorres - 7/21/2015 - Added to account for rotated pages 
     *   with the origin (0,0) not set to the bottom-left of the page. [1400] */
    float coords[] = translateRotation(trans[0], trans[1], pageNum);
    coords = checkBounds(coords[0], coords[1], pageNum);

    /* Addition. ftorres - 10/20/2015 - If the comment was pinned in EPC, then
     *   render the comment inside a comment box. */
    if (pinned == 1) {
        insertPinnedComment(coords[0], coords[1], id + " - " + deptValue + " (" + userInit + ")", comment,
                pageNum, masterHeight, masterWidth);

        // Add the pinned comment text to page annotation -Jon Changkachith 11/24/2015
        Rectangle rect = new Rectangle(0, 0, 0, 0);
        PdfAnnotation annotation = PdfAnnotation.createText(pds.getWriter(), rect,
                id + " - " + deptValue + " (" + userInit + ")", cleanupComment(comment), true, id);
        pds.addAnnotation(annotation, pds.getWriter().getCurrentPageNumber());
    } else {
        Image image = Image.getInstance(commentIMGPath);
        image.setAbsolutePosition(coords[0] + (scalar[0] * (id.length() / 5f)), coords[1]);

        /*
         * Commented out by Jon Changkachith 12/09/2015 because it was throwing
         * DocumentException with the message "The image must have absolute positioning."
        image.scaleAbsoluteHeight(1);
        image.scaleAbsoluteWidth(1);
        */
        image.scalePercent(ratio); //Added to fix DocumentException "The image must have absolute positioning." Jon Changkachith 12/09/2015
        image.setAnnotation(new Annotation(id + " - " + deptValue + " (" + userInit + ")",
                cleanupComment(comment), 0, 0, 0, 0));
        fg.addImage(image);
    }

    fg.setLineWidth(.5f * ratio);
    fg.setColorStroke(new BaseColor(Color.decode("0x6E2405").getRGB()));
    fg.setColorFill(new BaseColor(Color.decode("0x6E2405").getRGB()));

    float tHeight = scalar[1];
    float tWidth = 0;
    if (id.length() > 3) {
        tWidth = (scalar[0] * (id.length() / 5f));
    } else {
        tWidth = (scalar[0]);
    }

    fg.moveTo(coords[0], coords[1]);
    fg.lineTo(coords[0] + (10f * ratio), coords[1] - (tHeight / 2));
    fg.lineTo(coords[0] + tWidth, coords[1] - (tHeight / 2));
    fg.lineTo(coords[0] + tWidth, coords[1] + (tHeight / 2));
    fg.lineTo(coords[0] + (10f * ratio), coords[1] + (tHeight / 2));

    fg.lineTo(coords[0], coords[1]);
    fg.closePathFillStroke();
    fg.fill();

    // Comment number that goes on the comment tag image
    Phrase p = new Phrase(id);
    p.getFont().setColor(BaseColor.WHITE);
    p.getFont().setSize(8f * ratio); //comment number font size = 8f
    //p.getChunks().get(0).setAnnotation(PdfAnnotation.createText(pds.getWriter(), new Rectangle(trans[0],trans[1], trans[0]+5f, trans[1]+5f), id, comment, true, id));

    float fs[] = translateRotation(f[0], f[1], pageNum);
    fs = checkBounds(fs[0], fs[1], pageNum);
    ColumnText.showTextAligned(fg, Element.ALIGN_LEFT, p, (float) (fs[0] + (9 * ratio)),
            (float) (fs[1] - (3 * ratio)), 0);

    return "";
}