2016-05-09 6 views
1

問題: ネットワークプリンタでPDFファイルを印刷したいと思います。 txtファイルや文字列の印刷に問題はありませんが、PDF/JEPG/DOCファイルを印刷しようとすると問題が発生します。Java印刷。奇妙なwymbolsどこの印刷PDF

PRINTER:サムスンSCX-6545シリーズPCL

CODE:PRINTED PDF OF

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"); 
} 

結果:PRINTED PNG OF PDF

結果:PNG

+0

あなたは、変数 "カウント" を宣言しましたか? – likeToCode

+0

代わりに、[DocFlavor.URL.AUTOSENSE](http://docs.oracle.com/javase/8/docs/api/javax/print/DocFlavor.URL.html#AUTOSENSE)を使用する方が良いかもしれません。 FileInputStreamの代わりに、 'Paths.get(filename).toUri()。toURL()'を使用して、SimpleDocコンストラクタの最初の引数として渡すオブジェクトを取得します。 – VGR

答えて

1

私はこの問題を解決します。

コード:

import org.apache.pdfbox.pdfparser.PDFParser; 
import org.apache.pdfbox.pdmodel.PDDocument; 
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.INPUT_STREAM.AUTOSENSE; 

    Doc myDoc = new SimpleDoc(psStream, psInFormat, null); 
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
    aset.add(new Copies(1)); 
    // aset.add(new Copies(5)); 
    // 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("ITE_LUB_MONO")) { 
      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))); 
     System.out.println(services[count].getName()); 
     DocPrintJob job = services[count].createPrintJob(); 
     try { 
      // właściwy kod 
      PDFParser parser = new PDFParser(psStream); 
      PrinterJob job1 = PrinterJob.getPrinterJob(); 
      job1.setPrintService(services[count]); 
      PDDocument doc = PDDocument.load(filename); 
      doc.silentPrint(job1); 
      // właściwy kod koniec 

     } catch (PrinterException pe) { 
      System.out.print(pe); 

     } 
    } 
    psStream.close(); 
} 

public static void main(String[] args) throws IOException { 
    printFile("C:/Users/jakub.wojtczak/Desktop/aaa.pdf"); 
}