Solved

Printing JEditorPane

Posted on 2001-07-11
1
357 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_
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 47
Exception after setting jdbc session management 2 38
word0 challenge 4 53
Java JRE greater than 1.6 5 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

20 Experts available now in Live!

Get 1:1 Help Now