Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. import java.awt.print.PrinterException;
  2. import java.awt.print.PrinterJob;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import javax.print.attribute.HashPrintRequestAttributeSet;
  8. import javax.print.attribute.PrintRequestAttributeSet;
  9. import javax.print.attribute.standard.Sides;
  10. import org.apache.pdfbox.pdmodel.PDDocument;
  11. import org.apache.pdfbox.printing.PDFPageable;
  12.  
  13. public class PrintPDF{
  14.  
  15. //Method 1
  16. public static void printDocument() throws IOException, PrinterException{
  17. PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
  18.  
  19. pras.add(Sides.TWO_SIDED_SHORT_EDGE);
  20. PDDocument input = PDDocument.load(new File("Karteikarten.pdf"));
  21.  
  22. PrinterJob job = PrinterJob.getPrinterJob();
  23. job.setPageable(new PDFPageable(input));
  24.  
  25. if (job.printDialog(pras)) {
  26. job.print(pras);
  27. }
  28. }
  29.  
  30. //Method 2
  31. public static void print(File file, int copies, PrintService ps) {
  32.  
  33. logger.debug("print start - " + file);
  34. try {
  35. try (PDDocument document = PDDocument.load(file)) {
  36.  
  37. MediaSizeName size = getMediaSizeName(document);
  38.  
  39. PrinterJob job = PrinterJob.getPrinterJob();
  40. job.setPrintService(ps);
  41. job.setPageable(new PDFPageable(document));
  42. PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
  43. attr.add(size);
  44. attr.add(new Copies(copies));
  45. attr.add(new JobName(file.getName(), null));
  46.  
  47. PageFormat pf = job.getPageFormat(attr);
  48. Paper paper = pf.getPaper();
  49. paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
  50. pf.setPaper(paper);
  51.  
  52. job.print(attr);
  53. }
  54. } catch (IOException | PrinterException ex) {
  55. logger.warn(ex, ex);
  56. }
  57. logger.debug("print end - " + file);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement