Solved

Printing JEditorPane

Posted on 2001-07-11
1
383 Views
Last Modified: 2009-07-29
I have some html text inside a JEditorPane, how do i print that.
(i down't whant to print the text i whant to print the redered html)

tx

luis
0
Comment
Question by:_lr_
[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
1 Comment
 
LVL 4

Accepted Solution

by:
kylar earned 50 total points
ID: 6273878
Make a printable JEditorPane (code follows). Make sure that it is set to an HTMLEditorKit (I assume that you are already doing this since you can display HTML) then make sure that it is in a JScrollPane (This is important, otherwise it will only print the visible area) then call doPrintActions().

Cheers,
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

756 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