Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTextPane with HTML printing!

Posted on 2000-05-18
9
Medium Priority
?
1,288 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

705 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