First download the final version of StandardPrint
http://onesearch.sun.com/search/clickthru?qt=StandardPrint&url=http%3A%2F%2Fforum.java.sun.com%2Fthread.jspa%3FforumID%3D57%26threadID%3D5122492&pathInfo=%2Fsearch%2Fonesearch%2Findex.jsp&hitNum=1&col=developer-forums
Make sure you also get SpecialPrint from the first post
Then create a SpecialPrint with the dimensions of your image that paints the image in the print method.
You are welcome to use and modify this code, but please do not change the package or take credit for it as your own work
hi,
finally I could make the impression, even lack some things. now I need that the impression is but fast. this it is the test code.
somebody that helps me, please.
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.print.*;
import com.sun.media.jai.codec.*;
public class JAIImagePrint implements Printable {
protected ImageDecoder dec;
protected int numPages;
private final int INCH = 72;
public static void main(String[] args)throws IOException {
JAIImagePrint sp = new JAIImagePrint();
if(args.length < 1)
System.out.println("Enter a valid image file name");
else sp.loadAndPrint(args[0]);
System.exit(0);
}
public void loadAndPrint(String filename) throws IOException {
loadTiff(filename);
//Finally, print it
print();
}
protected void loadTiff(String filename) throws IOException {
SeekableStream s = new FileSeekableStream(filename);
TIFFDecodeParam param = null;
dec = ImageCodec.createImageDecoder("tiff", s, param);
numPages = dec.getNumPages();
//System.out.println("numPages: " + numPages);
}
protected void print() {
PrinterJob printerJob = PrinterJob.getPrinterJob();
//set up a format
PageFormat pageFormat = printerJob.defaultPage();
//define the orientation
pageFormat.setOrientation(PageFormat.PORTRAIT);
//set the paper size
Paper paper = pageFormat.getPaper();
paper.setSize(8.5 * INCH, 11 * INCH);
//define the full area as imageable
//paper.setImageableArea(0 * INCH, 0 * INCH, 8.5 * INCH, 11 * INCH);
paper.setImageableArea(1, 1, 8.5 * INCH, 11 * INCH);
//set this paper as the current format
pageFormat.setPaper(paper);
printerJob.setPrintable(this, pageFormat);
//Include this statement if you need the print dialog
boolean doPrint = printerJob.printDialog();
if(doPrint) {
try{
printerJob.print();
} catch (Exception e){
System.out.println(e);
}
}
}
public int print(Graphics g, PageFormat f, int pageIndex){
RenderedImage renderedImage = null;
if(pageIndex >= numPages) return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D) g;
g2d.translate(f.getImageableX(), f.getImageableY());
try {
renderedImage = (RenderedImage)dec.decodeAsRenderedImage(pageIndex);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
if(renderedImage != null){
double g2dWidth = renderedImage.getWidth();
double g2dHeight = renderedImage.getHeight();
double pageWidth = f.getImageableWidth();
double pageHeight = f.getImageableHeight();
double scaleFactor = 1.0; // default scale factor doesn't change image size
//Before printing, scale the image appropriately.
//If the image is bigger than the page, you may to scale it to fit the paper.
if (f.getOrientation() == PageFormat.LANDSCAPE) {
scaleFactor = pageHeight / g2dHeight;
} else if (f.getOrientation() == PageFormat.PORTRAIT) {
scaleFactor = pageWidth / g2dWidth;
}
AffineTransform at = new AffineTransform();
at.translate(f.getImageableX(), f.getImageableY());
g2d.scale(scaleFactor, scaleFactor);
g2d.drawRenderedImage(renderedImage, at);
g2d.dispose();
g2d.finalize();
return Printable.PAGE_EXISTS;
} else return Printable.NO_SUCH_PAGE;
}
}