2016-09-10 1 views
0

J'ai un problème, Mon code qui convertit un PDF au format imprimable qui peut être imprimé verrouille mon fichier pdf.Java Itext PDF impression, l'impression du fichier reste verrouillé par Java

Mon code:

public class PDFPrinter { 

public PDFPrinter(File file) { 
    try { 
     FileInputStream fis = new FileInputStream(file); 
     FileChannel fc = fis.getChannel(); 
     ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 
     fis.close(); 
     fc.close(); 
     PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page 
     PDFPrintPage pages = new PDFPrintPage(pdfFile); 

     // Create Print Job 
     PrinterJob pjob = PrinterJob.getPrinterJob(); 
     PageFormat pf = PrinterJob.getPrinterJob().defaultPage(); 
     Paper a4paper = new Paper(); 
     double paperWidth = 8.26; 
     double paperHeight = 11.69; 
     a4paper.setSize(paperWidth * 72.0, paperHeight * 72.0); 

     /* 
     * set the margins respectively the imageable area 
     */ 
     double leftMargin = 0.3; 
     double rightMargin = 0.3; 
     double topMargin = 0.5; 
     double bottomMargin = 0.5; 

     a4paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0, 
       (paperWidth - leftMargin - rightMargin) * 72.0, 
       (paperHeight - topMargin - bottomMargin) * 72.0); 
     pf.setPaper(a4paper); 

     pjob.setJobName(file.getName()); 
     Book book = new Book(); 
     book.append(pages, pf, pdfFile.getNumPages()); 
     pjob.setPageable(book); 

     // Send print job to default printer 
     if (pjob.printDialog()) { 
      pjob.print(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (PrinterException e) { 
     JOptionPane.showMessageDialog(null, "Printing Error: " 
       + e.getMessage(), "Print Aborted", 
       JOptionPane.ERROR_MESSAGE); 
     e.printStackTrace(); 
    } 
} 

class PDFPrintPage implements Printable { 
    private PDFFile file; 

    PDFPrintPage(PDFFile file) { 
     this.file = file; 
    } 

    public int print(Graphics g, PageFormat format, int index) 
      throws PrinterException { 
     int pagenum = index + 1; 

     // don't bother if the page number is out of range. 
     if ((pagenum >= 1) && (pagenum <= file.getNumPages())) { 
      // fit the PDFPage into the printing area 
      Graphics2D g2 = (Graphics2D) g; 
      PDFPage page = file.getPage(pagenum); 
      double pwidth = format.getImageableWidth(); 
      double pheight = format.getImageableHeight(); 

      double aspect = page.getAspectRatio(); 
      double paperaspect = pwidth/pheight; 

      Rectangle imgbounds; 

      if (aspect > paperaspect) { 
       // paper is too tall/pdfpage is too wide 
       int height = (int) (pwidth/aspect); 
       imgbounds = new Rectangle(
         (int) format.getImageableX(), 
         (int) (format.getImageableY() + ((pheight - height)/2)), 
         (int) pwidth, height); 
      } else { 
       // paper is too wide/pdfpage is too tall 
       int width = (int) (pheight * aspect); 
       imgbounds = new Rectangle(
         (int) (format.getImageableX() + ((pwidth - width)/2)), 
         (int) format.getImageableY(), width, (int) pheight); 
      } 

      // render the page 
      PDFRenderer pgs = new PDFRenderer(page, g2, imgbounds, null, 
        null); 
      try { 
       page.waitForFinish(); 
       pgs.run(); 
      } catch (InterruptedException ie) { 
      } 

      return PAGE_EXISTS; 
     } else { 
      return NO_SUCH_PAGE; 
     } 
    } 
} 
} 

Je l'appelle avec:

new PDFPrinter(file); 

Tout fonctionne très bien, mais après avoir commencé à imprimer le fichier PDF est verrouillé par Java. Qu'est-ce qui ne va pas?? Lorsque je redémarre Java, il fonctionne à nouveau, mais une seule fois, il est à nouveau verrouillé.

+0

Êtes-vous sûr que ce soit une question sur iText ?? ? –

+0

Pas à propos d'itext mais il y a un lien avec celui-ci –

+0

Je vois que vous avez supprimé l'étiquette iText. Je vous remercie. FYI: toutes les questions SO marquées avec 'itext' s'affichent dans un canal sur Slack d'iText (notre outil de communication interne). –

Répondre

0

J'ai trouvé une autre solution ...

Pour tout le monde:

public static void printPdf (String filePath, String jobName) throws IOException, PrinterException { 

    FileInputStream fileInputStream = new FileInputStream(filePath); 
    byte[] pdfContent = new byte[fileInputStream.available()]; 
    fileInputStream.read(pdfContent, 0, fileInputStream.available()); 
    ByteBuffer buffer = ByteBuffer.wrap(pdfContent); 

    final PDFFile pdfFile1 = new PDFFile(buffer); 
    pdf_print_engine pages1 = new pdf_print_engine(pdfFile1); 
    PrinterJob pjob = PrinterJob.getPrinterJob(); 
    PageFormat pfDefault = PrinterJob.getPrinterJob().defaultPage(); 
    Paper defaultPaper = new Paper(); 
    defaultPaper.setImageableArea(0, 0, defaultPaper.getWidth(), defaultPaper.getHeight()); 
    pfDefault.setPaper(defaultPaper); 
    pjob.setJobName("Test"); 
    if (pjob.printDialog()) { 
     // validate the page against the chosen printer to correct 
     // paper settings and margins 
     pfDefault = pjob.validatePage(pfDefault); 
     Book book = new Book(); 

     book.append(pages1, pfDefault, pdfFile1.getNumPages()); 
     pjob.setPageable(book); 

     try { 
      pjob.print(); 
     } catch (PrinterException exc) { 
      System.out.println(exc); 
     } 
    } 

Amusez-vous avec le Code

Tim