About the two days to study the Java Print PDF method record

Source: Internet
Author: User
Tags print format

These two days in the study of Java Call printer print PDF file method, learned a lot of things, especially to record.

On the Java print Web The most and also Java-Orthodox printing method is to use Printservice, a set of standard printed code such as the following:

public class Printdemo {public static void main (string[] args) {JFileChooser filechooser = new JFileChooser ();//create print job in T state = Filechooser.showopendialog (null), if (state = = jfilechooser.approve_option) {File File = Filechooser.getselectedfile (); Gets the selected file//Build Print Request Property Set Hashprintrequestattributeset Pras = new Hashprintrequestattributeset ();//sets the print format, because the type is not determined. So choose autosensedocflavor Flavor = Docflavor.input_stream. autosense;//Find all available Print Services Printservice printservice[] = printservicelookup.lookupprintservices (flavor, Pras);// Locate the default print service Printservice Defaultservice = Printservicelookup.lookupdefaultprintservice ();//Display the Print dialog box Printservice Service = Serviceui.printdialog (null, $, 200,printservice, Defaultservice, Flavor, Pras); if (service! = null) {try {DOCP Rintjob job = Service.createprintjob (); Create a print job FileInputStream fis = new FileInputStream (file); Construct the file stream to be printed docattributeset das = new Hashdocattributeset ();D oc Doc = new Simpledoc (FIS, Flavor, das); Job.print (Doc, Pras) ;} catch (Exception E{E.printstacktrace ();}}}}} 

But after trying to find that the PDF file is not printed in PDF format, but print out garbled (that is, the file binary code)

The initial conjecture is that the printer is not recognized. So the above Docflavor changed to Docflavor.input_stream. PDF;

Then printservice[] will be null, why? Because the Lookupdefaultprintservice () method only returns printers that support Docflavor ~

If the printer is not filtered by Docflavor. While printing directly. Will error: Invalid flavor, that is, does not support the type ~

In fact, we look at Docflavor. It is actually the content-type of the data stream sent to the printer, similar to the web, which is not supported by the Content-type. Then it's easier to understand.

The explanations later found on the Internet are:

The Java Print Service API natively supports printing, but it does not have the ability to interpret PDF files, which means that PDF files can be printed with the standard Java Print API, but the printers used must support the PDF printing standard, i.e. the printer supports post Script standard, and such printers are often expensive, and inexpensive printers circulating on the market today (using host-based drivers) are not supported


What about that? Don't we have a way to print a PDF file from a Java program? Whether or not, at least to try it ~

There is no particularly good way to find a solution online.

Probably collected several kinds of:

1, the more lazy method, the plug-in program to achieve printing, the most typical is Adobe Reader

2, using Pdfrenderer and other third-party libraries to convert the PDF file into a picture, then you can call Printservice to print


Let's say the first way.

First, you have to install Adobe Reader on your computer and call Adobe Reader to print a command that relies on acrord32 .

We know that in Java to invoke the command line requires a runtime object, the detailed code such as the following:

public static Boolean printpdf (String pdfpath) {try{runtime.getruntime (). EXEC ("cmd.exe/c start acrord32/p/h" + Pdfpath) ; return true;} catch (Exception e) {e.printstacktrace (); return false;}}

The Acrord32.exe method of Adobe Reader is called here to describe the parameters of this command.

Acrord32.exe filename-run Adobe Reader, and browse PDF

Other supported references are:

acrord32.exe/p filename-run Adobe Reader and print a PDF file

acrord32.exe/t path PrinterName drivername portname

Initialize Adobe Reader without ejecting the Print dialog box. Then end printing

The four number of parameters required for the/t command is

Path-Print file paths

PrinterName-Printer Name

drivername-Printer driver name

PortName-Printer Port

Other references:

/n open a new Adobe Reader, although Adobe Reader is turned on (silent printing is not necessary)

/s Open an Adobe reader does not open splash screen

/o open an Adobe reader without opening the Open File dialog box

/h to minimize the opening of an Adobe Reader (silent print usage)

About the two days to study the Java Print PDF method record

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.