Solved

JTextPane with HTML printing!

Posted on 2000-05-18
9
1,283 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
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

623 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