Skip to content

Instantly share code, notes, and snippets.

@HashRaygoza
Created March 11, 2018 19:03
Show Gist options
  • Save HashRaygoza/6df9e9501a928fb5ed543a7c4a41ee81 to your computer and use it in GitHub Desktop.
Save HashRaygoza/6df9e9501a928fb5ed543a7c4a41ee81 to your computer and use it in GitHub Desktop.
Envia un Pdf a la impresora
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mx.hash.impresionpdf;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
/**
*
* @author david
*/
public class Impresor {
private final static Logger LOGGER = Logger.getLogger("mx.hash.impresionpdf.Impresor");
public static void main(String[] args) {
Impresor impresor = new Impresor();
try {
impresor.imprimir();
} catch (PrinterException | IOException ex) {
JOptionPane.showMessageDialog(null, "Error de impresion", "Error", JOptionPane.ERROR_MESSAGE);
LOGGER.log(Level.SEVERE, null, ex);
}
}
/***
* Manda un documento a imprimir a la impresora que se indique en el dialogo
* @throws PrinterException
* @throws IOException
*/
public void imprimir() throws PrinterException, IOException {
// Indicamos el nombre del archivo Pdf que deseamos imprimir
PDDocument document = PDDocument.load(new File("./documento.pdf"));
PrinterJob job = PrinterJob.getPrinterJob();
LOGGER.log(Level.INFO, "Mostrando el dialogo de impresion");
if (job.printDialog() == true) {
job.setPageable(new PDFPageable(document));
LOGGER.log(Level.INFO, "Imprimiendo documento");
job.print();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment