0

I want to print out the contents of a JEditorPane, which contains highlighted text (the colour is set using setCharacterAttributes). However, when I use the most basic print() method, as in:

try {
    boolean complete = TextArea.print();
    if (complete) {
        return;
    }
    else {
        JOptionPane.showMessageDialog(null, "File could not be printed");
    }
}
catch (PrinterException ex) {
    JOptionPane.showMessageDialog(null, "File could not be printed");
}

the printed document does not look as I expect. Specifically:

  1. The background of the printed area, but not the margins, is light blue.
  2. Highlighted text is printed in colour.
  3. The font-size is quite large (only 6 or 7 words per line). This is specified when the JEditorPane is created - is there any way of using another size when printing?

What I want is black text on a white background. If anyone could point me in the right direction I'd be very grateful!

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68

1 Answers1

3

You can try this http://java-sl.com/JEditorPanePrinter.html to print content of JEditorPane.

Highlighting is not printed. It's not part of attributes. To print text background/foreground you should specify text element's attributes.

TO scale JEditorPane's content you can try this http://java-sl.com/Scale_In_JEditorPane.html

To see the background just set background of JEditorPane. However some editor kits use own rendering for the root views and you might need to specify the color in the root view instead.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks for the very quick response! However, reading your answer made me think I wasn't very clear when asking the question. I don't want to print out the background. What I want to do is send the contents of the JEditorPane, as a string, to a printer. Is there a quick and easy way to do this in Java? – Phil Whittaker Dec 22 '13 at 10:49
  • In the end I created a hidden JEditorPane, set the background colour and font size, copied across the text I wanted to output and printed it. A bit of a fudge, but it did what I needed. – Phil Whittaker Jun 16 '14 at 20:00