【问题标题】:Simple page size setting when printing JTextPane?打印 JTextPane 时的简单页面大小设置?
【发布时间】:2013-05-01 22:16:05
【问题描述】:

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

我目前的代码是:

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 属性中设置它。

感谢您的帮助;

安迪

【问题讨论】:

  • 可以this帮忙吗?
  • 有点,但我很难了解如何在我的代码中使用该示例。我希望我可以保留大致相同的代码(因为它很简单),但用某种纸张大小替换其中一个空参数?

标签: java swing printing jtextpane page-size


【解决方案1】:

您可以使用http://java-sl.com/JEditorPanePrinter.html的方法

您可以在此处传递您需要的 PageFormat,您可以在其中指定所需的纸张尺寸/类型。

【讨论】:

  • 谢谢,这看起来像我所追求的那种简单的解决方案。试一试我会报告的。
【解决方案2】:

实际上,在尝试了 StanislavL 提供的链接后,我在 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");
            }

}
}

希望这对其他人有所帮助,并不是说它完美,但它确实工作得很好,并且默认添加了双工。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2020-04-27
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多