• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing Problem to print header  RSS feed

rajeshraj gupta
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to implement for print header and footer.this is running for multiple pages.

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
import javax.print.PrintService;
import javax.print.attribute.PrintRequestAttributeSet;
import java.text.MessageFormat;
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
private static PageFormat p1;
public static void printComponent(Component c) {
// c.setFont(new Font("verdana",Font.PLAIN,8));
new PrintUtilities(c).print();

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


public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
MessageFormat footer = new MessageFormat("Page - {0}");
p1= printJob.defaultPage();

//Font f=new Font("verdana",1,10);

if (printJob.printDialog())
try {
} 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
//System.out.println("pageHeight="+pageHeight +"panelHeight="+panelHeight);
double pageWidth = pf.getImageableWidth(); //width of printer page

double scale = pageWidth / panelWidth;
int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);
g2.drawString("Page"+ (pageIndex+1),(int)pageWidth/2,(int)pageHeight+50);

// 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()+5);

// 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);
// System.out.print("scale="+scale);
componentToBePrinted.setFont(new Font("verdana",Font.PLAIN,8));

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

response = Printable.PAGE_EXISTS;

return response;

/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
public static void disableDoubleBuffering(Component c) {
//c.setFont(new Font("verdana",Font.PLAIN,9));
RepaintManager currentManager = RepaintManager.currentManager(c);
public static void pageSetup(Component c)
PrinterJob printJob = PrinterJob.getPrinterJob();
if (p1 == null)
p1 = printJob.defaultPage();
p1 = printJob.pageDialog(p1);

/** Re-enables double buffering globally. */

public static void enableDoubleBuffering(Component c) {
//c.setFont(new Font("verdana",Font.PLAIN,8));
RepaintManager currentManager = RepaintManager.currentManager(c);
[ March 04, 2008: Message edited by: rajeshraj gupta ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!