CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2000
    Location
    India
    Posts
    6

    Printing a html page through Swings

    I have generated a html page in a servlet and want to print it through Swings. The servlet where the generation of html page happens is a child servlet being called from within a loop of a parent servlet that is automatically activated by Jrun and NOT CALLED BY A CLIENT BROWSER.

    kris


  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Printing a html page through Swings


    If it's a simple HTML file ( Text only with Styles ) , you can open it in a hidden JTextPane ( or
    JEditorPane ) and print this component.


  3. #3
    Join Date
    Dec 1999
    Location
    surfing net
    Posts
    9

    Re: Printing a html page through Swings

    I am having the problem of printing an HTML page displayed on a JEditorPane which is more than a single page. Here is my code . Please correct it . Thank you inadvance.



    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.print.*;
    import java.awt.geom.*;
    import javax.swing.text.html.*;
    import java.util.*;

    public class HTMLBrowser extends JFrame {
    final JEditorPane jep = new JEditorPane();
    final JScrollPane jsp = new JScrollPane(jep);
    public HTMLBrowser(String startingUrl) {
    super("Test Pane");
    setSize(600,700);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
    we.getWindow().setVisible(false);
    System.exit(0);
    }});
    JPanel urlPanel = new JPanel();
    urlPanel.setLayout(new BorderLayout());
    JTextField urlField = new JTextField(startingUrl);
    urlPanel.add(new JLabel("Site: "), BorderLayout.WEST);
    urlPanel.add(urlField, BorderLayout.CENTER);
    final JLabel statusBar = new JLabel(" ");
    jep.setEditable(false);
    // force the pane to use our new editor kit
    jep.setEditorKitForContentType("text/html", new HTMLEditorKit());
    try {
    jep.setPage(startingUrl);
    } catch(Exception e) {statusBar.setText("Could not open starting page. Using a blank.");}
    JButton btnPrint = new JButton("Print");
    btnPrint.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    PrinterJob printJob = PrinterJob.getPrinterJob();
    PageFormat aPageFormat = printJob.defaultPage();
    printJob.setPageable(prepareBook(aPageFormat));
    if (printJob.printDialog()) {
    try {
    printJob.print();
    } catch (Exception ex) {ex.printStackTrace();}
    }
    }
    });
    getContentPane().add(jsp, BorderLayout.CENTER);
    getContentPane().add(urlPanel, BorderLayout.NORTH);
    getContentPane().add(btnPrint, BorderLayout.SOUTH);
    urlField.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    String urlText = null;
    try {
    if(ae.getSource() instanceof JTextField){
    urlText = ((JTextField)ae.getSource()).getText();
    }
    jep.setPage(urlText);
    }
    catch(Exception e) {statusBar.setText("Could not open starting page. Using a blank.");}
    }
    });
    }


    public Book prepareBook(PageFormat thePageFormat){
    Book aBook = new Book();
    PrintReport aPrintReport = new PrintReport();
    int aPageCount = aPrintReport.getPageCount(thePageFormat);
    aBook.append(aPrintReport, thePageFormat, aPageCount);
    return aBook;
    }

    class PrintReport implements Printable{
    public int print(Graphics g, PageFormat thePageFormat, int theIndex){
    Graphics2D g2d = (Graphics2D)g;
    if(theIndex >= getPageCount(thePageFormat)) return Printable.NO_SUCH_PAGE;
    drawPage(g2d, thePageFormat, theIndex);
    return Printable.PAGE_EXISTS;
    }
    public void drawPage(Graphics2D g2d, PageFormat thePageFormat, int theIndex){
    Shape aClipShape = g2d.getClip();
    Rectangle2D.Double rectangle = new Rectangle2D.Double(0,theIndex*thePageFormat.getImageableHeight(), thePageFormat.getImageableWidth(), thePageFormat.getImageableHeight());
    g2d.setClip(rectangle);
    layoutPage(g2d, thePageFormat);
    jep.paint(g2d);
    g2d.setClip(aClipShape);
    }
    public void layoutPage(Graphics2D g2d, PageFormat thePageFormat){
    Dimension dim = jep.getSize();
    double scaleFactor = (double)thePageFormat.getImageableWidth()/dim.width;
    g2d.scale(scaleFactor, 1);
    g2d.translate(thePageFormat.getImageableX(), thePageFormat.getImageableY());
    }
    public int getPageCount(PageFormat thePageFormat){
    Dimension dim = jep.getSize();
    RepaintManager manager = RepaintManager.currentManager(jep);
    final Image htmlImage = manager.getOffscreenBuffer(jep, dim.width, dim.height);
    return (int)Math.ceil(htmlImage.getHeight(null)/thePageFormat.getImageableHeight());
    }
    }


    public static void main(String args[]) {
    (new HTMLBrowser(args.length > 0
    ? args[0]
    :"file:///C:/WINNT/Profiles/nag/Desktop/Browser/HTMLReport.html")).setVisible(true);
    }
    }








  4. #4
    Join Date
    Sep 2000
    Location
    Chennai - India
    Posts
    98

    Re: Printing a html page through Swings

    Hi,

    If you don't mind can you tell me how to call a applet(it contains some swing components) from html page. I have used <applet code="Test.class" widhth=700 height=700></applet>, but it is showing some error in the browser statsu bar ie java.lang.ClassNotfoundExecption.

    Some my friends adviced me to install HTMLConverter & Plugin. I don't how to use the HTMLConverter.

    So can u tell how to call a applet from html page?

    Thanx in advance.

    BFN

    VTEE


  5. #5
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Printing a html page through Swings


    Look at the following site. You will get the answer for your question

    http://manning.spindoczine.com/sbe/f.../Chapter22.htm



  6. #6
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Printing a html page through Swings


    Hi,

    You are asking this question again and again. I told a step by step instruction
    in the following post.

    http://codeguru.earthweb.com/bbs/wt/...age=0&Limit=25

    If you dont understand, or if you still have problems, please explain it clearly.
    Do not just say "I don't how to use the HTMLConverter". I really dont think HTMLConverter
    is that much complicated one to use.

    Could you please tell me what did you do after you downloaded the HTMLConverter ?
    Dont worry about Java plugin now. Just tell me what did you do after you downloaded
    the zip file from the sun site.

    Poochi...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured