Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes Font Problem in Java Print API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Font Problem in Java Print API" Watch "Font Problem in Java Print API" New topic

Font Problem in Java Print API

as chhipa

Joined: Sep 04, 2006
Posts: 26
I'm trying to print contents of notepad file. For this I have filled the JTextArea with the contents of File. The font type of both JtextArea and notepad file is Courier New and Size Font Size is 14.I am passing the JTextArea to the printComponent method.
My problem is that when I try to print the notepad files which I receive from other system automatically the font size issmallest(probably 2 inch).
But now I make the same file manully and try to print then there is no problem. I can not understand why there is font-descripancy.

One more thing If I use Times New Roman, there is no problem. But I can't use that because it misalign the contents of the auto-generated file.

Following is my program,

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class PrintUtilities implements Printable {
private Component componentToBePrinted;

public static void printComponent(Component c) {
new PrintUtilities(c).print();

public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;

public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
if (printJob.printDialog())
try {
System.out.println("Calling PrintJob.print()");
System.out.println("End PrintJob.print()");
catch (PrinterException pe) {
System.out.println("Error printing: " + pe);

public int print(Graphics g, PageFormat pf, int pageIndex) {
int response = NO_SUCH_PAGE;

Graphics2D g2 = (Graphics2D) g;

// for faster printing, turn off double buffering

Dimension d = componentToBePrinted.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) {
response = NO_SUCH_PAGE;
else {

// 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);

componentToBePrinted.paint(g2); //repaint the page for printing

response = Printable.PAGE_EXISTS;
return response;

public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Why you posted this n the JSP forum is a mystery to me. Please take the time to choose an appropriate forum for your questions.

[Asking smart questions] [About Bear] [Books by Bear]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I know zip about printing, but Tom Jacobs' StandardPrint class works well

Run it (you need to comment out the references to SpecialPrint first) and if
it doesn't have the same problem, look for differences to your code.
I agree. Here's the link:
subject: Font Problem in Java Print API
It's not a secret anymore!