Java Native API has print, using print to manipulate the printer for printing operations, get printer properties, the following is the code
Print program (silent printing)
PackageCom.boci.PrintPDF;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJavax.print.Doc;ImportJavax.print.DocFlavor;ImportJavax.print.DocPrintJob;ImportJavax.print.PrintService;ImportJavax.print.PrintServiceLookup;ImportJavax.print.ServiceUI;ImportJavax.print.SimpleDoc;ImportJavax.print.attribute.Attribute;ImportJavax.print.attribute.AttributeSet;ImportJavax.print.attribute.DocAttributeSet;ImportJavax.print.attribute.HashDocAttributeSet;ImportJavax.print.attribute.HashPrintRequestAttributeSet;Importjavax.print.attribute.standard.Chromaticity;Importjavax.print.attribute.standard.Copies;Importjavax.print.attribute.standard.Finishings;ImportJavax.print.attribute.standard.MediaSizeName;Importjavax.print.attribute.standard.NumberUp;Importjavax.print.attribute.standard.OrientationRequested;Importjavax.print.attribute.standard.SheetCollate;ImportJavax.print.attribute.standard.Sides;/** * *@authorClare * @Company * *print file by Javax **/ Public classPrintpdfjavax { Public Static voidMain (string[] args) {File file=NewFile ("D:/archive/a2.pdf");//get the selected file//Build a Print request property setHashprintrequestattributeset Pras=NewHashprintrequestattributeset (); //set the print format because the type is not determined, so select AutoSenseDocflavor Flavor=Docflavor.input_stream. AutoSense; //find all of the available print servicesPrintservice printservice[]=printservicelookup.lookupprintservices (flavor, Pras); System.out.println ("-------------------All Services-------------------"); for(inti=0;i<printservice.length;i++) {System.out.println (printservice[i]); } //Locate the default print service//Printservice Service1 = Printservicelookup.lookupdefaultprintservice (); if(printservice.length>0) {System.out.println ("-------------------Choose Printer-------------------"); System.out.println (printservice[0]); //Specifies that you use Microsoft XPS Document WriterPrintservice Service = printservice[0]; //Get printer PropertiesAttributeSet attributes =service.getattributes (); for(Attribute A:attributes.toarray ()) {String name=A.getname (); String value=Attributes.get (A.getclass ()). ToString (); SYSTEM.OUT.PRINTLN (Name+ " : " +value); } //Show Print dialog box//Printservice service = Serviceui.printdialog (NULL, $, $, printservice,//Service1, flavor, Pras); if(Service! =NULL) { Try{System.out.println ("Begin Print PDF:" +file.getname ()); Docprintjob Job= Service.createprintjob ();//Create a print jobFileInputStream fis=NewFileInputStream (file);//construct the file stream to be printedDocattributeset das=NewHashdocattributeset (); //Request a color printerPras.add (Chromaticity.color); //Request Landscape ModePras.add (Orientationrequested.landscape); //US Letter-size paper attributesPras.add (Mediasizename.na_letter); //European A4 PaperPras.add (MEDIASIZENAME.ISO_A4); //Request BindingPras.add (finishings.staple); //Organize multiple copiesPras.add (sheetcollate.collated); //Request DuplexPras.add (Sides.duplex); //2 pages to a worksheetPras.add (NewNumberup (2)); //Number of copiesPras.add (NewCopies (2)); Doc Doc=NewSimpledoc (FIS, Flavor, das); Job.print (Doc, Pras); } Catch(Exception e) {e.printstacktrace (); } } } }}
Java Print Printing