首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在Javafx 11+中创建自定义纸张大小

在JavaFX 11+中创建自定义纸张大小,可以通过以下步骤实现:

  1. 首先,需要创建一个自定义的Paper类,用于定义纸张的大小和单位。可以使用JavaFX的Measurement类来表示纸张的尺寸和单位。
代码语言:txt
复制
import javafx.print.Paper;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.transform.Scale;

public class CustomPaper {
    public static void main(String[] args) {
        // 创建自定义纸张大小
        double widthInInches = 8.5;
        double heightInInches = 11;
        Paper customPaper = createCustomPaper(widthInInches, heightInInches);

        // 创建打印任务
        Printer printer = Printer.getDefaultPrinter();
        PrinterJob printerJob = PrinterJob.createPrinterJob(printer);

        // 设置打印纸张大小
        printerJob.getJobSettings().setPageLayout(printer.createPageLayout(customPaper, Printer.MarginType.DEFAULT));

        // 执行打印任务
        if (printerJob != null) {
            boolean success = printerJob.printPage(printNode);
            if (success) {
                printerJob.endJob();
            }
        }
    }

    private static Paper createCustomPaper(double widthInInches, double heightInInches) {
        double widthInPoints = inchesToPoints(widthInInches);
        double heightInPoints = inchesToPoints(heightInInches);
        return Paper.custom(widthInPoints, heightInPoints, PaperUnit.POINTS);
    }

    private static double inchesToPoints(double inches) {
        return inches * 72; // 1 inch = 72 points
    }
}
  1. 在上述代码中,首先创建了一个自定义纸张大小的Paper对象,通过调用createCustomPaper方法来实现。该方法将英寸转换为点(1英寸=72点),并使用Paper.custom方法创建自定义纸张。
  2. 接下来,创建打印任务并设置纸张大小。通过调用Printer.getDefaultPrinter方法获取默认打印机,然后使用PrinterJob.createPrinterJob方法创建打印任务。通过调用printerJob.getJobSettings().setPageLayout方法设置打印纸张的布局,传入自定义的纸张对象和默认的边距类型。
  3. 最后,执行打印任务。通过调用printerJob.printPage方法打印指定的节点(printNode),并根据返回值判断打印是否成功。如果成功,调用printerJob.endJob方法结束打印任务。

这样,就可以在JavaFX 11+中创建自定义纸张大小并进行打印。请注意,以上代码仅为示例,实际应用中可能需要根据具体需求进行适当的修改。

推荐的腾讯云相关产品:腾讯云云服务器(CVM)和腾讯云云打印服务。腾讯云云服务器提供了高性能、可扩展的云计算服务,可用于部署和运行JavaFX应用程序。腾讯云云打印服务提供了便捷的打印解决方案,可用于将打印任务发送到云打印机。您可以通过访问腾讯云官网了解更多关于这些产品的详细信息和使用方法。

腾讯云云服务器(CVM)产品介绍链接:https://cloud.tencent.com/product/cvm 腾讯云云打印服务产品介绍链接:https://cloud.tencent.com/product/cps

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • IDEA与eclipse桌面配置基础

    在eclipse中配置jdk Window–>Preferences–>java–>installed JREs–>add–>Standard VM–>选择jdk安装路径就好了 设置字符集编码为utf-8,防止中文乱码 设置字符集编码为UTF-8:Window–>Preferences–>General–>Workspace–>选择Other为UTF-8,General–>Content Types里面的Text内容全部设为UTF-8 设置新建jsp页面默认为UTF-8编码:Window–>Preferences–>Web–>JSP Files–>Encoding设置为UTF-8 设置eclipse的代码自动提示 Window–>Preferences–>java–>editor–>content assist–>右侧框里auto activation triggers for java值设置为 “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXVZ.” 在eclipse配置Maven Window–>Preferences–>Maven–>Installations–>add–>选择maven的解压目录就好了,然后勾选新增的maven, 在配置User Settings–>选择maven的settings.xml文件 Eclipse中的Java–>Installed JREs,可以选择JRE所在目录,也可以选择JDK所在目录,选择JDK所在目录有个好处就是可以查看源码。 Compiler Compiler compliance level:编译Java程序时使用的JRE版本。 Libraries:配置classpath的地方,既然要运行Java程序,肯定要包含JRE。

    03

    c#实现打印功能 c#实现打印功能,可以设置纸张大小,字体和颜色等

    ///

    /// 打印的按钮 /// /// <param name="sender"></param> /// <param name="e"></param> private void btnPrint_Click(object sender, EventArgs e) { // printDocument1 为 打印控件 //设置打印用的纸张 当设置为Custom的时候,可以自定义纸张的大小,还可以选择A4,A5等常用纸型 this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 300); this.printDocument1.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage); //将写好的格式给打印预览控件以便预览 printPreviewDialog1.Document = printDocument1; //显示打印预览 DialogResult result = printPreviewDialog1.ShowDialog(); //if (result == DialogResult.OK) //this.MyPrintDocument.Print(); }

    01
    领券