Java 打印JTextPane时的简单页面大小设置?
我的应用程序中有一个非常简单的打印函数,可以打印Jtextpane的内容。我想将默认页面大小设置为A4,但在四处搜索后,我找到了许多涉及书籍和文档格式化程序等的方法,我希望尽可能简单 我目前的代码是:Java 打印JTextPane时的简单页面大小设置?,java,swing,printing,jtextpane,page-size,Java,Swing,Printing,Jtextpane,Page Size,我的应用程序中有一个非常简单的打印函数,可以打印Jtextpane的内容。我想将默认页面大小设置为A4,但在四处搜索后,我找到了许多涉及书籍和文档格式化程序等的方法,我希望尽可能简单 我目前的代码是: public void printy(){ JTextPane jtp = new JTextPane(); jtp.setBackground(Color.white); try { // open the file we have just
public void printy(){
JTextPane jtp = new JTextPane();
jtp.setBackground(Color.white);
try {
// open the file we have just decrypted
File myFile = new File(deletefile + "mx.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
String[] splitdata = aBuffer.split("`"); //recover the file and split it based on `
String lines = "";
for(String line : splitdata){
lines = lines + line + System.getProperty("line.separator") + System.getProperty("line.separator");
}
myReader.close();
System.out.println(Arrays.toString(splitdata));
System.out.println(lines);
jtp.setText(lines);
boolean show = true;
try {
//set the header and footer data here
MessageFormat headerFormat = new MessageFormat("HEADER HERE");
MessageFormat footerFormat = new MessageFormat("FOOTER HERE");
Paper A4 = new Paper();
A4.setSize(595, 842);
A4.setImageableArea(43, 43, 509, 756);
jtp.print(headerFormat, footerFormat, show, null, null, show);
} catch (java.awt.print.PrinterException ex) {
ex.printStackTrace();
}
} catch (Exception ez) {
System.out.println("error in array building");
}
}
}
我已经设置了A4纸张大小,但不知道如何在JtextPane的.print属性中进行设置
谢谢你的帮助
安迪你可以使用这种方法
在那里,您可以传递所需的PageFormat,在那里您可以指定所需的纸张大小/类型。您可以使用这种方法
<> P>你可以通过PageFormat,你需要在哪里指定所需的纸张大小/类型。 < P>实际上,在尝试了SnistLavl提供的链接之后,我在Oracle指南中发现了我认为是解决我问题的一个更好的方法,我的代码是:p>
public void printy(){
JTextPane jtp = new JTextPane();
jtp.setBackground(Color.white);
try {
// open the file we have just decrypted
File myFile = new File(deletefile + "mx.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
String[] splitdata = aBuffer.split("`"); //recover the file and split it based on `
String lines = "";
for(String line : splitdata){
lines = lines + line + System.getProperty("line.separator") + System.getProperty("line.separator");
}
myReader.close();
System.out.println(Arrays.toString(splitdata));
System.out.println(lines);
jtp.setText(lines);
boolean show = true;
try {
//set the header and footer data here
MessageFormat headerFormat = new MessageFormat("Your header here - {0}"); //sets the page number
MessageFormat footerFormat = new MessageFormat("Your footer here");
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(MediaSizeName.ISO_A4);
attr_set.add(Sides.DUPLEX);
jtp.print(headerFormat, footerFormat, show, null, attr_set, show);
} catch (java.awt.print.PrinterException ex) {
ex.printStackTrace();
}
} catch (Exception ez) {
System.out.println("error in array building");
}
}
}
希望这能帮助其他人,不是说它很完美,但它确实工作得很好,默认情况下添加了双工实际上,在尝试了斯坦尼斯拉夫提供的链接之后,我在Oracle指南中发现了我认为是解决问题的更好方法,我的代码是:
public void printy(){
JTextPane jtp = new JTextPane();
jtp.setBackground(Color.white);
try {
// open the file we have just decrypted
File myFile = new File(deletefile + "mx.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
String[] splitdata = aBuffer.split("`"); //recover the file and split it based on `
String lines = "";
for(String line : splitdata){
lines = lines + line + System.getProperty("line.separator") + System.getProperty("line.separator");
}
myReader.close();
System.out.println(Arrays.toString(splitdata));
System.out.println(lines);
jtp.setText(lines);
boolean show = true;
try {
//set the header and footer data here
MessageFormat headerFormat = new MessageFormat("Your header here - {0}"); //sets the page number
MessageFormat footerFormat = new MessageFormat("Your footer here");
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(MediaSizeName.ISO_A4);
attr_set.add(Sides.DUPLEX);
jtp.print(headerFormat, footerFormat, show, null, attr_set, show);
} catch (java.awt.print.PrinterException ex) {
ex.printStackTrace();
}
} catch (Exception ez) {
System.out.println("error in array building");
}
}
}
希望这能帮助其他人,不是说它很完美,但它确实工作得很好,默认情况下添加了双工 能帮上忙吗?有点,但我很难看到如何在我的代码中使用这个示例。我希望我可以保留大致相同的代码(因为它很简单),但用某种纸张大小替换其中一个空参数?可以帮助?有点,但我很难看到如何在代码中使用该示例。我希望我可以保留大致相同的代码(因为它很简单),但用某种纸张大小替换其中一个空参数?谢谢,这看起来像是我想要的那种简单的解决方案。我一试就会回来汇报。谢谢,这看起来像是我想要的那种简单的解决方案。我一试就回来报到。