从 JAVA 类将 RTF 文件直接后台打印到 Lipi 打印机

发布于 2024-11-28 05:49:20 字数 1825 浏览 1 评论 0原文

我的服务器上有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-12-05 05:49:20

呃,生意太糟糕了。最简单的方法是调用外部程序(如写字板或 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 into javax.activation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文