javapdflib

PDFlib TET get name of the current color


My need is to read the color of a text with PDFlib TET.

As a basis I'm using this PDFlib example: https://www.pdflib.com/tet-cookbook/tet_and_pdflib/search_and_replace_text/

Before both result.add(new rectangle(...)) calls I'm trying to read the color like this:

    String csname = tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/name");
    if ("Separation".equals(csname)) {
        String type = tet.pcos_get_string(doc, "type:colorspaces[" + tet.colorspaceid + "]/colorantname");
        System.out.println(type);
        if (StringUtils.equalsIgnoreCase("name", type)) {
            System.out.println(tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/colorantname"));
        }
    }

Unfortunately tet.colorspaceid is always 0.

But the correct colorspaceid is 6 (with "correct" = the index of the color the text actually is written with). I know the indexes because I iterated over all colorspaces like this and for i=6 the system prints the name of the intended color:

    String type = tet.pcos_get_string(doc, "type:colorspaces[" + i + "]/colorantname");
    if (StringUtils.equalsIgnoreCase("name", type)) {
        System.out.println(tet.pcos_get_string(doc, "colorspaces[" + i + "]/colorantname"));
    }

What do I need to do for tet.colorspaceid being the id of the colorspace of the currently found word fragment?

Or am I completely wrong and TET reads the color somehow else?


Solution

  • Found it - the solution is method print_color_value in this example: https://www.pdflib.com/tet-cookbook/text/glyphinfo/

    Just copy method print_color_value, return csname (or colorantname in the if blocks) and rename the method to e.g. getColorValue.

    If needed throw away the formatter stuff.