Print Multiple RTF Files in Java

2019-06-13 19:28发布

问题:

I have a list of RTF files downloaded from the server.

I want to print all these .rtf files at a single click without any print dialogues or only one.

Please suggest how do i do it.

I am using Aspose to print rtf files.

Please find the attached below code for the same.

import java.io.File;

import javax.print.attribute.AttributeSet;

import com.aspose.words.Document;


public class DocumentPrinter {
    public static void main(String ar[]) throws Exception{
        File folder = new File("D:\\projects\\emrs3\\PMS\\Claim\\PaperRTF");
        File[] listOfFiles = folder.listFiles();
        int j =3 ;
            for (int i = 0; i <j ; i++) {
              if (listOfFiles[i].isFile()) {
                //System.out.println("File " + listOfFiles[i].getName());
                  Document doc = new Document(listOfFiles[i].getAbsolutePath());
                  doc.print();

              } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());
              }
            }
    }
}

Problem attached with the above code is it's always asking for a print dialogue as I have put doc.print(); it in a for loop.

Is there any way I can make a list of Document and print them all at a time.

Thanks.

回答1:

I work as social media developer at Aspose. Aspose.Words supports silently printing the documents without showing the print dialog. You can use the following code to achieve this:

File folder = new File("D:\\projects\\emrs3\\PMS\\Claim\\PaperRTF");                                       
File[] listOfFiles = folder.listFiles();                                                                   
int j =3 ;                                                                                                 
for (int i = 0; i <j ; i++) {                                                                              
    if (listOfFiles[i].isFile()) {                                                                         

        //System.out.println("File " + listOfFiles[i].getName());                                          
        Document doc = new Document(listOfFiles[i].getAbsolutePath());                                     

        PrinterJob pj = PrinterJob.getPrinterJob();                                                        

        // Initialize the Print Dialog with the number of pages in the document.                           
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();                          
        attributes.add(new PageRanges(1, doc.getPageCount()));                                             

        // Create the Aspose.Words' implementation of the Java Pageable interface.                         
        AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);                           

        // Pass the document to the printer.                                                               
        pj.setPageable(awPrintDoc);                                                                        

        // Print the document with the user specified print settings.                                      
        pj.print(attributes);