Java Connection Printer

Source: Internet
Author: User

Package com.gdpe.ws.bpmn.web;
Import Java.io.File;
Import Java.io.FileInputStream;

Import Javax.print.Doc;
Import Javax.print.DocFlavor;
Import Javax.print.DocPrintJob;
Import Javax.print.PrintService;
Import Javax.print.PrintServiceLookup;
Import Javax.print.SimpleDoc;
Import Javax.print.attribute.DocAttributeSet;
Import Javax.print.attribute.HashDocAttributeSet;
Import Javax.print.attribute.HashPrintRequestAttributeSet;
Import Javax.print.attribute.PrintRequestAttributeSet;
public class printtest{
public void Printjpg (String getfile,string Fensu)
{
Constructs a file selector that defaults to the current directory

File File = new file (getfile);//Get Selected Files
Build a print Request property set
Printrequestattributeset Pras = new Hashprintrequestattributeset ();
Set the print format, because the file type is not determined, select AutoSense here
Docflavor flavor = Docflavor.input_stream. GIF;
Find all available Print services
Printservice printservice[] = printservicelookup.lookupprintservices (flavor, Pras);
Locate the default Print service
Printservice Defaultservice = Printservicelookup.lookupdefaultprintservice ();
Show Print dialog box
Long J=integer.parseint (FENSU);
for (int i=0;i<j;i++)
{
try {
Docprintjob job = Defaultservice.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); Create a print file format
Job.print (Doc, Pras); To print a file
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
public static void Main (String args[])
{
Printtest pic = new Printtest ();
Pic.printjpg ("D:\\image.gif", "1");
}
}

Java Connection Printer

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.