3

I've managed to make a WritableImage using

WritableImage snapshot = obj.getScene().snapshot(null);

Now I would like to output this screenshot on a pdf file. I've already managed to output text to a pdf using Apache pdfbox library using the following code:

PDDocument doc = null; PDPage page = null;

   try{
       doc = new PDDocument();
       page = new PDPage();

       doc.addPage(page);
       PDFont font = PDType1Font.HELVETICA_BOLD;

       PDPageContentStream content = new PDPageContentStream(doc, page);
       content.beginText();
       content.setFont( font, 12 );
       content.moveTextPositionByAmount( 100, 700 );
       content.drawString("Hello World");

       content.endText();
       content.close();
      doc.save("PDFWithText.pdf");
      doc.close();
    } catch (Exception e){
    System.out.println(e);
    }

How can I do this when using WritableImage rather that using basic String texts?

Also, how can I take a screenshot of certain nodes within a scene?

Thanks

Morelka
  • 177
  • 4
  • 16

1 Answers1

2

Taking a screenshot of a scene

You already have working code for this in your question.

WritableImage snapshot = stage.getScene().snapshot(null);

Taking a screenshot of a . . . portion of a scene in JavaFx 2.2

Taking a snapshot of Node is similar to taking snapshot of a Scene, you just use the snapshot methods on the Node rather than the scene. First place your Node in a Scene, and then snapshot the Node.

WritableImage snapshot = node.snapshot(null, null);

The first parameter which may be passed to the node.snapshot call is some configuration for SnapshotParameters (which you probably don't need, but you can investigate them to see if they are required or useful for your case).


Now I would like to output this screenshot on a pdf file. How can I do this when using WritableImage rather that using basic String texts?

I have not used the pdfbox toolkit you reference in your question. Likely the toolkit works with awt based images rather than JavaFX images, so you will need to convert your JavaFX snapshot image to an awt buffered image using SwingFXUtils.fromFXImage.

To actually get the awt encoded image into a pdf file, consult the documentation for your pdfbox toolkit. Kasas's answer to Add BufferedImage to PDFBox document would seem to provide a code snippet for this operation. Looks like the relevant code (and I haven't tried this) is:

PDPageContentStream content = new PDPageContentStream(doc, page);
PDXObjectImage ximage = new PDJpeg(doc, bufferedImage);
content.drawImage(ximage, x, y);
Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks for the reply! It really did help. I'm one step closer to placing the image on the pdf but when I use the code, the pdf doesn't appear but when I add an InputStream and OutputStream shown by the same thread you've provided, the document is created but I get an error message from the Adobe program saying Acrobat cannot display page correctly. Once I go back to using the BufferedImage class, document just doesn't get created and I don't know why. The code makes sense to me. – Morelka Aug 04 '13 at 00:51