1
PROBLEM: Ich möchte PDF-Datei auf Netzwerkdrucker drucken. Es gibt kein Problem mit dem Drucken von TXT-Datei oder String, aber Problem entsteht, wenn ich versuche, PDF/JEPG/DOC-Dateien zu drucken.Java-Drucken. Weird wymbols wo PDF
DRUCKER: Samsung SCX-6545 Series PCL
Code:
public static void printFile(String filename) throws IOException {
FileInputStream psStream = null;
try {
psStream = new FileInputStream(filename);
} catch (FileNotFoundException ffne) {
}
if (psStream == null) {
return;
}
int count = 0;
DocFlavor psInFormat = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
// aset.add(MediaSize.ISO_A4);
// aset.add(Sides.DUPLEX);
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, null);
for (int i = 0; i < services.length; i++) {
if (services[i].getName().contains("SCX")) {
count = i;
}
}
DocFlavor[] docFalvor = services[count].getSupportedDocFlavors();
for (int i = 0; i < docFalvor.length; i++) {
System.out.println(docFalvor[i].getMimeType());
}
if (services.length > 0) {
System.out.println(Files.probeContentType(Paths.get(filename)));
DocPrintJob job = services[count].createPrintJob();
try {
job.print(myDoc, aset);
} catch (PrintException pe) {
System.out.print(pe);
}
}
psStream.close();
}
public static void main(String[] args) throws IOException {
printFile("C:/Users/artur.pakula/Documents/aaa.pdf");
}
RESULT bedruckter PDF: PDF
RESULT OF PRINTED PNG: PNG
Wo haben Sie Variable "count" deklariert? – likeToCode
Möglicherweise haben Sie stattdessen mehr Glück mit [DocFlavor.URL.AUTOSENSE] (http://docs.oracle.com/javase/8/docs/api/javax/print/DocFlavor.URL.html#AUTOSENSE). Verwenden Sie anstelle von FileInputStream 'Paths.get (filename) .toUri(). ToURL()', um ein Objekt zu erhalten, das als erstes Argument an den SimpleDoc-Konstruktor übergeben wird. – VGR