M
Manish
Hi,
I am new to Java Printing. can someone tell me how to set custom page
in linux using java. I tried an example. please tell me if there is
any problem.
thanx
Manish
Source
------------------------------------------------------------------------------
package com.elbiz.indama.print;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
public class Print2DtoStream implements Printable {
/**
* Constructor
*
*
* @author manish Feb 19, 2004 1:05:06 PM
* @version 1.0
*/
public Print2DtoStream() {
/* Use the pre-defined flavor for a Printable from an InputStream */
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
/* Specify the type of the output stream */
String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
/* Locate factory which can export a GIF image stream as Postscript
*/
StreamPrintServiceFactory[] factories =
StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
flavor, psMimeType);
if (factories.length == 0) {
System.err.println("No suitable factories");
System.exit(0);
}
try {
/* Create a file for the exported postscript */
FileOutputStream fos = new FileOutputStream("out.ps");
/* Create a Stream printer for Postscript */
StreamPrintService sps = factories[0].getPrintService(fos);
/* Create and call a Print Job */
DocPrintJob pj = sps.createPrintJob();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(5));
Doc doc = new SimpleDoc(this, flavor, null);
pj.print(doc, aset);
fos.close();
} catch (PrintException pe) {
System.err.println(pe);
} catch (IOException ie) {
System.err.println(ie);
}
}
public static void main(String[] args) {
new Print2DtoStream();
}
/* (non-Javadoc)
* @see java.awt.print.Printable#print(java.awt.Graphics,
java.awt.print.PageFormat, int)
*/
public int print(Graphics g, PageFormat pf, int pageIndex) throws
PrinterException {
System.out.println("\npageIndex = " + pageIndex);
if (pageIndex < 5) {
Graphics2D g2d= (Graphics2D)g;
Paper paper = new Paper();
int INCH = 72;
double width = 8 * INCH;
double height = 8 * INCH;
paper.setSize(width, height);
paper.setImageableArea(INCH, INCH, width - INCH * 2, height - INCH
* 2);
pf.setPaper(paper);
System.out.println("g2d = " + g2d.getClass());
System.out.println("X = " + pf.getImageableX() + ", Y = " +
pf.getImageableY());
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(Color.blue);
g2d.drawString("Printing Example Class", 0, 250);
g2d.fill3DRect(0, 0, (int)pf.getImageableWidth(), 200, false);
return Printable.PAGE_EXISTS;
} else {
System.out.println("\nNO SUCH PAGE");
return Printable.NO_SUCH_PAGE;
}
}
}
I am new to Java Printing. can someone tell me how to set custom page
in linux using java. I tried an example. please tell me if there is
any problem.
thanx
Manish
Source
------------------------------------------------------------------------------
package com.elbiz.indama.print;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
public class Print2DtoStream implements Printable {
/**
* Constructor
*
*
* @author manish Feb 19, 2004 1:05:06 PM
* @version 1.0
*/
public Print2DtoStream() {
/* Use the pre-defined flavor for a Printable from an InputStream */
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
/* Specify the type of the output stream */
String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
/* Locate factory which can export a GIF image stream as Postscript
*/
StreamPrintServiceFactory[] factories =
StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
flavor, psMimeType);
if (factories.length == 0) {
System.err.println("No suitable factories");
System.exit(0);
}
try {
/* Create a file for the exported postscript */
FileOutputStream fos = new FileOutputStream("out.ps");
/* Create a Stream printer for Postscript */
StreamPrintService sps = factories[0].getPrintService(fos);
/* Create and call a Print Job */
DocPrintJob pj = sps.createPrintJob();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(5));
Doc doc = new SimpleDoc(this, flavor, null);
pj.print(doc, aset);
fos.close();
} catch (PrintException pe) {
System.err.println(pe);
} catch (IOException ie) {
System.err.println(ie);
}
}
public static void main(String[] args) {
new Print2DtoStream();
}
/* (non-Javadoc)
* @see java.awt.print.Printable#print(java.awt.Graphics,
java.awt.print.PageFormat, int)
*/
public int print(Graphics g, PageFormat pf, int pageIndex) throws
PrinterException {
System.out.println("\npageIndex = " + pageIndex);
if (pageIndex < 5) {
Graphics2D g2d= (Graphics2D)g;
Paper paper = new Paper();
int INCH = 72;
double width = 8 * INCH;
double height = 8 * INCH;
paper.setSize(width, height);
paper.setImageableArea(INCH, INCH, width - INCH * 2, height - INCH
* 2);
pf.setPaper(paper);
System.out.println("g2d = " + g2d.getClass());
System.out.println("X = " + pf.getImageableX() + ", Y = " +
pf.getImageableY());
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(Color.blue);
g2d.drawString("Printing Example Class", 0, 250);
g2d.fill3DRect(0, 0, (int)pf.getImageableWidth(), 200, false);
return Printable.PAGE_EXISTS;
} else {
System.out.println("\nNO SUCH PAGE");
return Printable.NO_SUCH_PAGE;
}
}
}