简体   繁体   English

使用Java中的不同打印机打印.PDF文件

[英]Print a .PDF File using Different Printers in Java

I'm trying to write an integration for enterprise system. 我正在尝试为企业系统编写集成。

There is a web server who is being used by many clients from two different place. 有一个Web服务器正在被来自两个不同地方的许多客户使用。 There are two network printers installed on this server. 此服务器上安装了两台网络打印机。

What I want to do is to print PDF documents to these printers. 我想要做的是将PDF文档打印到这些打印机。 I want the program to send the document to the printer where it was requested. 我希望程序将文档发送到请求它的打印机。

I can determine the location where the request was made. 我可以确定请求的位置。 However I can't set the default printer on runtime. 但是,我无法在运行时设置默认打印机。

Since it's a web server that works in background I can't populate a printer dialog and let the user choose the printer. 由于它是在后台运行的Web服务器,因此我无法填充打印机对话框并让用户选择打印机。 I must be able to set the printer that will be used programmatically. 我必须能够设置将以编程方式使用的打印机。

For now, I'am able to see the registered printers on system by using PrinterJob.lookupPrintServices(); 现在,我能够通过使用PrinterJob.lookupPrintServices();在系统上看到已注册的打印机PrinterJob.lookupPrintServices(); and I can set the service with requested printer but that doesn't change the default printer and the system keeps printing on default printer. 我可以使用请求的打印机设置服务,但不会更改默认打印机,系统会继续在默认打印机上打印。

Please give me your ideas on how to achieve it. 请告诉我你如何实现它的想法。

By more researching on web I solved my problem. 通过对网络的更多研究,我解决了我的问题。 I'm giving it here for those else who might need it; 我在这里给那些可能需要它的人;

I concluded the solution from this web site: 我从这个网站得出了解决方案:

http://webmoli.com/2008/11/03/java-print-pdf/ http://webmoli.com/2008/11/03/java-print-pdf/

Note: You need to install PdfRenderer .jar library to your project to run the code given in the website: The code initially in PrintPdf.java here doesn't give my solution however the author added a method in comments section to set different printers to print on runtime. 注意:您需要将PdfRenderer .jar库安装到您的项目中以运行网站中给出的代码:最初在PrintPdf.java中的代码不提供我的解决方案但是作者在注释部分添加了一个方法来设置不同的打印机在运行时打印。

The method is: 方法是:

/**
* Sets the printer service to be used for printing
*
* @param argPrintServiceName
* @throws PrinterException
*/
public void setPrintService(String argPrintServiceName) throws PrinterException {
PrintService[] printServices = PrinterJob.lookupPrintServices();
int i;
for (i = 0; i < printServices.length; i++) {
if (printServices[i].getName().equalsIgnoreCase(argPrintServiceName)) {
printerJob.setPrintService(printServices[i]);
break;
}
}
if (i == printServices.length) {
throw new PrinterException(“Invalid print service name: ” + argPrintServiceName);
}
}

You need to make a few changes on this method. 您需要对此方法进行一些更改。 Since printerJob is not a global variable it will not effect printing. 由于printerJob不是全局变量,因此不会影响打印。 To do this set return parameter of this method to PrintService as: 为此,请将此方法的返回参数设置为PrintService:

 public static PrintService setPrintService(String argPrintServiceName) throws PrinterException {
        PrintService psr = null;
    PrintService[] printServices = PrinterJob.lookupPrintServices();
    int i;
    for (i = 0; i < printServices.length; i++) {
    if (printServices[i].getName().equalsIgnoreCase(argPrintServiceName)) {
        psr = printServices[i];          
    break;
    }
    }
    if (i == printServices.length) {
    throw new PrinterException("Invalid print service name: " + argPrintServiceName);
    }
    return psr;
    }

In main method call the method like: 在main方法中调用方法如:

PrintService ps = setPrintService("Printer Name Here");

Now, you need to send this service to other methods; 现在,您需要将此服务发送到其他方法;

Change this: 改变这个:

PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF");

to

PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF", ps);

And those are other methods that you need to change as: 这些是你需要改变的其他方法:

public PrintPdf(byte[] content, String jobName, PrintService ps) throws  
        IOException, PrinterException 
        {
        initialize(content, jobName, ps);
    } 



 public PrintPdf(InputStream inputStream, String jobName, PrintService ps)
    {
     *
     *
     initialize(pdfContent, jobName, ps);
    }

Add this line of code after assigning pjob: pjob.setPrintService(ps); 在分配pjob后添加这行代码: pjob.setPrintService(ps);

private void initialize(byte[] pdfContent, String jobName, PrintService ps) throws      
 IOException, PrinterException 
 {
  *
  *
        pjob = PrinterJob.getPrinterJob();
        pjob.setPrintService(ps);
  *
  *
  ...
 }

This code works perfectly plus it is capable to direct-pdf printing. 此代码完美无缺,并且能够直接-pdf打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM