• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing Problem to print header

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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}");
if(p1==null){
p1= printJob.defaultPage();
}

printJob.setPrintable(this,p1);
//Font f=new Font("verdana",1,10);

if (printJob.printDialog())
try {
printJob.validatePage(p1);
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
//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

//enableDoubleBuffering(componentToBePrinted);
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));
//c.repaint();
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
public static void pageSetup(Component c)
{
PrinterJob printJob = PrinterJob.getPrinterJob();
if (p1 == null)
p1 = printJob.defaultPage();
p1 = printJob.pageDialog(p1);
if(p1==null){
//JOptionPane.showMessageDialog(null,""+printJob.getPrintService());
}


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

public static void enableDoubleBuffering(Component c) {
//c.setFont(new Font("verdana",Font.PLAIN,8));
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
[ March 04, 2008: Message edited by: rajeshraj gupta ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic