pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Beutel, Stephan" <sbeu...@heiler.com>
Subject Create a pdf form with pdfbox
Date Thu, 03 May 2012 08:44:13 GMT
Hello,

I need to create a pdf form which can be filled out with a computer.
In my testcase I want to vreate a simple text field, but I couldn't find any documentation
how to handle field creation.

I get an error while setting the value into the test field. Pdfbox can't find the defaultappearence.

If someone could send a small code snippet how to create forms with pdfbox I would be very
happy.

Here's my source:

package com.heiler;

import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextbox;
import org.apache.pdfbox.util.PDFOperator;

public class Main
{

  private static String path = "D:/Projects/playground";

  private void run() throws COSVisitorException, IOException
  {
    TestObject obj = new TestObject();
    String filename = Long.toString( GregorianCalendar.getInstance().getTimeInMillis() );

    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage( page );

    PDFont font = PDType1Font.HELVETICA_BOLD;

    // create simple text
    PDPageContentStream contentStream = new PDPageContentStream( document, page );

    contentStream.beginText();
    contentStream.setFont( font, 12 );
    contentStream.moveTextPositionByAmount( 100, 700 );
    contentStream.drawString( "Hello World" );
    contentStream.endText();

    contentStream.close();

    PDResources res = new PDResources();
    res.setFonts( createFontMap() );
    page.setResources( res );

    PDAcroForm acroForm = new PDAcroForm( document );
    // creating default resources
    PDResources dr = new PDResources();
    dr.setFonts( createFontMap() );
    acroForm.setDefaultResources( dr );

    document.getDocumentCatalog().setAcroForm( acroForm );

    COSDictionary dic = new COSDictionary();
    PDTextbox textField = new PDTextbox( acroForm, dic );
    COSDictionary textFieldDict = textField.getDictionary();
    textFieldDict.setName( COSName.FT, "Tx" );
    PDRectangle rect = new PDRectangle();
    rect.setLowerLeftX( ( float ) 88.14579772949219 );
    rect.setLowerLeftY( ( float ) 394.781005859375 );
    rect.setUpperRightX( ( float ) 232.9340057373047 );
    rect.setUpperRightY( ( float ) 416.5929870605469 );
    textField.getWidget().setRectangle( rect );
    textField.setPartialName( "Text1" );

    page.getAnnotations().add( textField );
    page.getAnnotations().add( textField.getWidget() );

    acroForm.getFields().add( textField );

    textField.setValue( "some text" );

    document.save( path + "/" + filename + ".pdf" );
    document.close();
  }

  private Map< String, PDType1Font > createFontMap()
  {
    Map< String, PDType1Font > fonts = new HashMap< String, PDType1Font >();

    fonts.put( PDType1Font.TIMES_ROMAN.getBaseFont(), PDType1Font.TIMES_ROMAN );
    fonts.put( PDType1Font.TIMES_BOLD.getBaseFont(), PDType1Font.TIMES_BOLD );
    fonts.put( PDType1Font.TIMES_ITALIC.getBaseFont(), PDType1Font.TIMES_ITALIC );
    fonts.put( PDType1Font.TIMES_BOLD_ITALIC.getBaseFont(), PDType1Font.TIMES_BOLD_ITALIC
);
    fonts.put( PDType1Font.HELVETICA.getBaseFont(), PDType1Font.HELVETICA );
    fonts.put( PDType1Font.HELVETICA_BOLD.getBaseFont(), PDType1Font.HELVETICA_BOLD );
    fonts.put( PDType1Font.HELVETICA_OBLIQUE.getBaseFont(), PDType1Font.HELVETICA_OBLIQUE
);
    fonts.put( PDType1Font.HELVETICA_BOLD_OBLIQUE.getBaseFont(), PDType1Font.HELVETICA_BOLD_OBLIQUE
);
    fonts.put( PDType1Font.COURIER.getBaseFont(), PDType1Font.COURIER );
    fonts.put( PDType1Font.COURIER_BOLD.getBaseFont(), PDType1Font.COURIER_BOLD );
    fonts.put( PDType1Font.COURIER_OBLIQUE.getBaseFont(), PDType1Font.COURIER_OBLIQUE );
    fonts.put( PDType1Font.COURIER_BOLD_OBLIQUE.getBaseFont(), PDType1Font.COURIER_BOLD_OBLIQUE
);
    fonts.put( PDType1Font.SYMBOL.getBaseFont(), PDType1Font.SYMBOL );
    fonts.put( PDType1Font.ZAPF_DINGBATS.getBaseFont(), PDType1Font.ZAPF_DINGBATS );

    return fonts;
  }

  public static void main( String[] args )
  {
    System.out.println( "started" );
    Main main = new Main();
    try
    {
      main.run();
    }
    catch ( COSVisitorException e )
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch ( IOException e )
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}


Thanks

Stephan


Stephan Beutel
Senior Java Developer HBC


Tel: +49 (711) 13984-277
Fax: +49 (711) 8666301
Mobil:
sbeutel@heiler.com<mailto:sbeutel@heiler.com>

Heiler Software AG
Mittlerer Pfad 5 - 70499 Stuttgart
www.heiler.com<http://www.heiler.com>

Management Board: Rolf J. Heiler, Dr. Wolfgang A. K?stler, Frank Schmidt, Dirk H?u?ermann
Chairman of the Supervisory Board: Antonio Schnieder
Registry Court: Stuttgart District Court, HRB 20399, German VAT ID.DE 146023553

[Heiler Software] <http://www.heiler.com>

E-Procurement Specialist Interview
Amazonization in Catalog Procurement

> Read now!<http://blog.heiler.com/02/2012/specialist-interview-amazonization-in-catalog-procurement/>


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message