2015-12-02 129 views
2

我已经创建了使用IText库创建PDF的方法。JavaFX - IText - 创建PDF时创建PDF

private void pdfluggage() 
     throws Exception { 
    try { 
     Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/fys", "fys", "Welkom01"); 
     Statement statement = connection.createStatement(); 

     ResultSet rs = statement.executeQuery("SELECT * FROM luggage ORDER BY luggage_id DESC LIMIT 1"); 
     Document document = new Document(); 
     String outputFile = "luggageform.pdf"; 

     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); 

    //PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 842, 1f); 
    //writer.setOpenAction(PdfAction.gotoLocalPage(1, pdfDest, writer)); 


     //writer.setOpenAction(new PdfAction(PdfAction.PRINTDIALOG)); 

     document.open(); 

     Paragraph Paragraph1 = new Paragraph(); 
     Image image = Image.getInstance("file:Image/Corendon-Logo.jpg"); 
     Image image2 = Image.getInstance("file:Image/footer.png"); 
     image.scalePercent(55f); 
     image2.scalePercent(35f); 
     image2.setAbsolutePosition(0f, 0f); 
     Paragraph1.setAlignment(Element.ALIGN_CENTER); 
     Font font1 = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLD | Font.UNDERLINE); 
     Font font2 = new Font(Font.FontFamily.COURIER, 15, Font.UNDERLINE); 
     Font font3 = new Font(Font.FontFamily.COURIER, 15); 

     Chunk chunk = new Chunk("Added luggage form", font1); 

     Paragraph1.add(chunk); 

     document.add(image); 
     document.add(new Phrase("\n")); 
     document.add(new Phrase("\n")); 
     document.add(new Phrase("\n")); 
     document.add(Paragraph1); 
     document.add(new Phrase("\n")); 
     document.add(new Phrase("\n")); 
     document.add(new Phrase("\n")); 
     document.add(new Phrase("\n")); 
     while (rs.next()) { 

      document.add(new Chunk("Luggage status: ", font3)); 
      Chunk chunk2 = new Chunk((rs.getString("status"))); 
      chunk2.setFont(font2); 
      document.add(chunk2); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Brand name: ", font3)); 
      Chunk chunk3 = new Chunk((rs.getString("brand_name"))); 
      chunk3.setFont(font2); 
      document.add(chunk3); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Luggage color: ", font3)); 
      Chunk chunk4 = new Chunk((rs.getString("color"))); 
      chunk4.setFont(font2); 
      document.add(chunk4); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Luggage type: ", font3)); 
      Chunk chunk5 = new Chunk((rs.getString("luggage_type"))); 
      chunk5.setFont(font2); 
      document.add(chunk5); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Number of wheels: ", font3)); 
      Chunk chunk6 = new Chunk((rs.getString("number_of_wheels"))); 
      chunk6.setFont(font2); 
      document.add(chunk6); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Weight category: ", font3)); 
      Chunk chunk7 = new Chunk((rs.getString("weight_category"))); 
      chunk7.setFont(font2); 
      document.add(chunk7); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Flight number: ", font3)); 
      Chunk chunk8 = new Chunk((rs.getString("flight_number"))); 
      chunk8.setFont(font2); 
      document.add(chunk8); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Comments: ", font3)); 
      Chunk chunk9 = new Chunk((rs.getString("comments"))); 
      chunk9.setFont(font2); 
      document.add(chunk9); 
      document.add(new Phrase("\n")); 
      document.add(new Chunk("Current location: ", font3)); 
      Chunk chunk10 = new Chunk((rs.getString("current_location"))); 
      chunk10.setFont(font2); 
      document.add(chunk10); 
      document.add(new Phrase("\n")); 

     } 
     document.add(image2); 



     document.close(); 

     rs.close(); 
     statement.close(); 
     connection.close(); 

    } catch (FileNotFoundException | DocumentException ex) { 
     Logger.getLogger(TestPDF.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

当按下“Print PDF”按钮时,应执行此方法。我似乎无法正常工作的唯一方法是使用按钮创建时自动打开PDF文件。任何人都可以帮助我实现这个目标吗?

回答

0

要在平台的默认PDF查看器打开它,你可以这样做:

HostServices hostServices = ... ; 
File file = new File(outputFile); 
String url = file.toURI().toURL().toExternalForm(); 
hostServices.showDocument(url); 

获取主机服务实例是一个有点疼痛:你需要调用getHostServices()Application实例。显然,你只能在你的start()init()方法中做到这一点,所以你很可能需要在那里做,然后将该实例传递给你需要它的控制器。

0

我开发的PDF演示应用前一段时间,这是我的处理方法:

protected void handleFire() { 
    try { 
     Desktop.getDesktop().open(getFile()); 
    } catch (IOException | IllegalArgumentException | NullPointerException e) { 
     logger.log(Level.SEVERE, String.format("Could not open file. %s", e.getMessage())); 
    } 
} 
+0

这也可能是最好避免混合JavaFX的API和AWT API,如果你可以,不过,不是吗? –

0
Desktop.getDesktop().open(new File(filename));