Solved

Printing JEditorPane

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 117
JavaFX TableView not displaying correctly 3 163
How to convert from xls to xlsx using java 7 161
Delete .class files at every compile 4 58
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 different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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

752 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