Hi!
Is there an easy way to print a html document, that is displayed in a JEditorPane using the HTMLEditorKit. The document contains multiple pages. But there are no graphics, just formatted Text (bold, different fontsize,...) and horizontal lines.
Thanks a lot,
Dirk
Here comes the code:
Is there an easy way to print a html document, that is displayed in a JEditorPane using the HTMLEditorKit. The document contains multiple pages. But there are no graphics, just formatted Text (bold, different fontsize,...) and horizontal lines.
Thanks a lot,
Dirk
Here comes the code:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.print.*;
public class HTML extends JFrame implements ActionListener, Printable
{
private URL url;
private JMenuBar menu = new JMenuBar();
private JMenu datei = new JMenu("Datei");
private JMenuItem drucken = new JMenuItem("Drucken");
private JMenuItem speichern = new JMenuItem("Speichern");
private JMenuItem schliessen = new JMenuItem("Schliessen");
private JEditorPane editorPane = new JEditorPane();
public HTML()
{
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
menu.add(datei);
datei.add(drucken);
datei.add(speichern);
datei.add(schliessen);
setJMenuBar(menu);
drucken.addActionListener(this);
speichern.addActionListener(this);
schliessen.addActionListener(this);
editorPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(editorPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(scrollPane);
setSize(600,700);
setLocation(50,50);
editorPane.setContentType("text/html");
editorPane.setEditorKit(new HTMLEditorKit());
String somehtml =
"<html><head><title>Test</title></head><body>"+
"<H1>Überschrift</H1> <HR>"+
"<b>Label0: </b>Feld0 <BR>"+
"<b>Label1: </b>Feld1 <BR>"+
"blablabla..."+
"</body></html>";
editorPane.setText(somehtml);
/* //Initialisierung aus Datei
seturl("test.html");
try
{
editorPane.setPage(url);
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + url);
}
*/
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==schliessen)
{
dispose();
System.exit(0);
}
if (e.getSource()==speichern)
{
createFile();
}
if (e.getSource()==drucken)
{
//IS THIS THE RIGHT WAY, OR IS THERE ANY EASIER WAY???
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(this);
pj.printDialog();
try{ pj.print(); }
catch (Exception PrintE) {System.err.println(PrintE.toString());}
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
{
//???????
return NO_SUCH_PAGE;
}
public void createFile()
{
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter("out.html"));
bw.write(editorPane.getText());
bw.close();
}
catch(IOException IOEx)
{
System.err.println(IOEx.toString());
}
}
public static void main(String args[])
{
HTML f = new HTML();
f.setVisible(true);
}
public void seturl(String datei)
{
String s = null;
try
{
s = "file:" + System.getProperty("user.dir")
+ System.getProperty("file.separator")
+ datei;
url = new URL(s);
}
catch (Exception e)
{
System.err.println("Couldn't create help URL: " + s);
}
}
}