I was able to draw an InDesign drawing on a Java-generated .pdf file using PDF-Box with the following code:
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contents = new PDPageContentStream(document, page);
[...]
PDDocument doc = PDDocument.load(file);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, crop);
PDFRenderer r = new PDFRenderer(doc);
r.renderPageToGraphics(0, pdfBoxGraphics2D);
doc.close();
pdfBoxGraphics2D.dispose();
PDFormXObject xform = pdfBoxGraphics2D.getXFormObject();
contents.drawForm(xform);
Now I need to show the very same drawing on a Canvas (does not have to be a Canvas) in the JavaFX-program. I get the warning:
ICC profile is Perceptual, ignoring, treating as Display class
The .ia file is generated on a mac and the Java-Program runs on a Windows.
FXGraphics2D is a awt.Graphics2D to fx.GraphicsContext wrapper from https://github.com/jfree/fxgraphics2d
Canvas canvas = new Canvas();
PDDocument doc = PDDocument.load(file);
PDRectangle crop = doc.getPage(0).getCropBox();
canvas.setWidth(crop.getWidth());
canvas.setHeight(crop.getHeight());
PDFRenderer r = new PDFRenderer(doc);
FXGraphics2D g = new FXGraphics2D(canvas.getGraphicsContext2D());
r.renderPageToGraphics(0, g);
doc.close();
g.dispose();
vbContent.getChildren().add(canvas);
Using the above code I can barely see that it is drawn because the background is painted black. I guess the .ia file has a transparent background which is not painted correctly?
I already tried to set canvas.getGraphicsContext2D().setGlobalBlendMode(BlendMode.SCREEN);
before rendering with no effect.
Well... found it. The standard background color, which is used to initially clear the graphics area, is Black in FXGraphics2D.
I added the following before rendering. The FX color is transformed to the awt color. I found it in the link provided by James_D in a comment.
Color bg = Color.TRANSPARENT;
java.awt.Color awtBg = new java.awt.Color(
(float)bg.getRed(),
(float)bg.getGreen(),
(float)bg.getBlue(),
(float)bg.getOpacity());
g.setBackground(awtBg);