首页 > 解决方案 > 我想使用 Glen K. Peterson 的 Pdf Layout Manager 构建一个 PDF 文档,但我坚持构建一个表格

问题描述

我决定使用 GitHub 上提供的 Glen K Peterson 的 Pdf 布局管理器(https://github.com/GlenKPeterson/PdfLayoutManager)用我的应用程序生成 PDF 文档,我已经导入了源文件和 pom.xml 依赖项和一切,它工作得很好。问题是,我正在尝试在我想要通过单击按钮生成的文档之一中构建一个表格。我不知道如何提取(使用)TableBuilder,因为我在我的 JDeveloper IDE 中收到错误消息,即该类具有私有访问权限。

这是我的代码:

private void jBtnSalvareVerMetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSalvareVerMetActionPerformed
    // TODO add your handling code here:
    PDDocument document = new PDDocument();
    try {
        PDPage page = new PDPage();
        document.addPage(page);
        PDFont font = PDType1Font.COURIER;
        PDPageContentStream contents = new PDPageContentStream(document, page);
        contents.beginText();
        contents.setFont(font, 14);
        contents.newLineAtOffset(50, 500);
        Coord coordinate = new Coord(10, 700);
        PdfLayoutMgr pageMgr = PdfLayoutMgr.newRgbPageMgr();
        LogicalPage locatieTabel = pageMgr.logicalPageStart();
        TableBuilder tabel = new TableBuilder(locatieTabel, coordinate); // Getting the error at this point
        contents.newLineAtOffset(10, 700);
        contents.showText(tabel.toString());
        contents.endText();
        contents.close();
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(MeniuTaburi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } finally {
        try {
            document.close();
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(MeniuTaburi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }
    jLabelAverstismenteVerMet.setText("<html><center>Datele au fost salvate cu succes!</center></html>");
}//GEN-LAST:event_jBtnSalvareVerMetActionPerformed

我想过将 TableBuilder 的访问权限类型从私有更改为公共,但我认为这不是它实际应该工作的方式......还有其他方法,我可以构建我需要的表,无需在 TableBuilder 类中更改访问修饰符?

标签: javaswingpdfbox

解决方案


你尝试使用

TableBuilder tabel = new TableBuilder(locatieTabel, coordinate); // Getting the error at this point

但是那个构造函数是私有的

    private TableBuilder(LogicalPage lp, Coord tl) {
        logicalPage = lp; topLeft = tl;
    }

即你不打算使用它。不幸的是,也没有 JavaDoc 指示您应该改用什么。但是在构造函数之外查看TableBuilder源代码,您会立即发现以下内容:

    public static TableBuilder of(LogicalPage lp, Coord tl) {
        return new TableBuilder(lp, tl);
    }

因此,您应该使用以下工厂方法,而不是直接调用构造函数:

TableBuilder tabel = TableBuilder.of(locatieTabel, coordinate);

推荐阅读