javasvgpdfboxgraphics2d

Changing color when rendering SVG in PDFBox


Does anybody know how to change color when rendering SVG into PDF with pdfbox ?

I am using following code:

File svgFile = new File("roots.svg");

SVGUniverse svgUniverse = new SVGUniverse();
SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(svgFile.toURL()));

PdfBoxGraphics2D graphics = new PdfBoxGraphics2D(document, 32, 32);
graphics.setColor(Color.green);

try {
    diagram.render(graphics);
} catch (SVGException e) {
    e.printStackTrace();
} finally {
    graphics.dispose();
}

PDFormXObject xform = graphics.getXFormObject();

try (PDPageContentStream contentWriter = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, false)) {
    AffineTransform transform = AffineTransform.getTranslateInstance(0, 0);
    transform.scale(2,2);
    xform.setMatrix(transform);
    contentWriter.setNonStrokingColor(Color.green);
    contentWriter.setStrokingColor(Color.green);
    contentWriter.drawForm(xform);
}

With following dependencies:

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.25</version>
</dependency>

<dependency>
    <groupId>com.weblookandfeel</groupId>
    <artifactId>svg-salamander</artifactId>
    <version>1.1.2.2</version>
</dependency>

But the Icon appears black instead of green.

Thanks in advance to everybody!

contentWriter.setNonStrokingColor(Color.green);
contentWriter.setStrokingColor(Color.green);

Changes nothing.


Solution

  • try this:

    diagram.getRoot().addAttribute("fill", AnimationElement.AT_CSS, colorLight);+