尝试使用Java打印PDF文件时遇到问题.这是我的代码:
PdfReader readFtp = new PdfReader(); // This class is used for reading a PDF file
PDDocument document = readFtp.readFTPFile(documentID);
printRequestAttributeSet.add(new PageRanges(1, 10));
job.setPageable(document);
job.print(printRequestAttributeSet); // calling for print
document.close()
Run Code Online (Sandbox Code Playgroud)
我使用document.silentPrint(job);
和job.print(printRequestAttributeSet);
- 它工作正常.如果我使用document.silentPrint(job);
- 我无法设置PrintRequestAttributeSet
.
谁能告诉我如何设置PrintRequestAttributeSet?
我们最近收购了兄弟QL-700打印机,我们正在通过这台机器进行贴纸打印.
我们喂食的纸是62毫米宽的贴纸卷,没有"长度"限制.
问题是,不管我怎么做(我试过Book
,PrintRequestAttributeSet
),我无法使用Java来告诉打印机对话窗口使用正确的纸张尺寸打印机.我无法做到,例如,我们需要的确切的62毫米×40毫米.它总是"按下"到最近的纸张:
这是有问题的代码:
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
Paper p = pf.getPaper();
p.setSize(UnitConv.mm2pt(62), UnitConv.mm2pt(40));
p.setImageableArea(0, 0, UnitConv.mm2pt(62), UnitConv.mm2pt(40));
pf.setPaper(p);
pf.setOrientation(PageFormat.LANDSCAPE);
job.setPrintable(this, pf);
if (job.printDialog()) {
try {
job.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我可以确认打印机可以随意打印,如下面的屏幕截图所示(使用Brother的P-touch编辑器).请注意,虽然它是可调整的,但是36mm
由软件本身预设:
所以问题:
如何强制length
纸张的" "精确到40mm?
相关: 标签打印机的自定义纸张尺寸(Brother QL 570)
编辑
我做了一个媒体大小查询(代码),这里是它可以支持的媒体列表:
17 mm x 54 mm: width = 0.67; height = 2.12
17 mm x 87 mm: …
Run Code Online (Sandbox Code Playgroud) 我需要对我的打印机有更多的控制权,然后我试图获得打印机的PrinterState,然后使用PrintStareReasons.我的代码如下:
public void checkPrinterStatus(){
try {
logger.info("Check -------------- ");
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintService printer = configParamPrintService.getPrintService();
logger.info("State " + printer.isAttributeCategorySupported(PrinterState.class));
Set<Attribute> attributes = getAttributes(printer);
for(Attribute attr : attributes){
logger.info(attr.getName());
}
}
public static Set<Attribute> getAttributes(PrintService printer) {
Set<Attribute> set = new LinkedHashSet<Attribute>();
//get the supported docflavors, categories and attributes
Class<? extends Attribute>[] categories = (Class<? extends Attribute>[]) printer.getSupportedAttributeCategories();
DocFlavor[] flavors = printer.getSupportedDocFlavors();
AttributeSet attributes = printer.getAttributes();
//get all the avaliable …
Run Code Online (Sandbox Code Playgroud)