?
Solved

Printing the contents of a JEditorPane/HTML

Posted on 2002-06-28
5
Medium Priority
?
622 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
[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
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 150 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

Cyber Threats to Small Businesses (Part 2)

The evolving cybersecurity landscape presents SMBs with a host of new threats to their clients, their data, and their bottom line. In part 2 of this blog series, learn three quick processes Webroot’s CISO, Gary Hayslip, recommends to help small businesses beat modern threats.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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