0

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.

My code currently is:

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

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

Thanks for the help;

Andy

mKorbel
  • 109,525
  • 20
  • 134
  • 319
andy
  • 391
  • 13
  • 33
  • Can [this](http://stackoverflow.com/questions/13558152/how-can-i-print-a-custom-paper-size-cheques-8-x-4) help? – Mateusz May 07 '13 at 21:00
  • Sort of, but I am struggling to see how to use the example in my code. I was hoping i could keep largely the same code (as its simple) but replace one of the null parameters with some sort of paper size? – andy May 07 '13 at 21:32

2 Answers2

2

You can use the approach http://java-sl.com/JEditorPanePrinter.html

There you can pass PageFormat you need where you can specify desired paper size/type.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks, this looks like the kind of simple solution I'm after. I will report back once I give it a try. – andy May 08 '13 at 19:03
1

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.

andy
  • 391
  • 13
  • 33