Print text File to specific printer in java
By : Marius Ionescu
Date : March 29 2020, 07:55 AM
hop of those help? I'm not sure if this solves your problem but I use the following to print a text file code :
FileInputStream textStream;
textStream = new FileInputStream(FILE_NAME);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc mydoc = new SimpleDoc(textStream, flavor, null);
PrintService[] services = PrintServiceLookup.lookupPrintServices(
flavor, aset);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
if(services.length == 0) {
if(defaultService == null) {
//no printer found
} else {
//print using default
DocPrintJob job = defaultService.createPrintJob();
job.print(mydoc, aset);
}
} else {
//built in UI for printing you may not use this
PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);
if (service != null)
{
DocPrintJob job = service.createPrintJob();
job.print(mydoc, aset);
}
}
|
Automatically Print the Back side of All print jobs on Duplexed printer with same file
By : Nurudin
Date : March 29 2020, 07:55 AM
Hope that helps The answer provided by Douglas Anderson is the easiest option. However if you also want this to get enabled in a client-server environment, then it would be difficult. One option is to go for a print processor which would insert this template job after every page. However this may affect drivers that use their own print processors. Another easy solution - if you need this backside template for all jobs that are fired to this printer, why not use a preprinted paper with the template already printed? So you dont need to write any software at all!!
|
How to print using a line printer dirictly from java using the fonts of the printer?
By : user3447325
Date : March 29 2020, 07:55 AM
hope this fix your issue This code doesn't require any Swing related component but still it needs Graphics class of awt, but you can print a text from console there is no UI component being displayed, just tested it: code :
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PageRanges;
public class DirectPrint implements Printable {
private PrintService[] printService;
private String text;
public DirectPrint() {
this.printService = PrinterJob.lookupPrintServices();
}
public static void main(String[] args) {
DirectPrint lt = new DirectPrint();
lt.printString("If this text gets printed, it will have worked! ;D");
}
public void printString(String input) {
this.text = input;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PageRanges(1, 1));
aset.add(new Copies(1));
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
try {
printJob.setPrintService(getDefaultPrintService());
//index of installed printers on you system
//not sure if default-printer is always '0'
printJob.print(aset);
} catch (PrinterException err) {
System.err.println(err);
}
}
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g.drawString(String.valueOf(this.text), 14, 14);
return PAGE_EXISTS;
}
}
InputStream in = null;
try {
log.debug("preparing input stream");
in = getFileTobePrinted();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// find the printing service
log.debug("fetching print service");
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("lq2170", null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet);
// create the print job
log.debug("creating print job");
DocPrintJob job = services[0].createPrintJob();
Doc doc = new SimpleDoc(in, flavor, null);
// monitor print job events
log.debug("preparing print job monitor");
PrintJobWatcher watcher = new PrintJobWatcher(job);
// print it
log.debug("start printing");
job.print(doc, null);
// wait for the print job is done
log.debug("waiting for the printing to finish");
watcher.waitForDone();
log.debug("done !");
} finally {
if (in != null) try { in.close(); } catch(Exception e) {}
}
|
Java - Print numbers saved to text file in past 24 hours
By : KikeSIlva
Date : March 29 2020, 07:55 AM
may help you . You are right that you need to change the format of your file. I would want to use either Instant.toString() or LocalDateTime.toString() for generating the time stamps. So each line would become like code :
5 2017-11-06T14:52:31.434Z
|
How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer
By : Halil Matıracı
Date : March 29 2020, 07:55 AM
it fixes the issue To print a PrintDocument object using the Microsoft Print to PDF printer without prompting for a filename, here is the pure code way to do this:
|