Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Printing JEditorPane

Posted on 2001-07-11
1
Medium Priority
?
402 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

705 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