从 JAVA 类将 RTF 文件直接后台打印到 Lipi 打印机
我的服务器上有 RTF 文件,我想使用 JAVA 程序直接打印该文件。 我尝试了以下代码(考虑只有一台打印机(LIPI)连接到服务器)
FileInputStream psStream = null;
try {
psStream = new FileInputStream("C://SampleBoard2.rtf");
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++){
String svcName = services[i].toString();
myPrinter = services[i];
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
文件被假脱机到打印机并且打印也开始,但打印机打印 thge RTF 文件的内容,就像 <文本文件内容。应该如何将文件渲染为 RTF 然后打印?
我在印刷品中看到类似这样的内容。
\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\froman\fcharset0 次新 罗马;}{\f1\froman\fcharset0 快递;}{\f2\froman\fcharset0 Arial;}{\f3\froman\fcharset0 未知;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}{\stylesheet {\style\s0 \ql\fi0\li0\ri0\f2\fs24\cf0 正常;}{\style\s3 \ql\fi0\li0\ri0\f2\fs26\b\cf0 标题 3;}{\style\s2 \ql\fi0\li0\ri0\f2\fs28\b\i\cf0 标题 2;}{\style\s1 \ql\fi0\li0\ri0\f2\fs32\b\cf0 标题 1;}} ..... .... ...
…………
我哪里出错了?
I have RTF file on the sever I want directly take the printout of the file using a JAVA Program.
I tried the following code (Consider only one printer(LIPI) is connected to server)
FileInputStream psStream = null;
try {
psStream = new FileInputStream("C://SampleBoard2.rtf");
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++){
String svcName = services[i].toString();
myPrinter = services[i];
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
The file gets spooled to the printer and print also starts, but the printer prints the contents of thge RTF file like a text file contents. What should be done to render the file to RTF and then print?
I get something like this in the print.
\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\froman\fcharset0 Times New
Roman;}{\f1\froman\fcharset0 Courier;}{\f2\froman\fcharset0
Arial;}{\f3\froman\fcharset0
unknown;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}{\stylesheet
{\style\s0 \ql\fi0\li0\ri0\f2\fs24\cf0 Normal;}{\style\s3
\ql\fi0\li0\ri0\f2\fs26\b\cf0 heading 3;}{\style\s2
\ql\fi0\li0\ri0\f2\fs28\b\i\cf0 heading 2;}{\style\s1
\ql\fi0\li0\ri0\f2\fs32\b\cf0 heading 1;}} ..... .... ....... ... ...
Where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃,生意太糟糕了。最简单的方法是调用外部程序(如写字板或 openoffice)直接打印文件 - 如果您调用 write.exe /p filename.rtf ,它将弹出打印对话框。 OpenOffice 应该与 Java 有一个桥梁,您可能需要研究一下
javax.activation
。Ugh, nasty business. The easiest way would be to invoke an external program like wordpad or openoffice to directly print the file - f.e. if you invoke
write.exe /p filename.rtf
it will pop up the printing dialogue. OpenOffice is supposed to have a bridge with Java, and you might want to look intojavax.activation
.