【问题标题】:Print Microsoft Office and PDF files in Java [closed]用 Java 打印 Microsoft Office 和 PDF 文件 [关闭]
【发布时间】:2011-09-06 19:34:07
【问题描述】:

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

【问题讨论】:

  • 您可以使用我们的产品 G​​nostice PDFOne(用于 Java),这里是我们的 PDF 打印演示 - How to Print a PDF Document in Java。 PDFOne 附带免版税的商业许可。
  • 我不希望任何对话出现在前面!!我只想通过我的应用程序中的所有规范
  • 如果您不想要该对话框,则无需调用 PDFPrinter.showPrintDialog() 方法。相反,调用 PdfPrinter.print(String pageRange, int numOfCopies) 方法。文章最后一行提到了。

标签: java api pdf printing ms-office


【解决方案1】:

在这里打印 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 );
    }
}

【讨论】:

  • 但是我仍然需要 MS Office 文件的解决方案!
  • 首先将 Office 文档转换为 PDF,然后打印。对于 docx,请尝试 docx4j;对于 xlsx 和旧的二进制格式,请尝试 POI,或者对于非 Java 方法,请尝试 OpenOffice。
【解决方案2】:

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

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

访问这里:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        试用 Apache POI for Microsoft 文档http://poi.apache.org/

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

        【讨论】:

          猜你喜欢
          • 2010-11-04
          • 2016-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多