473,657 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
1 21518
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;
}
}
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16001
by: dan | last post by:
This works in mozilla but I can't figure out why this won't work in IE. Say I have this html <html> header goes here header goes here
4
2175
by: Julie Siebel | last post by:
Apologies...I'm sure this has been asked before, but I can't seem to come up with the correct Google search terms. While my problem is with stylesheets, the errors are being caused by my javascript, so this seemed a more appropriate place to post. I'm using a media=print style sheet to do a version of a web page that I am building. Currently, I'm working on the IE version. I have an HTML page and 2 style sheets which all validate...
3
8348
by: Craig | last post by:
First of all, this one is driving me crazy, so thanks in advance for any help!! I've got a javascript function in a parent document that generates an HTML document on the fly when a button is clicked... that generated popup contains a button that's supposed to print the contents of the popup window (self). However, the button won't print (nothing happens on click) from the newly generated popup to print the contents of the window... the...
21
5724
by: Steel | last post by:
Hi at all, I have a very long html page with many photo. Therefore the best to print this page is to print the some page as PDF. Therefore I maked a PDF file like my page to print best. I'ld want that when the user press the print button in the browser , it print automatically the pdf file and not the html page. To do that I insert at the top of my page: <link media="print" rel="alternate" href="printthepage.pdf"> But it do not work and...
1
5194
by: David Templet | last post by:
I have a C# application that generates its reports in HTML. After it creates the report, it opens it in IE so it can be viewed or printed. I would like to add the ability to print the report directly from the application, but I haven't found an easy way to do this. I am probably making it overly complicated, but this is what I am doing right now: 1. Instance an "AxWebBrowser" control 2. Navigate the browser control to the report HTML...
22
130156
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the document and closes it with the following code: <body onload="window.print(); window.close();"> This works correctly (or at least the way I expect it to work under MS Internet Explorer, but it cuases Netscape to "crash"
2
2815
by: prophet | last post by:
how do I create a print button that is on my index page that prints my target page (and only the target page) I tried <INPUT onclick=window.print(); type=button value="Print This Page" style="font-size: 10px; float:right"> But it prints the index page. Thank you.
0
2365
by: thirunavukarasukm | last post by:
Hai.... To print Html page using PrintDialog and Print Document i am created one windows appication.. the windows application have many pages in my booking page i have two buttons one btnprint,btnclose
3
2928
by: rajkumarpb | last post by:
Hi friends, I am new to this forum...But not for programming...I want a Print CSS File to be added in my page..OK.... Here is the page and i want you friends to help me create the CSS File for the page i give here...Frankly said, i am a noob in CSS....Please help me....Here is my HTML Page and i want only the <div id="pageloader"> to be displayed.. <%@ page session="true" %> <%@ page import="java.util.*" %> <%@ page...
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.