1

I need to set a value for PDF form using JAVA pdacroform api

below the code for setting up a value for particular field in PDF file but it throws

Exception in thread "main" java.lang.NoClassDefFoundError: org/fontbox/afm/AFMParser

despite of adding fontbox-1.7.jar

can any one help me out pls

import java.io.IOException;

import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.pdfbox.pdmodel.interactive.form.PDField;

import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentCatalog;

import org.pdfbox.exceptions.COSVisitorException;

import org.pdfbox.examples.AbstractExample;

public class SetField extends AbstractExample {

    public void setField(PDDocument pdfDocument, String name, String value)
            throws IOException {
        PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        PDField field = acroForm.getField(name);
        if (field != null) {
            field.setValue(value);
        } else {
            System.err.println("No field found with name:" + name);
        }

    }

    public static void main(String[] args) throws IOException,
            COSVisitorException {
        SetField setter = new SetField();
        setter.setField(args);
    }

    private void setField(String[] args) throws IOException,
            COSVisitorException {
        PDDocument pdf = null;
        try {
            if (args.length != 3) {
                usage();
            } else {
                SetField example = new SetField();

                pdf = PDDocument.load(args[0]);
                example.setField(pdf, args[1], args[2]);
                pdf.save(args[0]);
            }
        } finally {
            if (pdf != null) {
                pdf.close();
            }
        }
    }

    private static void usage() {
        System.err
                .println("usage: org.apache.pdfbox.examples.fdf.SetField <pdf-file> <field-name> <field-value>");
    }
}

Thanks

Ganeshja
  • 2,535
  • 11
  • 35
  • 56

2 Answers2

5

You are getting java.lang.NoClassDefFoundError because of missing jar with class definition.

You need to add FontBox jar to your classpath.

zibi
  • 2,949
  • 3
  • 25
  • 45
  • this s my class path code snippet and even i too have set it using cmd – Ganeshja Jan 29 '13 at 12:42
  • set classpath=%;c:\downloads\fontbox-1.7.1.jar% – Ganeshja Jan 29 '13 at 12:43
  • You are referensing the jars from 2 different placesL C:/Users/34/Downloads/fontbox-1.7.1.jar and c:\downloads\fontbox-1.7.1.jar - which of them is correct? – zibi Jan 29 '13 at 12:52
  • According to your eclipse project file, you use PdfBox 0.7.3 in combination with fontbox 1.7.1. The current version of PdfBox is 1.7.1. Are you sure your old PdfBox version cooperates well with the current FontBox version? – mkl Jan 29 '13 at 12:52
  • C:/Users/34/Downloads/fontbox-1.7.1.jar – Ganeshja Jan 29 '13 at 12:54
  • oops m not sure abt it ... let me update my pdfbox 0.7.3 to 1.7.1 – Ganeshja Jan 29 '13 at 12:55
  • Also the command line class path should look like - set CLASSPATH=classpath1;classpath2 - so in your example: set CLASSPATH=%CLASSPATH%;c:\downloads\fontbox-1.7.1.jar – zibi Jan 29 '13 at 12:56
  • yep updated to pdfbox1.7.1 and also update the class path as u mentioned but now i' m getting this exception : Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory – Ganeshja Jan 29 '13 at 13:01
  • @Ganeshja See my answer, commons-logging is also required. – mkl Jan 29 '13 at 13:04
  • Awesome got it , its working now !!! thanks a lot @user1951544 @ mkl thanks a lot – Ganeshja Jan 29 '13 at 13:10
  • @Ganeshja ... don't forget jempbox which is also documented as a hard dependency. – mkl Jan 29 '13 at 13:26
1

According to your comments to @user1951544's answer you seem to use a very old PdfBox version 0.7.3. This very likely is not cooperating well with the current FontBox jar. I would advice updating to the current state.

In that case you should also consider the other hard dependencies required by fontbox:

The main PDFBox component, pdfbox, has hard dependencies on the fontbox and jempbox components and the commons-logging library.

mkl
  • 85,104
  • 14
  • 121
  • 242