• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Font Problem in Java Print API

 
as chhipa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
System.out.println("Calling PrintJob.print()");
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
disableDoubleBuffering(componentToBePrinted);

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

enableDoubleBuffering(componentToBePrinted);
response = Printable.PAGE_EXISTS;
}
return response;
}

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

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know zip about printing, but Tom Jacobs' StandardPrint class works well

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=711572

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic