pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chitgoks <chitg...@gmail.com>
Subject Re: drawing arrow in content stream
Date Mon, 21 Aug 2017 02:41:48 GMT
Hi maruan

this is just a followup to my previous email regarding the apperance stream
of an arrow annotation. attached is my sample code instead

because there is no padding, part of the arrow head is cut off

public class Test {

    public static void main(String[] args) throws Exception {
        PDDocument doc = new PDDocument();
        doc.getPages().add(new PDPage());

        PDPage pdPage = doc.getPages().get(0);

        float x1 = 217.2f;
        float y1 = 358.8f;
        float x2 = 330.6f;
        float y2 = 465;
        PDRectangle location = new PDRectangle(x1,y1,x2-x1,y2-y1);

        String color = "#ff0000";
        int c = Integer.parseInt(color.substring(1), 16);
        float r = ((c & 0xFF0000) >> 16) / 255f;
        float g = ((c & 0x00FF00) >>  8) / 255f;
        float b = ((c & 0x0000FF)) / 255f;
        PDColor pdColor = new PDColor( new float[] { r, g, b },
PDDeviceRGB.INSTANCE);

        PDAnnotationLine lineAnnot = new PDAnnotationLine();
        lineAnnot.setRectangle(location);
        lineAnnot.setColor(pdColor);
        lineAnnot.setEndPointEndingStyle(PDAnnotationLine.LE_OPEN_ARROW);

        float[] linePos = new float[4];
        linePos[0] = x1;
        linePos[1] = y1;
        linePos[2] = x2;
        linePos[3] = y2;
        lineAnnot.setLine(linePos);

        // draw appearance
        PDAppearanceDictionary appearanceDictionary = new
PDAppearanceDictionary();
        PDAppearanceStream appearanceStream = new PDAppearanceStream(doc);
        appearanceStream.setResources(new PDResources());

        PDPageContentStream cs = new PDPageContentStream(doc,
appearanceStream);
        PDRectangle bbox = new PDRectangle(location.getWidth(),
location.getHeight());
        appearanceStream.setBBox(bbox);
        cs.setStrokingColor(Color.RED);
        cs.setLineWidth(1);

        float[] lineArray = lineAnnot.getLine();
        float xx1 = lineArray[0] - location.getLowerLeftX();
        float yy1 = lineArray[1] - location.getLowerLeftY();
        float xx2 = lineArray[2] - location.getLowerLeftX();
        float yy2 = lineArray[3] - location.getLowerLeftY();

        cs.moveTo(xx1, yy1);

        float dx = xx2 - xx1;
        float dy = yy2 - yy1;
        double arrowAngle = Math.atan2(dy, dx);
        int arrowLength  = 10;

        cs.lineTo(xx2, yy2);

        // Draw end arrow.
        cs.lineTo((float) (xx2 - arrowLength * Math.cos(arrowAngle -
Math.PI / 6)),
            (float) (yy2 - arrowLength * Math.sin(arrowAngle - Math.PI /
6)));
        cs.moveTo(xx2, yy2);
        cs.lineTo((float) (xx2 - arrowLength * Math.cos(arrowAngle +
Math.PI / 6)),
            (float) (yy2 - arrowLength * Math.sin(arrowAngle + Math.PI /
6)));

        cs.stroke();

        cs.close();
        appearanceDictionary.setNormalAppearance(appearanceStream);
        lineAnnot.setAppearance(appearanceDictionary);

        pdPage.getAnnotations().add(lineAnnot);

        doc.save(new File("test.pdf"));
    }

}



On Sun, Aug 20, 2017 at 2:34 PM, chitgoks <chitgoks@gmail.com> wrote:

> hi maruan here
>
> https://www.dropbox.com/sh/f5zfhh00jhab48n/AABMJ4wpqeyNrmRFhKQPh_Gfa?dl=0
>
> all annotations are created with pdfbox
>
> there are 2 arrows , 1st is a line dimension (shows only 1 arrow head
> since i dont know yet how to draw the first point arrow head). 2nd one is
> an arrow.
>
> the inklist, rectangle shape are code generated from your ShowAnnotation
> sample. though i noticed that they also do not have padding which in the
> inklist side, shows the top part somehow a bit cut off
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message