Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JTextPane with HTML printing!

Posted on 2000-05-18
9
Medium Priority
?
1,291 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.
Suggested Courses

916 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