PrimaryChicken
PrimaryChicken

Reputation: 973

iText - PdfAnnotation.createInk

I am trying to parse annotation data in XFDF and draw it using the iText Library. I want to draw the annotation like the attached image.

Following is my code to test the PdfAnnotation.createInk function, but it is not working after run the code.

I have Google and read the documents, but not much information provided. Any suggestions and advice? Thanks!

// step 1
    Document document = new Document(PageSize.A4);
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destFile));
    // step 3
    document.open();

    PdfContentByte pcb = new PdfContentByte(writer);
    pcb.setColorStroke(BaseColor.RED);
    Rectangle rect = new Rectangle(52.92f, 397.56f, 173.36f, 530.67f);
    float[][] inkList = {{61.736111f,530.669250f},{61.295139f,525.820984f},{61.295139f,518.768860f},
            {61.295139f,505.986969f},{61.295139f,490.560547f},{61.295139f,470.726562f},{59.972221f,452.214844f},
            {57.767361f,434.143890f},{56.003471f,418.276703f},{53.357639f,404.172516f},{51.593750f,391.390625f},
            {50.711807f,382.134766f},{49.829861f,376.845703f}
    };

    //float inkList[][] =
    PdfAnnotation an = PdfAnnotation.createInk(writer, rect, "", inkList);
    an.setPage(1);
    an.setColor(BaseColor.RED);
    an.setFlags(PdfAnnotation.FLAGS_PRINT);

    writer.addAnnotation(an);
    //Step 5
    document.close();

enter image description here

Upvotes: 2

Views: 844

Answers (1)

mkl
mkl

Reputation: 95918

The output PDF seemingly does not contain an annotation because the defined annotation is a mere collection of isolated points.

The float[][] parameter of PdfAnnotation.createInk corresponds to the InkList entry of the Ink annotation dictionary:

InkList array (Required) An array of n arrays, each representing a stroked path. Each array shall be a series of alternating horizontal and vertical coordinates in default user space, specifying points along the path. When drawn, the points shall be connected by straight lines or curves in an implementation-dependent way.

Thus, in case of the OPs array

float[][] inkList = {{61.736111f,530.669250f},{61.295139f,525.820984f},{61.295139f,518.768860f},
        {61.295139f,505.986969f},{61.295139f,490.560547f},{61.295139f,470.726562f},{59.972221f,452.214844f},
        {57.767361f,434.143890f},{56.003471f,418.276703f},{53.357639f,404.172516f},{51.593750f,391.390625f},
        {50.711807f,382.134766f},{49.829861f,376.845703f}
};

we have a collection of 13 paths each of which contain only a single point. So, nothing is drawn.

If we combine all the points in a single path, though,

    float[][] inkList = {{61.736111f,530.669250f,61.295139f,525.820984f,61.295139f,518.768860f,
            61.295139f,505.986969f,61.295139f,490.560547f,61.295139f,470.726562f,59.972221f,452.214844f,
            57.767361f,434.143890f,56.003471f,418.276703f,53.357639f,404.172516f,51.593750f,391.390625f,
            50.711807f,382.134766f,49.829861f,376.845703f}
    };

the result is this:

Screenshot

which looks like the left line of the "H" in the screenshot in the question.

Upvotes: 4

Related Questions