Why is iText's PdfWriter printing JTextFields

2019-05-24 23:01发布

问题:

I hava got a strange conundrum. I am currently trying to make a Runnable Jar of an Eclipse project I'm working on that has a number of JTextField's and JFormattedTextField's all nicely arranged in JPanel's. I am then taking these JPanel's and printing them out use iText's nice PdfWriter.

The issue is this: In Eclipse, this prints out just fine. When I export my project into a runnable jar, I get the following:

All of those black rectangles are where my JTextField's and JFormattedTextField's are located.

Does anyone have any idea what may be causing this? This is how I am printing things out, and remember, it is working in Eclipse but not when exported as runnable jar:

private void print() throws DocumentException, IOException {
    Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
    File file = new File(System.getProperty("user.home") + File.separator
            + "Desktop" + File.separator + "temp.pdf");
    if (!file.exists()) {
        file.createNewFile();
    }
    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(file));
    document.open();
    PdfContentByte contentByte = writer.getDirectContent();
    int x = (int) document.getPageSize().getWidth() / 2 - 250;
    int y = (int) document.getPageSize().getHeight() - 375;
    for (int i = 0; i < jobs.size(); i++) {
        jobs.get(i).setFocusable(false);
        contentByte.moveTo(0, y + 235);
        contentByte.lineTo(document.getPageSize().getWidth(), y + 235);
        contentByte.closePathStroke();
        y += 5;
        PdfTemplate template = contentByte.createTemplate(600, 250);
        @SuppressWarnings("deprecation")
        Graphics2D g2 = template.createGraphics(600, 250);
        jobs.get(i).print(g2);
        g2.dispose();
        contentByte.addTemplate(template, x, y);
        y -= 105;
        jobs.get(i).setFocusable(true);
    }
    contentByte.moveTo(0, y + 235);
    contentByte.lineTo(document.getPageSize().getWidth(), y + 235);
    contentByte.closePathStroke();

    y = 25;
    PdfTemplate template = contentByte.createTemplate(600, 25);
    @SuppressWarnings("deprecation")
    Graphics2D g2 = template.createGraphics(600, 25);
    total.setFocusable(false);
    totalPanel.setOpaque(false);
    totalPanel.print(g2);
    g2.dispose();
    contentByte.addTemplate(template, x - 65, y);
    total.setFocusable(true);
    totalPanel.setOpaque(true);

    document.close();
}

Thanks for all of your help!