2011-06-02 124 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和打印。