Showing posts with label print. Show all posts
Showing posts with label print. Show all posts

Sunday, December 16, 2012

Print JEditorPane content using Java with header and footer

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javatest.pritom;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

/**
 *
 * @author User
 */
public class JavaSimpleBrowser extends JPanel {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Print Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(new JavaSimpleBrowser(), BorderLayout.CENTER);
                frame.setSize(800, 600);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
   
    private JComponent simpleBrowser;
    private final JEditorPane editorPane;
    private final JScrollPane scrollPane;
    public JavaSimpleBrowser() {
        this.setSize(new Dimension(800, 600));
        this.setPreferredSize(new Dimension(800, 600));
        JPanel pnlM = new JPanel(new BorderLayout());
        this.add(pnlM, BorderLayout.CENTER);
       
        HTMLEditorKit kit = new HTMLEditorKit();
       
        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("body {color:#000; font-family:times; margin: 0px; }");
        styleSheet.addRule("p.line {"
                + "border-width: 3px; border-style: solid; border-color: red;"
                + "font : 10px monaco; padding: 4px; background-color: gray;"
                + "} ");
       
        editorPane = new JEditorPane();
        editorPane.setEditorKit(kit);
        Document doc = kit.createDefaultDocument();
        editorPane.setDocument(doc);
        editorPane.setContentType("text/html");
        editorPane.setText(getHtml());
        editorPane.setEditable(false);
       
        scrollPane = new JScrollPane(editorPane);
        scrollPane.setPreferredSize(new Dimension(780, 500));
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
       
        pnlM.add(scrollPane, BorderLayout.CENTER);
       
        JButton button = new JButton("Print");
        pnlM.add(button, BorderLayout.NORTH);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MessageFormat header = new MessageFormat("Order Details History");               
                MessageFormat footer = new MessageFormat(" Page #{0,number,integer}");
                try {
                    editorPane.print(header, footer);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
   
    private String getHtml() {
        StringBuffer sb = new StringBuffer();
       
        sb.append("<html>");
        sb.append("<body>");
        for(int index = 0; index < 500; index++) {
            sb.append("<p class='line'>I AM IN LINE: "+index+"</p>");
        }
        sb.append("</body>");
        sb.append("</html>");
       
        return sb.toString();
    }
}