0
votes

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?

1
You need an RTF rendering engine in the same way that you need to render HTML pages before printing. - Thorbjørn Ravn Andersen
@Thorbjørn Ravn Andersen: Can you suggest a RTF rendering engine for JAVA and where can I get one and how to place it between the printer and the class... - Sangeet Menon
No. I do not have experience with RTF engines. Be prepared to pay money though. - Thorbjørn Ravn Andersen
ohhh...thats not very encouraging... - Sangeet Menon

1 Answers

0
votes

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.