用Java打印Microsoft Office和PDF文件

时间:2011-06-02 04:38:30

标签: java api pdf printing ms-office

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

5 个答案:

答案 0 :(得分: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 );
    }
}

答案 1 :(得分:1)

看看OpenOffice API,有一些printing examples provided。 OpenOffice可以打开MS Office文档,但给定的API非常有限。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

您可以在java中使用struts的display-table标签将数据导出为xls,pdf,cvs格式,然后也可以打印。

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

访问:

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

答案 4 :(得分:0)

尝试使用Apache POI for Microsoft docs http://poi.apache.org/

有关打印设置,请参阅此网址http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFPrintSetup.html