2013-04-30 109 views
10

Javaを使用してPDFファイルを印刷しようとすると問題が発生します。ここに私のコードは次のとおりです。JavaでPrinterJobを使用してPDFファイルを印刷する

PdfReader readFtp = new PdfReader(); // This class is used for reading a PDF file 
PDDocument document = readFtp.readFTPFile(documentID); 

printRequestAttributeSet.add(new PageRanges(1, 10)); 

job.setPageable(document); 
job.print(printRequestAttributeSet); // calling for print 

document.close() 


私はdocument.silentPrint(job);job.print(printRequestAttributeSet);を使用する - それが正常に動作します。私が使用している場合document.silentPrint(job); - 私はPrintRequestAttributeSetを設定することはできません。

PrintRequestAttributeSetを設定する方法を教えてもらえますか?

答えて

0

すると、このコードを試してみてください。

FileInputStream fis = new FileInputStream(“C:/mypdf.pdf”); 
Doc pdfDoc = new SimpleDoc(fis, null, null); 
DocPrintJob printJob = printService.createPrintJob(); 
printJob.print(pdfDoc, new HashPrintRequestAttributeSet()); 
fis.close(); 

あなたはまた、these steps

+0

DocFlavorなしでPDFを印刷するにはどうすればいいですか? – whizzzkey

11

に従ってください。これは、プレーンJREでPDFを印刷するには私のために働いたことができます。PDDocumentの

public static void main(String[] args) throws PrintException, IOException { 
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; 
    PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet(); 
    patts.add(Sides.DUPLEX); 
    PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts); 
    if (ps.length == 0) { 
     throw new IllegalStateException("No Printer found"); 
    } 
    System.out.println("Available printers: " + Arrays.asList(ps)); 

    PrintService myService = null; 
    for (PrintService printService : ps) { 
     if (printService.getName().equals("Your printer name")) { 
      myService = printService; 
      break; 
     } 
    } 

    if (myService == null) { 
     throw new IllegalStateException("Printer not found"); 
    } 

    FileInputStream fis = new FileInputStream("C:/Users/John Doe/Desktop/SamplePDF.pdf"); 
    Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null); 
    DocPrintJob printJob = myService.createPrintJob(); 
    printJob.print(pdfDoc, new HashPrintRequestAttributeSet()); 
    fis.close();   
} 
+0

これはLinuxでのみ動作します。私はWindows上でJavaでPDFレンダラがないと思っていますが、正しいですか? – Daniel

+1

@Mirko Seifertこのコードは機能しません。私のプリンタはPDFドキュメントではなく、読めない文字しか印刷しません。 – whizzzkey

+2

このコードは、PDFを単独で解釈できるプリンタでのみ動作します。 HP LaserJetなど一部のプリンタが見つかりましたが、一部のプリンタでPDFファイルのテキストをテキストとして印刷することもありました。そのような場合は、PDFを画像に変換する必要があります(Ghostviewなど)。 –

0

Pageableを実装が推奨されていません代わりにPDPageableアダプタクラスを使用し、setPageableの代わりにsetPrintableを試してください:

job.setPrintable(new PDPageable(document)); 
+0

こんにちは新規ユーザー!あなたの例でPageRangesを設定する方法を示す良い例は、javadoc apisとバージョン –

1

次は、印刷ダイアログで複数のPDF文書を印刷するために私のために働いた:

public void printPDF() 
{ 
    PrinterJob printerJob = PrinterJob.getPrinterJob(); 

    PrintService printService; 
    if(printerJob.printDialog()) 
    { 
     printService = printerJob.getPrintService(); 
    } 
    DocFlavor docType = DocFlavor.INPUT_STREAM.AUTOSENSE; 

    for (//fetch documents to be printed) 
    { 
     DocPrintJob printJob = printService.createPrintJob(); 
     final byte[] byteStream = // fetch content in byte array; 
      Doc documentToBePrinted = new SimpleDoc(new ByteArrayInputStream(byteStream), docType, null); 
     printJob.print(documentToBePrinted, null); 
    } 
} 
13

私のプリンタは、ネイティブPDF印刷をサポートしていませんでした。

オープンソースライブラリApache PDFBox https://pdfbox.apache.orgを使用してPDFを印刷しました。印刷自体はまだJavaのPrinterJobによって引き継がれています。

import java.awt.print.PrinterJob; 
import java.io.File; 

import javax.print.PrintService; 
import javax.print.PrintServiceLookup; 

import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.printing.PDFPageable; 

public class PrintingExample { 

    public static void main(String args[]) throws Exception { 

     PDDocument document = PDDocument.load(new File("C:/temp/example.pdf")); 

     PrintService myPrintService = findPrintService("My Windows printer Name"); 

     PrinterJob job = PrinterJob.getPrinterJob(); 
     job.setPageable(new PDFPageable(document)); 
     job.setPrintService(myPrintService); 
     job.print(); 

    }  

    private static PrintService findPrintService(String printerName) { 
     PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); 
     for (PrintService printService : printServices) { 
      if (printService.getName().trim().equals(printerName)) { 
       return printService; 
      } 
     } 
     return null; 
    } 
} 
+1

への参照と同様に素晴らしく、PDDocumentを閉じるほうがよいので、try catchとfinallyが必要ですが全体的にはokです! –

関連する問題