Solved

JTextPane with HTML printing!

Posted on 2000-05-18
9
1,271 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

747 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

11 Experts available now in Live!

Get 1:1 Help Now