[Java]View Plaincopy
- Public class Printdemo {
- public static void Main (string[] args) {
- JFileChooser filechooser = new JFileChooser (); //Create a print job
- int state = Filechooser.showopendialog (null);
- if (state = = jfilechooser.approve_option) {
- File File = Filechooser.getselectedfile (); //Get the selected file
- //Build a Print Request property set
- Hashprintrequestattributeset Pras = new Hashprintrequestattributeset ();
- //Set print format because the type is not determined, so select AutoSense
- Docflavor 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 ();
- //Show Print dialog box
- Printservice service = Serviceui.printdialog (null, $ ,
- Printservice, Defaultservice, Flavor, Pras);
- if (service! = null) {
- try {
- Docprintjob job = Service.createprintjob (); //Create a print job
- FileInputStream FIS = new FileInputStream (file); //construct the file stream to be printed
- Docattributeset das = new Hashdocattributeset ();
- Doc 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 binary code of the file)
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 you do not filter the printer through Docflavor, and print directly, the error will be: Invalid flavor, that is, the type is not supported ~
In fact, we look at Docflavor, it is actually sent to the printer data flow Content-type, similar to the web, that is, do not support the Content-type, then it is more easily understood.
What about that? Don't we have a way to print a PDF file from a Java program? No matter what, at least try it ~
Discover that there is no particularly good solution on the web. Probably collected several kinds of:
1, relatively 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 need Adobe Reader on your computer, and you can call Adobe Reader to print a command that relies on acrord32 .
We know that the runtime object is required to invoke the command line in Java, with the following code:
[Java]View Plaincopy
- 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;
- }
- }
This calls the Adobe Reader's Acrord32.exe method, which describes the parameters of the command below.
Acrord32.exe filename-Execute adobe Reader and browse pdf
Other parameters that are supported are:
acrord32.exe/p filename-executes Adobe Reader and prints a PDF file
acrord32.exe/t path PrinterName drivername portname
Initialize Adobe Reader, do not eject the Print dialog box, and then end printing
The four parameters required for the/t command are
Path-Print file paths
PrinterName-Printer Name
drivername-Printer driver name
PortName-Printer Port
Other parameters:
/n opens 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)
Java Print records for PDF methods