0

I found a nice answer here about printing a non node object with JavaFX:
JavaFX printing non node objects

I'll report the code (I used exactly this):

PrinterJob job = PrinterJob.createPrinterJob();
PDFFile pdfFile = ... ;
if (job != null) {
    boolean success = true ;
    for (int pageNumber = 1; pageNumber <= pdfFile.getNumPages() ; pageNumber++) {
        PDFPage page = pdfFile.getPage(pageNumber, true);
        Rectangle2D bounds = page.getBBox();
        int width = (int) bounds.getWidth();
        int height = (int) bounds.getHeight();
        java.awt.Image img = page.getImage(width, height, bounds, null, true, true);
        BufferedImage bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bImg.createGraphics().drawImage(img, 0, 0, null);
        javafx.scene.image.Image fxImg = SwingFXUtils.toFXImage(bImg, null);
        ImageView imageView = new ImageView(fxImg);
        success = success & job.printPage(imageView);
    }

    if (success) {
        job.endJob();
    }
}

My problem is now about the image quality. Premise that I don't need the best image quality, but what is needed is only a "readable" content. I tried to double width and height of the java.awt.Image and then rescale the bufferedImage but nothing happened.

Is possible to improve "just a little bit" the printed image? I found ghost4j but I don't want to make the project too heavy only because of this.

Thanks in advance

Pagbo
  • 691
  • 5
  • 13
Stefano Miceli
  • 263
  • 1
  • 4
  • 17
  • Probably a ill chosen resolution: pdf uses `1/72` inches as unit. Printers more likely support multiples of 10 dpi. Furthermore you're using a pretty low resolution (72 dpi). BTW: Not loosing quality is impossible in most cases since most objects in pdfs are vector graphics. – fabian Apr 13 '18 at 16:05
  • well, thank you. So I should change the BufferedImage creation? In effects the title that I wrote is not what I am really looking for. But my real problem is that I cannot make the image too much bigger because it has to be printed in A5 format – Stefano Miceli Apr 13 '18 at 16:09
  • Are you using itext? I don't know how exactly javafx sends the info to the printer but maybe using `fitWidth` and `fitHeight` of the `ImageView` could result in a higher resolution while keeping the size the same. – fabian Apr 13 '18 at 16:12
  • yes, I am using itext. And the problem is that i am not expert with javaFX, but since some weeks I am in a javaFX project. i'll try with fit in a few moments – Stefano Miceli Apr 13 '18 at 16:17
  • nothing happened :( – Stefano Miceli Apr 13 '18 at 16:23

0 Answers0