Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改JTextPane上的打印边距_Java_Printing_Margins - Fatal编程技术网

Java 更改JTextPane上的打印边距

Java 更改JTextPane上的打印边距,java,printing,margins,Java,Printing,Margins,我曾遇到过各种在Java中打印时改变页边距的解决方案,但似乎都不起作用。和 到目前为止,我得到的是 TextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); // Define a keyword attribute SimpleAttributeSet keyWord = new SimpleAttributeSet(); StyleConstants.setBold(ke

我曾遇到过各种在Java中打印时改变页边距的解决方案,但似乎都不起作用。和

到目前为止,我得到的是

TextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);

Style style = doc.addStyle("StyleName", null);
StyleConstants.setIcon(style, new ImageIcon(qrcode));

doc.insertString(0, "Title Here\n", null );
doc.insertString(doc.getLength(), "Ignored", style);

textPane.print();

使用内置打印方法时,边距默认设置为25.4mm。我希望能够编辑这些页边距,同时仍然能够有一个打印对话框

我可以验证的是,类似这样的内容会影响页面大小和输出的边距

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

//  Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);

Style style = doc.addStyle("StyleName", null);
//StyleConstants.setIcon(style, new ImageIcon(qrcode));

doc.insertString(0, "Title Here\n", null);
doc.insertString(doc.getLength(), "Ignored", style);

Paper paper = new Paper();
paper.setSize(fromCMToPPI(21.0), fromCMToPPI(29.7)); // A4
paper.setImageableArea(fromCMToPPI(5.0), fromCMToPPI(5.0), 
                fromCMToPPI(21.0) - fromCMToPPI(10.0), fromCMToPPI(29.7) - fromCMToPPI(10.0));

PageFormat pageFormat = new PageFormat();
pageFormat.setPaper(paper);

PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(textPane.getPrintable(null, null), pageFormat);
PageFormat pf = pj.pageDialog(pageFormat);
if (pj.printDialog()) {
    pj.print();
}
在突出显示更改后添加的正常输出与修改的输出边框

对话的方法

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

protected static double toPPI(double inch) {
    return inch * 72d;
}
我无法验证的是,这些值是否出现在页面设置或打印机对话框中,因为MacOS决定我不需要查看它们:/


根据以前在Windows上的经验,我似乎记得它在工作

打印对话框不提供页面设置选项卡吗?@MadProgrammer是的,但我希望该值由程序设置。感谢您的回复,但变量pj指的是什么?@hahahakebab它应该是PrinterJob pj=PrinterJob.getPrinterJob;,我已经更新了示例仅供参考,这些值不会显示在打印机对话框中,但会显示在页面设置中。我在Windows10上。