Solved

Printing the contents of a JEditorPane/HTML

Posted on 2002-06-28
5
603 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now