Set the print properties to construct a new empty print request property set. Printrequestattributeset Pras = new Hashprintrequestattributeset ();p Ras.add (New Copies (3));//print copies, 3 copies// Sets the format of the printed data docflavor.byte_array. PNG MIME type = "Image/png", prints the data representation of the class name = "[B" (byte array) of the Docflavor. Docflavor flavor = Docflavor.byte_array. png;//Find all eligible Print Services Lookupprintservices (flavor, Pras); Find the docflavor that can print the specified printservice. printservice[] Printservice = printservicelookup.lookupprintservices (flavor, Pras);//Match all the found printers to the printer you want, Find the printer you want Lookupprint p=new lookupprint (); Printservice Myprintservice = P.getprintservice ("Printname"); The PrinterJob class is the main class that controls printing. The application calls the methods in this class to set the job, optionally calls the Print dialog box with the user, and then prints the page of the job. Getprinterjob () Creates and returns the PrinterJob associated with the default printer when it is initialized. Print task PrinterJob job = Printerjob.getprinterjob ();//setprintservice (Printservice service) This printerjob with the new Printservice Association. Hang the task on the Service Job.setprintservice (Myprintservice); PDDocument This is an in-memory representation of the PDF document. When the document is no longer needed, you must call #close (Method Load (file file) parses a PDF. The unrestricted main memory will be used to buffer the PDF stream. PDDocument document = Pddocument.load (new File ("Printpath"));//convert PDF document to PDF stream into memory//paper class describes the physical characteristics of a sheet of paper. Paper Paper = new Paper ();p aper.setsize (252, 141); 1/72 inch//sets the width and height of this Paper object, which represents the properties of the page to be used for printing. Paper.setimageablearea (0, 5, paper.getwidth (), Paper.getheight ()); Setimageablearea: Sets the imageable area of this Paper. An imageable area is the area on the page that is used for printing. X-coordinate, Y-coordinate, area-width, height-//pageformat class in the upper-left corner describes the page size and orientation to be printed. Pageformat Pageformat = new Pageformat ();p ageformat.setpaper (paper);//For this Pageformat set paper Object//book class provides a representation of the document, The document's pages can use different page formats and page painter. This class interacts with PrinterJob using the Pageable interface. Book book = new book ();//Append multiple pages to the end of this book. Book.append (new Pdfprintable (document), Pageformat, Document.getnumberofpages ());//Querying document for page count and pageable instance The Pageformat and Printable of each page saved in document. Job.setpageable (New Pdfpageable (document, orientation.portrait)); job.setpageable (book);//print a set of pages. Job.print ();
Java-implemented printer printing 1