Impressão Direta de PDF com Java
Autor: Douglas Pinheiro | Publicado: 2009-11-06T11:37:00-03:00

Neste exemplo, imprimimos um arquivo PDF direto do sistema. Para isso, basta informar uma parte ou todo o nome da impressora e indicar a localização do arquivo. Minha impressora é a printer001 e o meu arquivo como podem ver é o c:/teste.pdf. Deixem seus comentários.package printpdf;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import javax.print.Doc;import javax.print.DocFlavor;import javax.print.DocPrintJob;import javax.print.PrintService;import javax.print.PrintServiceLookup;import javax.print.SimpleDoc;public class PrintPDFFactory {&nbsp;&nbsp;&nbsp; private static PrintService impressora;&nbsp; &nbsp;&nbsp;&nbsp; public void printPDF(File f){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; try{&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DocFlavor dflavor = DocFlavor.INPUT_STREAM.AUTOSENSE;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PrintService[] impressoras = PrintServiceLookup.lookupPrintServices(dflavor, null);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for(PrintService ps : impressoras){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("Impressora Encontrada: "+ps.getName());&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(ps.getName().contains("printer001")){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  System.out.println("Impressora Selecionada: "+ps.getName());&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; impressora = ps;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DocPrintJob dpj = impressora.createPrintJob();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; FileInputStream fis = new FileInputStream(f);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; byte[] buffer = new byte[fis.available()];&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int buff = 0;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while((buff = fis.available()) != 0){&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fis.read(buffer, 0, buff);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(new String(buffer));&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; InputStream stream = new ByteArrayInputStream(buffer);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp; Doc doc = new SimpleDoc(stream, flavor, null);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dpj.print(doc, null);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }catch (Exception e) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  e.printStackTrace();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; public static void main(String[] args) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PrintPDFFactory  pdffactory = new PrintPDFFactory();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pdffactory.printPDF(new File("c:/teste.pdf"));&nbsp;&nbsp;&nbsp; }}