日期:2014-05-20  浏览次数:20901 次

java打印页面边距设置
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);
   
// 打印 
  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());
}

------解决方案--------------------
BufferedImage bufferedimage1 = new BufferedImage(barcode1.getSize().width, barcode1.getSize().height, 1);
把barcode1.getSize().width
barcode1.getSize().height
改成1000试试
BufferedImage bufferedimage1 = new BufferedImage(1000,1000, 1);
------解决方案--------------------
我的条形码的图片打印功能也有边距设置,条码纸本来就很小,边距就占了半个条码纸,如何设置doc的页眉和左边距


/**
 * 打印图片
 */
package com.twl.core.framework.printUtil;

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

/**
 * @包名 com.twl.core.framework.printUtil
 * @文件名 PrintImage.java
 * @作者 xiexiaoyi
 * @创建日期 May 3, 2012
 * @版本 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;
}
// 获得打印属性
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
// 纵向打印
pras.add(OrientationRequested.PORTRAIT);// 设置成竖打
// 设定打印区域大小
MediaPrintableArea mp = new MediaPrintableArea(0f, 0f, 100f, 40f,
Size2DSyntax.MM);// 5f, 5f, 100f, 40f
pras.add(mp);
// 设定纸张大小
// MediaSizeExp 为重写MediaSize代码
MediaSize ms = new MediaSize(100, 110f, Size2DSyntax.MM,
MediaSizeName.INVOICE);// 100, 110f//MediaSizeName.INVOICE 发票
pras.add(ms.getMediaSizeName());
// 查找所有的可用打印服务
// PrintService printService[] =
// PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup
.lookupDefaultPrintService();

// 显示打印对话框
// PrintService service = ServiceUI.printDialog(null, 200, 200,
// printService , defaultService, flavor, pras);
// 获得打印工作
DocPrintJob job = defaultService.createPrintJob();// service.createPrintJob()