【问题标题】:Print Multiple RTF Files in Java在 Java 中打印多个 RTF 文件
【发布时间】:2014-05-07 16:00:43
【问题描述】:

我有一个从服务器下载的 RTF 文件列表。

我想一键打印所有这些 .rtf 文件,没有任何打印对话框或只有一个。

请建议我该怎么做。

我正在使用 Aspose 打印 rtf 文件。

请在下面找到相同的代码。

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

上述代码附带的问题是它总是要求打印对话,因为我已将 doc.print(); 放在 for 循环中。

有什么方法可以列出Document 并一次打印出来。

谢谢。

【问题讨论】:

    标签: java printing rtf aspose aspose.words


    【解决方案1】:

    我在 Aspose 担任社交媒体开发人员。 Aspose.Words 支持静默打印文档而不显示打印对话框。您可以使用以下代码来实现:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      相关资源
      最近更新 更多