Call printer demo

Source: Internet
Author: User
The printservice interface is the factory of docprintjob. Printservice describes the features of a printer and allows you to query the attributes supported by the printer.

Java code
  1. Import java. Io. file;
  2. Import java. Io. fileinputstream;
  3. Import javax. Print. Doc;
  4. Import javax. Print. docflavor;
  5. Import javax. Print. docprintjob;
  6. Import javax. Print. printservice;
  7. Import javax. Print. printservicelookup;
  8. Import javax. Print. serviceui;
  9. Import javax. Print. simpledoc;
  10. Import javax. Print. Attribute. docattributeset;
  11. Import javax. Print. Attribute. hashdocattributeset;
  12. Import javax. Print. Attribute. hashprintrequestattributeset;
  13. Import javax. Swing. jfilechooser;
  14. /**
  15. * Establish a connection with the printer
  16. * @ Author Administrator
  17. *
  18. */
  19. Public class printdemo {
  20. Public static void main (string [] ARGs ){
  21. Jfilechooser filechooser = new jfilechooser (); // create a print job
  22. Int state = filechooser. showopendialog (null );
  23. If (State = filechooser. approve_option ){
  24. File file = new file ("D:/zkyzl.txt"); // obtain the selected file
  25. // Construct the print request property set
  26. Hashprintrequestattributeset pras = new hashprintrequestattributeset ();
  27. // Set the print format. Because the type is not determined, select autosense.
  28. Docflavor flavor = docflavor. input_stream.autosense;
  29. // Find all available print services
  30. Printservice [] = printservicelookup. lookupprintservices (flavor, pras );
  31. // Locate the default Print Service
  32. Printservice defaultservice = printservicelookup. lookupdefaprintprintservice ();
  33. // Display the Print dialog box
  34. Printservice service = serviceui. printdialog (null, 200,200, printservice,
  35. Defaservice service, flavor, pras );
  36. If (service! = NULL ){
  37. Try {
  38. Docprintjob job = service. createprintjob (); // create a print job
  39. Fileinputstream FCM = new fileinputstream (File); // construct the file stream to be printed
  40. Docattributeset DAS = new hashdocattributeset ();
  41. Doc = new simpledoc (FCM, flavor, das );
  42. Job. Print (Doc, pras );
  43. } Catch (exception e ){
  44. E. printstacktrace ();
  45. }
  46. }
  47. }
  48. }
  49. }

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.