简体   繁体   中英

How can I print content of JTextPane

I have code that should print content of my JTextPane control, but nothing prints on page. Page is blank. Here is my code:

 @Override
        public void actionPerformed(ActionEvent arg0) {
            // kod za printanje sadrzaja iz JTextPane-a
            /*
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(new Editor());
            boolean ok = job.printDialog();
            if(ok){
                try{
                    job.print();
                }
                catch(PrinterException pex){
                    JOptionPane.showMessageDialog(new Editor(), "Greška pri printanju dokumenta!", "Poruka", JOptionPane.INFORMATION_MESSAGE);
                }
                */
            try{
                //System.out.println(tekst1.getText());
                // PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
                  //  attr_set.add(MediaSizeName.ISO_A4);
                tekst1.setContentType("text/html");


                tekst1.print();
            }
            catch(Exception pex){
                pex.printStackTrace();
            }


        }
    };

Could anyone help me!?

As you have defined content type to text/html hence try it after setting HTML editor kit.

jTextPane.setEditorKit(new HTMLEditorKit());

or you can try it without any editor kit by setting content typt to text/pain

jTextPane.setContentType("text/plain");

or removing content type.

//jTextPane.setContentType("text/html");

For more information see Java doc of method JEditorPane.setContentType()


A sample code with screenshots:

Note: save printed file as Microsoft XPS Document Writer

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLEditorKit;

public class PrintJTextPane {
    public static void main(String[] args) {
        JFrame jframe = new JFrame();
        jframe.setSize(500, 200);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JTextPane jTextPane = new JTextPane();

        jTextPane.setEditorKit(new HTMLEditorKit());

        JButton btn = new JButton("Print");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    jTextPane.setContentType("text/html");

                    boolean done = jTextPane.print();
                    if (done) {
                        JOptionPane.showMessageDialog(null, "Printing is done");
                    } else {
                        JOptionPane.showMessageDialog(null, "Error while printing");
                    }
                } catch (Exception pex) {
                    JOptionPane.showMessageDialog(null, "Error while printing");
                    pex.printStackTrace();
                }
            }
        });

        jframe.add(btn, BorderLayout.SOUTH);

        jframe.add(jTextPane);
        jframe.setVisible(true);
    }
}

C1

C2

C3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM