Reputation: 317
I need to print to a label printer. I found the code below in the internet and was trying to modify it to my needs. It works fine when the label printer is the default printer in the system, but when I set up other printer as the default it doesn't print properly. In the picture on the link below one can see the result when is default (the one on the right), the first attempt when is not default (the middle one) and a second attempt when is not default. I need to print properly whether the printer is the default or not, since this will be part of a software that we will be releasing to clients. My question is: Is there any specific attributes or commands that I should sent to the printer in order to print properly when is not the default printer? or How can I set that printer as the default printer from inside my code, before printing, and then restore the default printer when I am done printing? picture link (https://lh4.googleusercontent.com/-jBa7bA77ik0/URplehZYVIE/AAAAAAAAQWg/bO4EE3wepjg/s279-c/21213)
the source code
/*
* Copyright (c) 2004 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 3nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose,
* including teaching and use in open-source projects.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book,
* please visit http://www.davidflanagan.com/javaexamples3.
*/
//package je3.print;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
import org.apache.pdfbox.pdmodel.PDDocument;
/**
* This utility program demonstrates the javax.print API and allows you to list
* available printers, query a named printer, print text and image files to a
* printer, and print to postscript files.
*/
public class Print {
public static void main(String[] args) throws IOException {
// These are values we'll set from the command-line arguments
boolean query = false;
String printerName = "Brother QL-570 LE";
String inputFileName = "C:\\dev\\label.pdf";
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(OrientationRequested.LANDSCAPE);
if (query) {
// Look for a named printer that can support the
// attributes and print its status
queryPrinter(printerName, attributes);
} else {
// print to the named printer, or to the default
// printer otherwise.
print(printerName, inputFileName, attributes);
}
// The main() method ends here, but there may be a printing thread
// operating in the background. So the program may not terminate
// until printing completes.
}
// List names of all PrintServices that can support the attributes
public static void queryServices(PrintRequestAttributeSet attributes) {
// Find all services that can support the specified attributes
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributes);
// Loop through available services
for (int i = 0; i < services.length; i++) {
// Print service name
System.out.print(services[i].getName());
// Then query and print the document types it can print
DocFlavor[] flavors = services[i].getSupportedDocFlavors();
for (int j = 0; j < flavors.length; j++) {
// Filter out DocFlavors that have a representation class other
// than java.io.InputStream.
String repclass = flavors[j].getRepresentationClassName();
if (!repclass.equals("java.io.InputStream"))
continue;
System.out.println("\t" + flavors[j].getMimeType());
}
}
}
// List details about the named printer
public static void queryPrinter(String printerName, PrintRequestAttributeSet attributes) {
// Find the named printer
PrintService service = getNamedPrinter(printerName, attributes);
if (service == null) {
System.out.println(printerName + ": no such printer capable of "
+ "handling the specified attributes");
return;
}
// Print status and other information about the printer
System.out.println(printerName + " status:");
Attribute[] attrs = service.getAttributes().toArray();
for (int i = 0; i < attrs.length; i++)
System.out.println("\t" + attrs[i].getName() + ": " + attrs[i]);
}
// Print the contents of the named file to the named printer (or to a
// default printer if printerName is null) requesting the specified
// attributes.
public static void print(String printerName, String filename, PrintRequestAttributeSet attributes)
throws IOException {
// Look for a printer that can support the attributes
PrintService service = getNamedPrinter(printerName, attributes);
if (service == null) {
System.out.println("Can't find a printer " + "with specified attributes");
return;
}
// Print the file to that printer. See method definition below
printToService(service, filename, attributes);
// Let the user know where to pick up their printout
System.out.println("Printed " + filename + " to " + service.getName());
}
// Print to an output file instead of a printer
public static void printToFile(String outputFileName, String outputFileType,
String inputFileName, PrintRequestAttributeSet attributes) throws IOException {
// Determine whether the system can print to the specified type, and
// get a factory object if so.
// The name of this static method is way too long!
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(null, outputFileType);
// Error message if we can't print to the specified output type
if (factories.length == 0) {
System.out.println("Unable to print files of type: " + outputFileType);
return;
}
// Open the output file
FileOutputStream out = new FileOutputStream(outputFileName);
// Get a PrintService object to print to that file
StreamPrintService service = factories[0].getPrintService(out);
// Print using the method below
printToService(service, inputFileName, attributes);
// And remember to close the output file
out.close();
}
// Print the contents of the named file to the specified PrintService,
// requesting the specified attributes.
// This is shared code used by print() and printToFile() above.
public static void printToService(PrintService service, String filename,
PrintRequestAttributeSet attributes) throws IOException {
// Figure out what type of file we're printing
DocFlavor flavor = getFlavorFromFilename(filename);
// Open the file
//InputStream in = new FileInputStream(filename);
PDDocument document = null;
try
{
document = PDDocument.load(filename);
}
catch (Exception e)
{
System.out.println("Unable to open PDF file ");
}
// Create a Doc object to print from the file and flavor.
Doc doc = new SimpleDoc(document, flavor, null);
// Create a print job from the service
DocPrintJob job = service.createPrintJob();
// Monitor the print job with a listener
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCompleted(PrintJobEvent e) {
System.out.println("Print job complete");
System.exit(0);
}
public void printDataTransferCompleted(PrintJobEvent e) {
System.out.println("Document transfered to printer");
}
public void printJobRequiresAttention(PrintJobEvent e) {
System.out.println("Print job requires attention");
System.out.println("Check printer: out of paper?");
}
public void printJobFailed(PrintJobEvent e) {
System.out.println("Print job failed");
System.exit(1);
}
});
// Now print the document, catching errors
try {
job.print(doc, attributes);
} catch (PrintException e) {
System.out.println(e);
System.exit(1);
}
}
// A utility method to look up printers that can support the specified
// attributes and return the one that matches the specified name.
public static PrintService getNamedPrinter(String name, PrintRequestAttributeSet attrs) {
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attrs);
if (services.length > 0) {
if (name == null)
return services[0];
else {
for (int i = 0; i < services.length; i++) {
if (services[i].getName().equals(name))
return services[i];
}
}
}
return null;
}
// A utility method to return a DocFlavor object matching the
// extension of the filename.
public static DocFlavor getFlavorFromFilename(String filename) {
String extension = filename.substring(filename.lastIndexOf('.') + 1);
extension = extension.toLowerCase();
if (extension.equals("gif"))
return DocFlavor.INPUT_STREAM.GIF;
else if (extension.equals("jpeg"))
return DocFlavor.INPUT_STREAM.JPEG;
else if (extension.equals("jpg"))
return DocFlavor.INPUT_STREAM.JPEG;
else if (extension.equals("png"))
return DocFlavor.INPUT_STREAM.PNG;
else if (extension.equals("ps"))
return DocFlavor.INPUT_STREAM.POSTSCRIPT;
else if (extension.equals("txt"))
return DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
else if (extension.equals("pdf"))
return DocFlavor.SERVICE_FORMATTED.PAGEABLE;
// Fallback: try to determine flavor from file content
else
return DocFlavor.INPUT_STREAM.AUTOSENSE;
}
}
Upvotes: 0
Views: 5460
Reputation: 930
You can try the pdf print function with pdfbox https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java?view=markup
Upvotes: 1
Reputation: 16
public static void main(String[] args) {
try
{
FileInputStream fis = new FileInputStream("c:\\N0018902726.pdf");
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocAttributeSet das = new HashDocAttributeSet();
Doc pdfDoc = new SimpleDoc(fis, flavor, null);
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
fis.close();
}
catch (Exception e) {
System.out.println("EXCEPTIOn : " +e.getMessage());
}
}
Upvotes: 0