How to print an Html Document

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Myrilath

    How to print an Html Document

    I was just wondering how I should go about printing an html document. I can
    read it in and print it out as source but I would like to print it as it
    should display. At the moment I hace been having it scan the source for tags
    and set formatting based on them. The problem with this is it is long, slow
    and means I have to manually draw tables and coloured backgrounds. I know I
    am probably doing it the long hard way but the only nfo I keep finding
    doesn't seem to help me out.

    Thanks in advance

    Raymond


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.683 / Virus Database: 445 - Release Date: 12/05/2004


  • Klaus

    #2
    Re: How to print an Html Document

    Hi Raymond,

    use to display your HTML a modified JEditorPane, like in the example
    below. Create a class, which extends JEditorPane and implement the
    printable and serializable interfaces. It's very important, that you
    set
    the content type of the editor to "text/html", than read your
    HTML-document
    into the editor. For printing create a new PrinterJob and set your
    painter.
    In your case the painter is your editor. If you want to display the
    standard
    printer dialog call the printDialog() method of the PrinterJob.
    Call print() to print out yout HTML document.

    Regards,
    Klaus


    // Part of code

    printableEditor Pane jEditorPane = new printableEditor Pane();

    jEditorPane.set ContentType("te xt/html");
    jEditorPane.rea d(new BufferedReader( ...), ""); /* Read your HTML File
    */

    PrinterJob job = PrinterJob.getP rinterJob();
    job.setPrintabl e(jEditorPane);
    if (job.printDialo g()) /* Displays the standard system print dialog
    */
    {
    try
    {
    job.print();
    }
    catch (Exception ex)
    {
    System.out.prin tln(ex);
    }
    }

    // End


    public class printableEditor Pane extends JEditorPane implements
    Printable, Serializable
    {
    public int print (Graphics g, PageFormat pf, int pageIndex) throws
    PrinterExceptio n
    {
    Graphics2D g2 = (Graphics2D)g;
    g2.setColor (Color.black);

    RepaintManager. currentManager( this).setDouble BufferingEnable d(false);
    Dimension d = this.getSize();
    double panelWidth = d.width;
    double panelHeight = d.height;
    double pageWidth = pf.getImageable Width();
    double pageHeight = pf.getImageable Height();
    double scale = pageWidth / panelWidth;
    int totalNumPages = (int)Math.ceil( scale * panelHeight /
    pageHeight);

    // Check for empty pages
    if (pageIndex >= totalNumPages) return Printable.NO_SU CH_PAGE;

    g2.translate(pf .getImageableX( ), pf.getImageable Y());
    g2.translate(0f , -pageIndex * pageHeight);
    g2.scale(scale, scale);
    this.paint(g2);

    return Printable.PAGE_ EXISTS;
    }
    }

    Comment

    Working...