Solved

Printing the contents of a JEditorPane/HTML

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 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

14 Experts available now in Live!

Get 1:1 Help Now