?
Solved

Print JEditorPane

Posted on 2003-03-25
5
Medium Priority
?
935 Views
Last Modified: 2011-09-20
How can I print a HTML file displayed in JEditorPane?
Also, Hhw can I print a text displayed in JEditorPane?
0
Comment
Question by:csound
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 240 total points
ID: 8203767
following code written by '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
 

Expert Comment

by:CleanupPing
ID: 9058496
csound:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9072184
recommendation: points to bobbit31
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9732385
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept bobbit31's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question