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 7043033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:14:17+00:00 2026-05-28T02:14:17+00:00

I have a web application that uses a couple of PDF forms to create

  • 0

I have a web application that uses a couple of PDF forms to create documents of up to 500 pages; each form is one page and has 40-50 fields on it. The completed document is display-and-print only, there is no need to retain the fill-in aspect of the PDF form as the document is being created.

I have working code using iText 1.4.5; it creates these documents in less than 30 seconds (websphere, MVS) which is fine for my purposes.

The app does use significant amounts of memory, and recently contributed to a server crash. I am interested in whether I can modify the existing code to keep most of its attributes and use significantly less memory. It seems to me it should be possible, given that the amount of memory used suggests the entire document is in memory until completed, and my logic has no need for that — once a page is filled, my program is done with it, it could be written to disk and any memory associated just with that page freed.

I have found reference to the method com.lowagie.text.pdf.PdfWriter.freeReader(), but am not sure how to use it in my environment. My question is whether it would cause my program to use less memory (at one time) and where to put the call.

I create iText Document , PdfWriter, and PdfReader objects as follows:

public PdfFormFiller(String givenInputSpecification, 
                        Document givenDocument, 
                        PdfWriter givenWriter) 
{
  // instance fields stored for PDF or tracking purposes.
  inputSpecification = givenInputSpecification;
  document = givenDocument;
  writer = givenWriter;
  contentByte = writer.getDirectContent();
  // 'DirectContentUnder' is a contentByte object that allows
  // our app to write out document content that appears
  // underneath things written to the DirectContentOver; i.e.,
  // this is a layer underneath some other things.
  underContent = writer.getDirectContentUnder();

  try
  {
    PdfReader reader = new PdfReader(inputSpecification);
    template = writer.getImportedPage(reader, 1);           // this line limits us to 1-page forms;
    AcroFields aFields = reader.getAcroFields();            // the fields on the form.
  <<more stuff in this constructor, deleted from here>>  

I fill in values in the form using this:

/**
 * * 'Fill' this given form with the given data values, i.e., write the given data
 * values onto the positions in the forms corresponding to their field names. 
 * @param fieldValueMap a map with each key the name
 * of the data field, and each value the string to be put on
 * the form for that field.  
 */
public void fillForm(Map fieldValueMap) throws DocumentException
{
  Iterator keys = fieldValueMap.keySet().iterator();
  while (keys.hasNext())
  {
    String fieldName = (String)keys.next();
    FormField formField = (FormField)fields.get(fieldName);
    String value = null;
    if (fieldName != null)
      {
        value = (String)fieldValueMap.get(fieldName);
      }
    if (null != value && null != formField)
    {
      fillField(formField, value);
    }
  }
  // add the template of the form; the fact that it is added
  // to "underContent" causes iText to put it in a list if it's
  // not already there, so it only gets added once per doc.
  underContent.addTemplate(getTemplate(), 0, 0);

  // start a new page - throws DocumentException
  document.newPage();
}

And I write a value to a field using this:

/**
 * fills the given field with the given value
 * @param formField field and attributes
 * @param value String value
 */
private void fillField(FormField formField, String value) throws DocumentException
{
  if (formField.fieldType == AcroFields.FIELD_TYPE_CHECKBOX)
  {
    if (value.substring(0,1).equalsIgnoreCase("Y")) { value = "X"; } 
                                                else { value = " "; }
  }

  ColumnText columnText = new ColumnText(contentByte); 

  <<excised code determining fontToUse>>

        setSimpleColumn(columnText, value, fontToUse, formField.box,
                            leading, Element.ALIGN_LEFT, false);
}

‘setSimpleColumn()’ is a utlility routine handling the fitting of text into a rectangle on the form.

private int setSimpleColumn(ColumnText columnText, String value, Font fontToUse, 
                                Rectangle box, int leading, int alignment, boolean simulate)
    throws DocumentException
{
  columnText.setSimpleColumn(new Phrase(value, fontToUse),
        box.left(), box.bottom(),
        box.right(), box.top(),
       leading, alignment
      );
  int result = columnText.go(simulate);
  return result;
}

So again, the main 2 questions are: would use of PdfWriter.freeReader() help free up memory that’s otherwise held until the document is complete, and (2) where would I put a call to it?

If someone wants to tell me how to do multi-page forms, I’m interested in that as well…

  • 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-05-28T02:14:18+00:00Added an answer on May 28, 2026 at 2:14 am

    I’m not seeing the code that loops through the documents, but PdfWriter.freeReader() will free up memory when you are concatenating multiple documents. Here is the javadoc explanation:

    Use this method to writes the reader to the document and free the memory used by it. The main use is when concatenating multiple documents to keep the memory usage restricted to the current appending document.

    So is that what you are doing?

    As simple as it sounds, what I think you need is to close each document as you loop through the processing, something like:

            //loop iteration
            // step 1
            Document document = new Document();
            // step 2
            PdfWriter.getInstance(document, new FileOutputStream(filename));
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph("Hello World!"));
            //process the document.
            ...
            //save the document.
            ...
            // step 5
            document.close();
            //next loop iteration
    

    Since you don’t need to save each document, would it work to combine 20 or 30 forms at a time as a single pdf, close it out, then create another 20 or 30 forms, do the same and then combine/merge the final document with these other create documents to avoid holding everything open until the end?

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

Sidebar

Related Questions

I have a web forms application that uses entity framework, the application is deployed
I have web application that uses JasperReports for PDF report generation. I'd like to
I have a web application that uses Ext-JS 2.2. In a certain component, we
I have a web application that uses the current version of JQuery that needs
I have a web application that uses a number of WCF Services. I deploy
I have a web application that uses TONS of javascript, and as usual, there
I have a web application that uses AzMan authorization to grant different functionality to
I have a web application that uses two databases. DB1 Users perform their CRUD
I have a web application that uses the CDO Message object to email reports.
I have a web application that uses a library which resides in TOMCAT_HOME/common/lib. This

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.