473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 21492
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

printableEditorPane jEditorPane = new printableEditorPane();

jEditorPane.setContentType("text/html");
jEditorPane.read(new BufferedReader(...), ""); /* Read your HTML File
*/

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(jEditorPane);
if (job.printDialog()) /* Displays the standard system print dialog
*/
{
try
{
job.print();
}
catch (Exception ex)
{
System.out.println(ex);
}
}

// End
public class printableEditorPane extends JEditorPane implements
Printable, Serializable
{
public int print (Graphics g, PageFormat pf, int pageIndex) throws
PrinterException
{
Graphics2D g2 = (Graphics2D)g;
g2.setColor (Color.black);

RepaintManager.currentManager(this).setDoubleBuffe ringEnabled(false);
Dimension d = this.getSize();
double panelWidth = d.width;
double panelHeight = d.height;
double pageWidth = pf.getImageableWidth();
double pageHeight = pf.getImageableHeight();
double scale = pageWidth / panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight /
pageHeight);

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

g2.translate(pf.getImageableX(), pf.getImageableY());
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
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
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...
3
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...
21
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...
1
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...
22
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...
2
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"...
0
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.