打印JTextPane时简单的页面大小设置? [英] Simple page size setting when printing JTextPane?

查看:137
本文介绍了打印JTextPane时简单的页面大小设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个非常简单的Print函数,用于打印Jtextpane的内容.我想将默认页面大小设置为A4,但是在四处搜寻之后,我发现了很多涉及书籍和文档格式化程序等的方法,我希望将其保持尽可能的简单.

I have a really simple Print function in my application that prints the contents of a Jtextpane. I want to set the default page size to A4 but after searching around I find lots of ways that involve book and document formater etc, I want to keep this as simple as possible.

我的代码当前为:

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属性中进行设置.

I have set the A4 paper size but don't know how to set it in the .print attributes for the JtextPane.

感谢您的帮助;

安迪

推荐答案

实际上,在尝试了StanislavL提供的链接后,我在oracle指南中发现了我认为是解决问题的更好方法的代码, ;

Actually after trying the link provided by StanislavL I found in the oracle guides what I consider to be a better way of solving my problem, the code I went with was;

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");
            }

}
}

希望这对其他人有帮助,虽然它并不完美,但效果很好,默认情况下会添加双工.

Hope this helps someone else, not saying its perfect but it does work nicely and adds duplex by default.

这篇关于打印JTextPane时简单的页面大小设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆