JavaFX打印1920x1080屏幕尺寸为24“ by18”

时间:2018-07-19 20:04:56

标签: java javafx printing

我正在创建软件,但是,我希望客户端能够以(8.5x11、24x18、36x24)纸张尺寸打印到本地打印机。我正在将节点布局设置为用户屏幕的大小(大多数情况下为1920 x 1080像素)。

This is what the node looks like. This is what i'm getting

我已经尝试了多个Stack溢出帖子,例如:Link1Link2。看来我的问题与Link2密切相关。

根据我的理解,我认为我在错误地缩放节点,并且不适合可打印的场景。

下面是我尝试过的两种方法。

import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Modality;

public class Print {

    public static void pageSetup(Node node) { 

        PrinterJob job = PrinterJob.createPrinterJob();

        if (job == null){
            return;
        }
        boolean proceed = job.showPrintDialog(null);

        if (proceed){
            print(job, node);
        }
    } // end pageSetup

    private static void print(PrinterJob job, Node node) { //Prints the page

        boolean printed = job.printPage(node);

        if (printed){
            job.endJob(); //End the printer job
        }
        else { //Printing Error
            Alert printerError = new Alert(AlertType.ERROR);
            printerError.setTitle("Error");
            printerError.setHeaderText("Printing Error");
            printerError.setContentText("Uh oh! That's not supposed to happen!");
            printerError.initModality(Modality.APPLICATION_MODAL);
            printerError.showAndWait();
        }
    }// end print
}

此方法不起作用,因为它可以打印并转换1920x1080节点。

然后引导我进入下一个方法。我在哪里实际设置可成像区域和纸张尺寸。

package DSP;

import DSP.RevisionLog;
import com.itextpdf.text.BadElementException;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.Paper;
import java.awt.Image;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.*;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;
import javax.imageio.ImageIO;
import javax.print.*;

public class PrintTest{

    private static BufferedImage image;
    static Image scaled = null;

    public static void printPDF() throws IOException, BadElementException, PrintException {

        WritableImage snapshot = RevisionLog.group.snapshot(new SnapshotParameters(), null);
        File file = new File("log.png");
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", file);
        } catch (IOException e) {
            System.err.println(e);
        }

        image = ImageIO.read(new File("log.png"));

        PrinterJob pj = PrinterJob.getPrinterJob();
        if (pj.printDialog()) {
            PageFormat pf = pj.defaultPage();
            Paper paper = pf.getPaper();

            //    609.6 x 457.2 = 24x18 in
            double width = fromCMToPPI(60.96);
            double height = fromCMToPPI(45.72);

            //2100.0,1500.0);
            paper.setSize(609.6,457.2);         

            paper.setImageableArea(
                        fromCMToPPI(1.0),
                        fromCMToPPI(1.0),
                        609.6 - fromCMToPPI(1.0),
                        457.2 - fromCMToPPI(1.0));
            pf.setOrientation(PageFormat.LANDSCAPE);
            pf.setPaper(paper);           

            PageFormat validatePage = pj.validatePage(pf);
            System.out.println("Valid- " + dump(validatePage));
            pj.setPrintable(new MyPrintable(), validatePage);
            try {
                pj.print();
            } catch (PrinterException ex) {
                ex.printStackTrace();
            }
        }
    }

    protected static double fromPPItoCM(double dpi) {
        return dpi / 72 / 0.393700787;
    }

    protected static double fromCMToPPI(double cm) {
        return toPPI(cm * 0.393700787);
    }

    protected static double toPPI(double inch) {
        return inch * 72d;
    }

    protected static String dump(Paper paper) {
        StringBuilder sb = new StringBuilder(64);
          sb.append(paper.getWidth()).append("x").append(paper.getHeight())
                    .append("/").append(paper.getImageableX()).append("x").
                    append(paper.getImageableY()).append(" - ").append(paper
                    .getImageableWidth()).append("x").append(paper.getImageableHeight());
    return sb.toString();
    }

    protected static String dump(PageFormat pf) {
        Paper paper = pf.getPaper();
        return dump(paper);
    }

    public static class MyPrintable implements Printable {

        @Override
        public int print(Graphics graphics, PageFormat pageFormat,
                                     int pageIndex) throws   PrinterException {
            System.out.println(pageIndex);
            int result = NO_SUCH_PAGE;
            if (pageIndex < 1) {
                Graphics2D g2d = (Graphics2D) graphics;
                System.out.println("[Print] " + dump(pageFormat));
                double width = pageFormat.getImageableWidth();
                double height = pageFormat.getImageableHeight();

                System.out.println("Print Size = " + fromPPItoCM(width) + "x" + fromPPItoCM(height));
                g2d.translate((int) pageFormat.getImageableX(),
                            (int) pageFormat.getImageableY());

                if (width > height) {
                    scaled = image.getScaledInstance((int)Math.round(width), -1, Image.SCALE_SMOOTH);
                } else {
                    scaled = image.getScaledInstance(-1, (int)Math.round(height), Image.SCALE_SMOOTH);
                }
                g2d.drawImage(scaled, 0, 0, null);
                result = PAGE_EXISTS;
            }
            return result;
        }

    }


}

在某些坐标能够适合可成像区域的情况下,此方法确实给出了不同的结果。但是,它们非常不一致(我猜是因为纸张尺寸和可成像区域是在两个不同的实例上设置的。)

0 个答案:

没有答案