JavaFX - IText - 创建时打开PDF

时间:2015-12-02 13:54:22

标签: java pdf javafx pdf-generation itext

我已创建此方法以使用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文件。任何人都可以帮助我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

void allPermutations(int k , double lower , double upper)
    double[] d = new double[d]
    d[0] = lower
    allPermutations(k - 1 , d , upper)

//k := number of elements that still need to be inserted into the permutation
void allPermutations(int k , double[] arr , double upper)
    if(k == 0)
        print(arr)
    else
        double lower = arr[length(arr) - k - 1]
        for(double d = lower ; d < upper + 0.2 ; d += 0.1)
            arr[length(arr) - k] = d
            allPermutations(k - 1 , arr)

答案 2 :(得分:0)

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