Print Multiple RTF Files in Java

我与影子孤独终老i 提交于 2019-12-23 05:22:23

问题


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); 


来源:https://stackoverflow.com/questions/23522735/print-multiple-rtf-files-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!