2013年9月17日星期二

java print page margin settings

BufferedImage bufferedimage1 = new BufferedImage (barcode1.getSize (). Width, barcode1.getSize (). Height, 1);
java.awt.Graphics2D graphics2d1 = bufferedimage1.createGraphics ();
barcode1.paint (graphics2d1);
JPEGImageEncoder jpegimageencoder = JPEGCodec.createJPEGEncoder (servletoutputstream);
JPEGEncodeParam jpegencodeparam = jpegimageencoder.getDefaultJPEGEncodeParam (bufferedimage1);
jpegencodeparam.setQuality (1.0F, true);
jpegimageencoder.setJPEGEncodeParam (jpegencodeparam);
jpegimageencoder.encode (bufferedimage1, jpegencodeparam);

/ / Print
Graphics g = bufferedimage1.createGraphics ();
g.drawString ("", 0, 0);
ByteArrayOutputStream outstream = new ByteArrayOutputStream ();
ImageIO.write (bufferedimage1, "jpg", outstream);
byte [] buf = outstream.toByteArray ();
InputStream stream = new ByteArrayInputStream (buf);
PrintService se = PrintServiceLookup.lookupDefaultPrintService ();
try {
DocPrintJob job = se.createPrintJob ();
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
Doc doc = new SimpleDoc (stream, flavor, null);
job.print (doc, null);
} catch (Exception ex) {
System.out.print (ex.getMessage ());
}
------ Solution ----------------------------------- ---------
BufferedImage bufferedimage1 = new BufferedImage (barcode1.getSize (). width, barcode1.getSize (). height, 1);
The barcode1.getSize (). width
barcode1.getSize (). height Try
into 1000
BufferedImage bufferedimage1 = new BufferedImage (1000,1000, 1);
------ Solution ---------------- ----------------------------
my barcode image printing function also has margin settings , barcode paper already small , margins accounted for half of the barcode paper , how to set the header and left margin doc


/ **
* Print Image
* /
package com.twl.core.framework.printUtil;

import java.io. *;
import javax.print. *;
import javax.print.attribute. *;
import javax.print.attribute.standard. *;

/ **
* @ package name com.twl.core.framework.printUtil
* @ file name PrintImage.java
* @ author xiexiaoyi
* @ creation date May 3, 2012
* @ version V 1.0
* /
public class PrintImage {
public PrintImage (String filename) {
try {
DocFlavor flavor = null;
if (filename.endsWith (". jpeg")) {
flavor = DocFlavor.INPUT_STREAM.JPEG;
} else if (filename.endsWith (". gif")) {
flavor = DocFlavor.INPUT_STREAM.GIF;
} else if (filename.endsWith (". png")) {
flavor = DocFlavor.INPUT_STREAM.PNG;
} else {
flavor = DocFlavor.INPUT_STREAM.JPEG;
}
/ / get print properties
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet ();
pras.add (new Copies (1));
/ / portrait printing
pras.add (OrientationRequested.PORTRAIT) ;/ / set vertical play
/ / Set Print Area Size
MediaPrintableArea mp = new MediaPrintableArea (0f, 0f, 100f, 40f,
Size2DSyntax.MM) ;/ / 5f, 5f, 100f, 40f
pras.add (mp);
/ / set the paper size
/ / MediaSizeExp to rewrite code MediaSize
MediaSize ms = new MediaSize (100, 110f, Size2DSyntax.MM,
MediaSizeName.INVOICE) ;/ / 100, 110f / / MediaSizeName.INVOICE invoice
pras.add (ms.getMediaSizeName ());
/ / find all available print services
/ / PrintService printService [] =
/ / PrintServiceLookup.lookupPrintServices (flavor, pras);
PrintService defaultService = PrintServiceLookup
. lookupDefaultPrintService ();

/ / display the Print dialog box
/ / PrintService service = ServiceUI.printDialog (null, 200, 200,
/ / printService, defaultService, flavor, pras);
/ / get the print job
DocPrintJob job = defaultService.createPrintJob () ;/ / service.createPrintJob ()
FileInputStream fin = new FileInputStream (filename);
/ / barcode paper attributes Sides

/ / Sides sides = new Sides ();
DocAttributeSet docSet = new HashDocAttributeSet () ;/ / doc attribute
Doc doc = new SimpleDoc (fin, flavor, docSet) ;/ / null
job.print (doc, pras);
fin.close ();
} catch (IOException ie) {
ie.printStackTrace ();
} catch (PrintException pe) {
pe.printStackTrace ();
}
}
}


------ For reference only ---------------------------------- -----
printed page margin settings is not required to print barcodes , 60 * 5 , but the margins are too big to play out a blank barcode printers , content is playing out normal printer so it feels is centered page margins problem
------ For reference only ------------ ---------------------------
not ah now mainly to print the contents of the display to sit corner can be. . left = 0 top = 0

Daniel who quickly help
------ For reference only ---------------------------- -----------
4 floor rewritten MediaSize ask how can send a copy of the source code to turn over ?

没有评论:

发表评论