Search code examples
javaswingprintingjtextpanepage-size

Simple page size setting when printing JTextPane?


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


Solution

  • 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.