Solved

Printing the contents of a JEditorPane/HTML

Posted on 2002-06-28
5
607 Views
Last Modified: 2008-03-17
I have two scenarios where I could need to print the contents of a html page.
In the first case I am using the HTMLEditorKit to display a HTML page through a JEditorPane - how do I print the contents?
And the second, I have managed to send the `raw` contents of a html file to the printer without using the JEditorPane, but all the source tags are also displayed with no formatting - is there a way to preformat the code before sending it to the printer?? I am already trying to put this through the JEditorPane as I thought this may provide a way around this, but now have the problem of printing from there.
Any help is much appreciated,
Thanks,
Mike Smith
0
Comment
Question by:cen7mjs
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 7116597
for the first scenario:

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) {}
                   }

just call doPrintActions()
0
 
LVL 4

Expert Comment

by:kylar
ID: 7122721
Wow, just stole my code and didn't even credit me. Bad form!

(Printing HTML in Editor Pane:)
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20249747

http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20149088

Printing JTable (Just in case).
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20136146

Cheers,
Kylar
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7123057
> Wow, just stole my code and didn't even credit me

sorry, i copied it a long time ago to my local resources and didn't paste author/location information (don't tell me you've never done that)... so didn't know who to credit to or even where i got it from.

(note made in my local file)

0
 
LVL 4

Expert Comment

by:kylar
ID: 7123091
:)

Kylar
0
 

Author Comment

by:cen7mjs
ID: 7124097
Thanks guys,

I have managaged to solve the problem now although the way i have done it slightly different as i don`t have an actual URL to pass in - instead i have a String of html code which is place in a StringBuffer and passed into the HTMLEditorKit and used to create the JEditor Pane in the first place. I have then used a class similiar to the one above (PrintableEditorPane) extending JeditorPane to create my reference to my jEditorPane and then pretty much as above.

So... i can`t really split points, but will award points to the first answer i recieved.

Cheers,
Mike
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 191
varialbe initialization 11 32
login jsp example 24 46
Configure a Bean in an XML file 4 31
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

785 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