【问题标题】:JavaFX - IText - Open PDF when createdJavaFX - IText - 创建时打开 PDF
【发布时间】:2016-03-06 19:10:50
【问题描述】:

我创建了这个方法来使用 IText 库制作 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);
    }
}

当按下“打印 PDF”按钮时,应该执行此方法。我似乎无法开始工作的唯一一件事是使用按钮创建时自动打开 PDF 文件。谁能帮我解决这个问题?

【问题讨论】:

    标签: java pdf javafx pdf-generation itext


    【解决方案1】:
    Desktop.getDesktop().open(new File(filename));
    

    【讨论】:

      【解决方案2】:

      前段时间我为 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()));
          }
      }
      

      【讨论】:

      • 如果可以的话,最好避免混合 JavaFX API 和 AWT API,不是吗?
      【解决方案3】:

      要在平台的默认 PDF 查看器中打开它,您可以:

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

      获取主机服务实例有点麻烦:您需要在Application 实例上调用getHostServices()。显然,您只能在 start()init() 方法中执行此操作,因此您很可能需要在那里执行此操作,然后将该实例传递给您需要它的控制器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        • 2019-03-03
        相关资源
        最近更新 更多