Java Print Printing

Source: Internet
Author: User
Tags print format

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

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.