Hi guys
Kindly assist me on how to print a simple text file in a small font, say verdana with font size of 8. My problem is when I print my text file (see source code below), it gives a default font and size according to the printer. What I need is a code that I can change it to any fonts I want.
Code:
import java.io.*;
import javax.print.*;
public class Printpb {
public static void main(String[] args) throws IOException {
//we are going to print "printtest.txt" file which is inside working directory
File file = new File("D:/printtest.txt");
InputStream is = new BufferedInputStream(new FileInputStream(file));
//Discover the default print service. If you call PrintServiceLookup.lookupPrintServices
//then it will return an array of print services available and you can choose a
//printer from them
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
//Doc flavor specifies the output format of the file (Mime type + Char encoding)
//You can retrieve doc flavors supported by the printer, like this
//DocFlavor [] supportedFlavors = service.getSupportedDocFlavors();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// Create the print job
DocPrintJob job = service.createPrintJob();
//Create the Doc. You can pass set of attributes(type of PrintRequestAttributeSet) as the
//3rd parameter specifying the page setup, orientation, no. of copies, etc instead of null.
Doc doc = new SimpleDoc(is, flavor, null);
//Order to print, (can pass attributes instead of null)
try {
job.print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
//DocPrintJob.print() is not guaranteed to be synchronous. So it's better to wait on completion
//of the print job before closing the stream. (See the link below)
is.close();
System.out.println("Printing done....");
}
}