Solved

JTextPane with HTML printing!

Posted on 2000-05-18
9
1,281 Views
Last Modified: 2012-06-21
How can I print the content of a JTextPane?
Exist somewhere an example?

Thank you
       sey.
0
Comment
Question by:sey7
[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
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2821484
you can extend your TextPane from JTextPand and implement the
java.awt.print.Printable Interface
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2821538
and some code:
//////////////////////////////////

public class TextPane extends JTextPane implements Printable
{
    public void PrintThis( Printable prObj )
    {
        SecurityManager security = System.getSecurityManager( );
        if( security != null )
        {
            try
            {
                security.checkPrintJobAccess( );
            }
            catch( SecurityException se )
            {
                System.out.println( "Security error trying to print data !!! :(" );
            }
        }        
        else
        {
            System.out.println( "No security manager !!! :(" );
            PrinterJob printJob = PrinterJob.getPrinterJob( );
            printJob.setPrintable( prObj );
            if( printJob.printDialog( ) )
            {
                try
                {
                    printJob.print( );
                }
                catch( Exception ex )
                {
                    ex.printStackTrace( );
                }
            }
        }
    }
    /**
     *      print this report
     *      @param g is a Graphics instance where the paint is done
     *      @param pf is the page format that is printed
     *      @param pageIndex is the page index that is printed
     */
    public int print( Graphics g, PageFormat pf, int pageIndex )    
    {
        if( pageIndex != 0 )
        {      return NO_SUCH_PAGE;
        }
        Graphics2D g2 = (Graphics2D) g;
        g2.translate( pf.getImageableX( ), pf.getImageableY( ) );
       
        System.out.println( " paper  height  " + pf.getImageableHeight() );
        System.out.println( " report height  " + getBounds().height );
      System.out.println( " paper  width  " + pf.getImageableWidth() );
        System.out.println( " report width " + getBounds().width );

        double yscale = (double) (pf.getImageableHeight( ) / (double)getBounds( ).height),
               xscale = (double) (pf.getImageableWidth( ) / (double)getBounds( ).width);
        // we do not want to enlarge the height of the report
        if( yscale > 1.0 )
        {      yscale = 1.0;
        }
        System.out.println( "Scale on x with " + xscale + " and on y with " + yscale );
        g2.scale( xscale, yscale );
        paint(g2);
        return PAGE_EXISTS;
    }
}
///////////////////////////////////////////////
0
 
LVL 1

Author Comment

by:sey7
ID: 2821542
I know how to print circles, plain text, but I have problems only when I want to print "the content of a JTextPane that contain HTML".
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 1

Author Comment

by:sey7
ID: 2821650
The solution is nice, but one of my problems remains. I can print only the first page of the HTML content.
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2821681
in other idea:if you have the content you must parse it and paint it item by item ...

and other thing:
did you tried the code that I've sent to you?

0
 
LVL 1

Author Comment

by:sey7
ID: 2821717
The solution is nice, but one of my problems remains. I can print only the first page of the HTML content.
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2821812
You are right.
"The Printable interface is implemented by the print methods of the current page painter, which is called by the printing systemto render a page. When building a Pageable, pairs of PageFormat instances and instances that implement this interface are used to describe each page. The instance implementing Printable is called to print the page's graphics. "
So it seems you must implement Pageable interface.

0
 
LVL 1

Author Comment

by:sey7
ID: 2821830
The solution is nice, but one of my problems remains. I can print only the first page of the HTML content.
0
 
LVL 4

Accepted Solution

by:
kylar earned 300 total points
ID: 2822343
Are you intentionally repeating yourself? This is a PrintableEditorPane that I implemented in a previous project. I set it to an HTMLEditorKit, loaded in a template, and am able to print it. Hope this helps.

public class JPRIPrintableEditPane extends JEditorPane
                           implements Printable, Serializable {

                     
//~~~~~~~~~~~~~~~~ IMPLEMENTATION OF KeyListener INTERFACE ~~~~~~~~~~~~~~~~~

 /**
  * 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) {}
   }

}
Then call doPrintActions when you want to print it.

Kylar
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

710 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