PDF中的Java数据

时间:2018-06-28 07:54:49

标签: java pdf javafx

对于Java程序,您在其中键入将放入PDF的数据(PDF中已经有数据,则应将来自程序的数据放入特定位置)。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以在maven项目中使用Itext PDF进行操作。您可以添加以下功能来打印数据。

       private static String FILE = "../print2.pdf";   

    try {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();            
         //This is title.
        Paragraph preface = new Paragraph();
        addEmptyLine(preface, 1);
        document.add(preface);
        preface.add(new Paragraph("This is cupcake.", catFont));
        addTitlePage(document);
        // This is content.
        Anchor anchor = new Anchor(AddSalesPromoController.tempPromoName.getText(), catFont);
        anchor.setName("First Chapter");
        document.close();
        if (Desktop.isDesktopSupported()) {
            try {
                File myFile = new File("../print2.pdf");
                Desktop.getDesktop().open(myFile);
            } catch (Exception ex) {
                ModelUtil.getAlert("Not supported", "not supported", Alert.AlertType.ERROR);
                // no application registered for PDFs
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }