java jasper报表打印自定义纸张的问题,求大神指教,卡了2天了。

maokai08 2015-10-16 06:21:42
现在是一台电脑连接了2台打印机,一个打印单据,一个打印标签,2个是不同的模版。如果2个打印都不预览,1个使用默认打印机,一个指定打印机,第2个打印出来的纸张的规格就是取的第一个的设置。如果都使用预览出来,然后手动选择打印机打印出来的都没问题.只有使用默认打印机的是没问题的。
现在是需要设定纸张的大小,由于不是国标,自定义纸张,但是都是说高度要大于宽度。
有没有遇到此问题已经解决的,没有很多的分数,求指教!!!!!。
以下附部分代码:

/**
* 按指定打印机打印
* @param jasperPrint
*/
public static boolean directPrintByPrintName(final JasperPrint jasperPrint,String printer) {
if (jasperPrint != null) {
try {
if (checkPrinter(printer)) {
long start = System.currentTimeMillis();

final MediaPrintableArea area = new MediaPrintableArea(
0,
0,
(int) ((jasperPrint.getPageWidth()) * 0.353),// 将像素px转化成毫米mm,算法有待改进
(int) ((jasperPrint.getPageHeight()) * 0.353),
Size2DSyntax.MM);

PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(area);

PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();

printServiceAttributeSet.add(new PrinterName(
printer, null));

final JRPrintServiceExporter exporter = new JRPrintServiceExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
exporter
.setParameter(
JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
printRequestAttributeSet);
exporter
.setParameter(
JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
printServiceAttributeSet);
exporter
.setParameter(
JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
Boolean.FALSE);
exporter
.setParameter(
JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
Boolean.FALSE);

Thread thread = new Thread(new Runnable() {
public void run() {
try {
exporter.exportReport();
} catch (Exception ex) {
System.err.println(ex.getLocalizedMessage());
}
}
});
thread.start();

System.err.println("Printing time : "
+ (System.currentTimeMillis() - start));
}else{
Thread thread = new Thread(new Runnable() {
public void run() {
try {
JasperPrintManager.printReport(jasperPrint, false);
} catch (Exception ex) {
}
}
});
thread.start();
}
} catch (Exception ex) {
System.err.println(ex.getLocalizedMessage());
return false;
}

}
return true;

}
...全文
411 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
maokai08 2015-10-16
  • 打赏
  • 举报
回复
其中设置MediaPrintableArea,没有效果。

67,543

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧