Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

Printing JEditorPane

I have some html text inside a JEditorPane, how do i print that.
(i down't whant to print the text i whant to print the redered html)

tx

luis
0
_lr_
Asked:
_lr_
1 Solution
 
kylarCommented:
Make a printable JEditorPane (code follows). Make sure that it is set to an HTMLEditorKit (I assume that you are already doing this since you can display HTML) then make sure that it is in a JScrollPane (This is important, otherwise it will only print the visible area) then call doPrintActions().

Cheers,
Kylar

import javax.swing.*;
                 import java.awt.*;
                 import java.awt.print.*;
                 import java.io.Serializable;
                 import java.net.URL;
                 import java.io.*;

                 public class PrintableEditorPane extends JEditorPane
                    implements Printable, Serializable {
                 
                    public PrintableEditorPane(URL url) throws IOException{
                      super(url);
                    }

                 /**
                 * The method @print@ must be implemented for @Printable@ interface.
                 * Parameters are supplied by system.
                 */
                    public int print(Graphics g, PageFormat pf, int pageIndex)
                                                    throws PrinterException {
                        Graphics2D g2 = (Graphics2D)g;
                        g2.setColor(Color.black);    //set default foreground color to black
                                               
                        RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
                        Dimension d = this.getSize();    //get size of document
                        double panelWidth  = d.width;    //width in pixels
                        double panelHeight = d.height;   //height in pixels
                        double pageHeight = pf.getImageableHeight();   //height of printer page
                        double pageWidth  = pf.getImageableWidth();    //width of printer page
                        double scale = pageWidth/panelWidth;
                        int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
                                               
                        // Make sure not print empty pages
                        if(pageIndex >= totalNumPages) {
                            return Printable.NO_SUCH_PAGE;
                        }
                                         
                        // Shift Graphic to line up with beginning of print-imageable region
                        g2.translate(pf.getImageableX(), pf.getImageableY());
                        // Shift Graphic to line up with beginning of next page to print
                        g2.translate(0f, -pageIndex*pageHeight);
                        // Scale the page so the width fits...
                        g2.scale(scale, scale);
                        this.paint(g2);   //repaint the page for printing
                        return Printable.PAGE_EXISTS;
                    }
                    public void doPrintActions(){
                        PrinterJob pj=PrinterJob.getPrinterJob();
                        pj.setPrintable(this);
                        pj.printDialog();
                        try{
                        pj.print();
                        }catch (Exception PrintException) {}
                    }
                   
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now