Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:58:16+00:00 2026-06-04T12:58:16+00:00

I have a Java desktop application that is using iText to generate PDFs from

  • 0

I have a Java desktop application that is using iText to generate PDFs from a resultset. The first time you generate a PDF, it works fine. The problem comes when you try to generate a second one. It throws a DocumentException saying that the document is closed. I have tried to find other examples of people having this problem, and I come up with very little, which leads me to believe that I have made a very simple mistake and I cannot find it.

The code below is a snippet of the event handler that calls the report class:

RptPotReport report = new RptPotReport();
try {
    report.rptPot();
} catch (DocumentException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

And here is the code for the report class itself. The error occurs on the second run through this code:

public class RptPotReport {

    public static void main(String[] args) throws IOException, DocumentException, SQLException {
        new RptPotReport().rptPot();
    }

    String fileOutput = "Potting Report.pdf";

    public void rptPot() throws DocumentException, IOException {
        File f = new File("Potting Report.pdf");
        if (f.exists()) {
            f.delete();
        }

        Document document = new Document();
        document = pdfSizes.getPdfLetter();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileOutput));
        document.open();

        Phrase title = new Phrase();
        title.add(new Chunk("Potting Report"));

        document.add(title); // ******* DocumentException here: "The document has been closed. You can't add any Elements."
        document.close();

        try {
            File pdfFile = new File(fileOutput);
            if (pdfFile.exists()) {
                if (Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().open(pdfFile);
                } else {
                    System.out.println("Awt Desktop is not supported!");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

EDIT: At someone’s suggestion, I tried calling the RptPotReport from a second thread, but that did not change anything. Looking into it further, the Document class of iText creates a new thread when it’s instantiated. So I’m right back where I started, still stuck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:58:17+00:00Added an answer on June 4, 2026 at 12:58 pm

    What does this line do exactly in your application:

    document = pdfSizes.getPdfLetter();

    Without the code and with your explanation it seems like the line sets the reference of the document variable to the one that you receive from pdfSizes.getPdfLetter(), which is reused between run, thus you no longer have the reference of the new Document() statement.

    I tend to think the pdfSizes.getPdfLetter() method is bugged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a desktop application and I want to launch that application using JAVA
Im developing desktop java application using maven. I got a *.properties file that I
I'm writing a Java desktop client application that retrieves data from a remote MySQL
I am trying to build remote desktop application using java. For that what I
I have developed Java desktop application using Netbeans. In my application, I used some
We have a java desktop application and we are planning to launch that java
I have been using Netbeans for my java desktop application since few months. Now
I am creating a Java Desktop Application that is going to have a Firewall.
I have a Java desktop application that I have written. During the execution I
I have a Java web application that has a 'disconnected' Java Swing desktop app.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.