Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

converter - Taking a screenshot of a scene or a portion of a scene in JavaFx 2.2

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...