2015-12-02 128 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));