2011-06-02 127 views
4

我在寻找可以打印Microsoft Office和PDF文件的Java API。我也想提供打印规格,即使在系统上没有打开这些文件的软件。商业图书馆很好。你可以推荐任何?用Java打印Microsoft Office和PDF文件

+0

您可以使用我们的产品的Gnostice PDFOne(用于Java),这里是我们的PDF印刷演示 - [如何打印PDF Document in Java](http://www.gnostice.com/nl_article.asp?id=160&t=How_to_Print_a_PDF_Document_in_Java)。 PDFOne附带免版税的商业许可证。 – BZ1 2011-06-02 05:41:26

+0

我不想要任何对话来在前面!我只想通过我的应用程序中的所有规范 – Harinder 2011-06-02 05:46:00

+0

如果您不想使用该对话框,则无需调用PDFPrinter.showPrintDialog()方法。相反,调用PdfPrinter.print(String pageRange,int numOfCopies)方法。它在最后一行中的文章中提到。 – BZ1 2011-06-02 06:01:01

回答

4

对于PDF打印这里是免费的!使用PDFBox的最佳解决方案..

import java.awt.print.PrinterJob; 

import javax.print.PrintService; 
import javax.print.PrintServiceLookup; 

import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.util.PDFTextStripper; 


public class PrintPDF 
{ 

    private static final String PASSWORD  = "-password"; 
    private static final String SILENT  = "-silentPrint"; 
    private static final String PRINTER_NAME = "-printerName"; 

    /** 
    * private constructor. 
    */ 
    private PrintPDF() 
    { 
     //static class 
    } 


    public static void main(String pdfFilepath,String printerindx) throws Exception 
    { 
     String password = ""; 
     String pdfFile = pdfFilepath; 
     boolean silentPrint = true; 

     PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();    


     if(pdfFile == null) 
     { 
      usage(); 
     } 

     PDDocument document = null; 
     try 
     { 
      document = PDDocument.load(pdfFile); 

      if(document.isEncrypted()) 
      { 
       document.decrypt(password); 
      } 
      PrinterJob printJob = PrinterJob.getPrinterJob(); 

      if(printerindx != null) 
      { 
       PrintService[] printService = PrinterJob.lookupPrintServices(); 

       printJob.setPrintService(printService[Integer.parseInt(printerindx)]); 


      } 
      txt=new PDDocument(document); 
      if(silentPrint) 
      { 


       document.silentPrint(printJob); 
      } 
      else 
      { 
       document.print(printJob); 
      } 
     } 
     finally 
     { 
      if(document != null) 
      { 
       document.close(); 
      } 
     } 
    } 

    /** 
    * This will print the usage requirements and exit. 
    */ 
    private static void usage() 
    { 
     System.err.println("Usage: java org.apache.pdfbox.PrintPDF [OPTIONS] <PDF file>\n" + 
       " -password <password>  Password to decrypt document\n" + 
       " -silentPrint     Print without prompting for printer info\n" 
     ); 
     System.exit(1); 
    } 
} 
+0

但我仍然需要一个MS Office文件的解决方案! – Harinder 2011-06-03 05:12:57

+2

首先将Office文档转换为PDF,然后打印。对于docx,请尝试docx4j;对于xlsx和旧的二进制格式,请尝试POI,或者对于非Java方法,请尝试OpenOffice。 – JasonPlutext 2011-06-03 12:16:50

0

您可以在Java中使用的支柱显示表标签,您的数据导出为XLS,PDF,CVS格式,然后可以打印。

语法非常简单,只需将您的集合提供给显示表,它将在jsp上显示集合本身的内容,如果您设置“export = true”,则可以轻松地将文件导出为pdf或xls格式。

访问这里:

http://displaytag.sourceforge.net/11/displaytag/tlddoc/display/table.html

1

Aspose拥有一套用于Word,Excel,PDF的产品。您可以导出到不同的格式,包括PDF和打印。