I am trying to print a pdf document using java.awt.print.PrinterJob.print (PrintRequestAttributeSet attributes). Before that, I put the necessary attributes in attributes using PrinterJob.printerDialog (attributes) and they really get there, but from everything I've tried, only the change in the number of pages that will be printed has been applied. The rest of the settings are ignored. How can I get the printer to print with the settings I want?
pdfReport - byte[] which was obtained with DatatypeConverter.parseBase64Binary from
printerService - standart "Microsoft Print to PDF"
PDDocument document = PDDocument.load(pdfReport);
try {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printer.printerService);
HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
if (job.printDialog(attributes)) {
try {
job.print(attributes);
} catch (PrinterException e) {
System.out.println(e);
}
}
} finally {
document.close();
}
P.S. : when the program is running, sun.awt.windows.WPrinterJob is used as the PrinterJob implemenjatation
Related
I want to use print product information on label printer. I have Zebra TLP 2844 This printer support EPL2 programming language. When I want to print non-ASCII character printer just print "?" instead of the correct character. What I did?
First I created a simple text file with the below content
əğüçşıö
and press CTRL+P to print. Output is ok printer print all characters normally.
But when I use EPL2 commands like below
N
A220,120,0,4,1,1,N,"əğçşıüö"
P1
My printer gives the below output
??ç??üö
How can I solve this problem? I use java print api to send command to my printer.
And this my code
PrinterJob pj = PrinterJob.createPrinterJob();
Doc doc = new SimpleDoc(sb.toString().getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
boolean result = false;
javax.print.PrintService printService = PrintServiceLookup.lookupPrintServices(null, null)[0];
try {
printService.createPrintJob().print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
PrinterJob pj = PrinterJob.createPrinterJob();
Doc doc = new SimpleDoc(sb.toString().getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
boolean result = false;
javax.print.PrintService printService = PrintServiceLookup.lookupPrintServices(null, null)[0];
try {
printService.createPrintJob().print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
Its not an issue with the code it's an issue with the printer. Most printers don't include those characters in their standard font set. You either need to use the character's hex to call them or, if they're not included on the font set on the printer, download a different font.
I've code up and running for sending Blob-Objects to a printer using javax.print. Everything works fine, except in some cases only the first page of multipage PDF-files is printed. The file looks normal when opened in acrobat and when I take the OutputStream that is used to feed the printjob and save it to another file I get and exact copy - so the stream seems to be okay. I have no idea why the following pages are dropped in some cases. here's my code:
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
aset.add(OrientationRequested.PORTRAIT);
aset.add(Sides.ONE_SIDED);
aset.add(MediaSizeName.ISO_A4);
aset.add(new JobName("Sammelausdruck", null));
ArrayList<String> fehlerDokumente = new ArrayList<String>();
//some standard print dialog
PrintService printService = ServiceUI.printDialog(null, 50, 50,
PrintServiceLookup.lookupPrintServices(null, aset), PrintServiceLookup.lookupDefaultPrintService(),
null, aset);
if (printService != null) {
DocPrintJob docjob = printService.createPrintJob();
aset.remove(JobName.class);
aset.add(new JobName(dok.getDokbezeichnung(), null));
File file = <some PDF-file>;
try {
FileInputStream psStream = new FileInputStream(file);
if (file.exists() && !file.isDirectory()) {
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
SimpleDoc doc = new SimpleDoc(psStream, psInFormat, null);
docjob.print(doc, aset);
}
} catch (FileNotFoundException e) {
...
} catch (PrintException e) {
...
}
}
}
Any ideas? I'm even interested in debug-ideas.
thank you
Edit:
In some very rare cases the printer (a professional office-printer with a touchscreen) provides an error-message that no matching paper is found for the given page-size and the pagesize shown is slightly off Din-A4 (210,2 x 295,8 or somthing like that). In this cases, if i manually tell the printer to use Din-A4, the printjob continues. I don't know if that has a complete different cause or is just another peculiarity of the same problem. I also tried to fiddle around with the MediaPrintableArea Attribute, but that didn't help either.
I am working with PDFBOX and the documentation on it seems sparse so I've come here for some help. I am trying to print out a pdf form that I've created, with fields populated dynamically by eclipse. I can get it to import and print, but when I do print, the fields I've set don't show up (although they do when I save it to HDD). Can someone point me to the settings to set visible when printing? I saw itext had something similar, and I'm hoping that PDFBox does too.
Here is my current code.
PDDocument doc = null;
try{
doc = PDDocument.load("resources/orderForm.pdf");
PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField("Orderer");
field.setValue("JohnTest");
} catch (IOException ie){
System.out.println(ie);
}
//doc.addPage(new PDPage());
try{
//doc.save("Empty PDF.pdf");
doc.silentPrint();
//doc.print();
doc.close();
} catch (Exception io){
System.out.println(io);
}
}
found my answer, can't use pdfbox to do it, although the alternative is just as simple. Use the desktop to print the file! example code as follows
public void printOrder(){
try {
File myFile = new File(finished);
//Desktop.getDesktop().open(myFile);
Desktop.getDesktop().print(myFile);
doc.close();
} catch (IOException ex) {
// no application registered for PDFs
}
}
How can I print duplex in java!?
Here's my code, but it didn't work - it only works in microsoft word (so the printer can do it)
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(2));
pras.add(Sides.DUPLEX);
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper temp = pf.getPaper();
temp.setImageableArea(1, 3, temp.getWidth(), temp.getHeight());
pf.setPaper(temp);
if (pj.printDialog(pras)) {
try {
pj.setPrintable(this, pf);
pj.print(pras); // Drucken
return true;
} catch (Exception PrintException) {
.....
}
I tried to give only the attributes to print, only to the dialog, etc. etc. etc.
It print's the document 2 times, but not duplex! (also tried Sides.TWO_SIDED_LONG_EDGE..)
The Problem was my printer,... it always turn the option for duplex to be disabled.
Hey everyone ,
I'm facing a real problem here while trying to print a pdf file using java print .
The problem is that when i send the file to the printer using print() method with cute pdf the file is well printed but with a real printer it couldn't be done .
`
try{
File file = new File(toprint);
InputStream is = new BufferedInputStream(new FileInputStream(file));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob job = p.createPrintJob();//p here is my printservice printer
HashPrintRequestAttributeSet printRequestSet = new HashPrintRequestAttributeSet();
HashDocAttributeSet ds=new HashDocAttributeSet();
Doc doc = new SimpleDoc(is, flavor, null);
job.print(doc, aset);
}
catch(Exception e){
System.out.println("An exception occured while printing the file "+ e);
}
`
I've tried it so many times but it doesn't work.
Any ideas?
I think your printer may not have support for pdf, in this case you will have to render it using a pdf renderer.
Look at http://java.net/projects/pdf-renderer and pageable print page.
If you find it helpful I will provide code samples.