我找到了一个Java打印示例here。
不幸的是,尝试执行我得到了一个异常
Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Unknown Source)
at application.UsePrintingServiceInJava.main(UsePrintingServiceInJava.java:55)
我还尝试使用DocFlavor.INPUT_STREAM.PNG
味道而不是pdf打印png,这很好用。打印机本身可能有问题,还是我必须更改代码?
此外,如果您有更好的解决方案用Java打印PDF文件,请告诉我。
答案 0 :(得分:1)
最后,Apache PDFBox解决了我的问题。我下载了pdfbox-app-2.0.4.jar并将其添加到构建路径中。 现在执行以下代码打印所需的.pdf文件:
class Segment:
labels = ['AM', 'IP', 'PM', 'OP']
def __init__(self, segid):
self.id = segid
self.values = [ [ 0, 0 ] for i in range(4) ]
def add(self, hour, indic):
ix = 3
if hour >= 6 and hour < 10: ix=0
elif hour >= 10 and hour < 16: ix=1
elif hour >= 16 and hour < 19: ix = 2
self.values[ix][indic] += 1
def percent(self, ix):
try:
return int(.5 + (100 * self.values[ix][1] /
(self.values[ix][0] + self.values[ix][1])))
except ZeroDivisionError:
return 0
dummy = next(fd)
splitter = re.compile(' +')
segments = dict()
for line in fd: # read and store
d, seg, indic = splitter.split(line.strip()) # could be replaced with a csv reader
hour = int(d[11:13])
if not seg in segments:
segments[seg] = Segment(seg)
segments[seg].add(hour, int(indic))
for seg in sorted(segments.keys()): # output the stats
for ix in range(4):
print(seg, Segment.labels[ix], segments[seg].percent(ix))
即使它没有显示打印对话框,我认为如果您只想打印文件,这是一个很好的解决方案。