javaapache-poipresentation

In Apache POI presentation, how to change the default color of a link added as XSLFTextRun to a cell in XSLFTable?


This is using Apache POI 5.2.5, with XMLSlideShow. While changing the color of non-link text works as expected, if a link is added to a XSLFTableCell, the color change doesn't work. Below is a sample code snippet (in Kotlin) -

    val tbl = slide.createTable()
    tbl.anchor = Rectangle(10, 10, 700, 150)
    val tr = tbl.addRow()
    val td = tr.addCell()
    val p = td.addNewTextParagraph()
    val r = p.addNewTextRun()
    r.setText("Search")
    val link = r.createHyperlink()
    link.address = "http://google.com"
    r.fontSize = 14.0
    r.setFontColor(Color(42, 198, 151)) // this does not works

It renders in default blue color.


Solution

  • A text-hyperlink does not have one font color. The text has one color when it is linked and a different color when the link has already been followed. In Office Open XML this is defined in theme color schemes. Per default a link text is blue when it is linked and magenta when the link has already been followed.

    If you need to change that, you need to change the theme color schemes.

    Example:

    ...
    XSLFSlide slide ...
    ...
    XSLFTheme theme = slide.getTheme();
    theme.getXmlObject().getThemeElements().getClrScheme().getHlink().getSrgbClr().setVal(new byte[]{(byte)255,0,0});
    theme.getXmlObject().getThemeElements().getClrScheme().getFolHlink().getSrgbClr().setVal(new byte[]{0,(byte)255,0});
    ...