PDFBOX - Changing pdf form values

I’ve been attempting to work with the open source PDFBOX project to manipulate form values. Through some google searching I’ve narrowed it down to some specific classes that I need, but things don’t seem to be working the way they should. At least by my figuring.

From tutorialspoint.com/pdfbox I found the following:

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage;
public class LoadingExistingDocument {

   public static void main(String args[]) throws IOException {

      //Loading an existing document 
      File file = new File("C:/PdfBox_Examples/sample.pdf"); 
      PDDocument document = PDDocument.load(file); 
        
      System.out.println("PDF loaded"); 
        
      //Adding a blank page to the document 
      document.addPage(new PDPage());  

      //Saving the document 
      document.save("C:/PdfBox_Examples/sample.pdf");

      //Closing the document  
      document.close(); 

From that I gleaned that I should do the following in my lucee code

pdfpath = expandPath("") & "/MASTER REPC packet.pdf";

pdffile = createObject('java', "java.io.File").init(pdfpath);

PDDocument = createObject('java', "org.apache.pdfbox.pdmodel.PDDocument");

pdfDoc = PDDocument.load(pdffile);

//DO STUFF WITH THE DOCUMENT JUST LOADED

Everything works until I add PDDocument.load(). Then I get the following error message:

org/apache/fontbox/ttf/TrueTypeFont

This is the full message:

lucee.runtime.exp.NativeException: org/apache/fontbox/ttf/TrueTypeFont
 	at java.lang.Class.getDeclaredMethods0(Native Method)
 	at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
 	at java.lang.Class.privateGetPublicMethods(Class.java:2902)
 	at java.lang.Class.getMethods(Class.java:1615)
 	at lucee.runtime.reflection.storage.SoftMethodStorage.store(SoftMethodStorage.java:63)
 	at lucee.runtime.reflection.storage.SoftMethodStorage.getMethods(SoftMethodStorage.java:48)
 	at lucee.runtime.reflection.Reflector.getMethodInstanceEL(Reflector.java:521)
 	at lucee.runtime.reflection.Reflector.getMethodInstance(Reflector.java:719)
 	at lucee.runtime.java.JavaObject.call(JavaObject.java:262)
 	at lucee.runtime.java.JavaObject.call(JavaObject.java:287)
 	at lucee.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:785)
 	at lucee.runtime.PageContextImpl.getFunction(PageContextImpl.java:1709)
 	at pdfbox.index_cfm$cf$28.call(/pdfbox/index.cfm:13)
 	at lucee.runtime.PageContextImpl._doInclude(PageContextImpl.java:944)
 	at lucee.runtime.PageContextImpl._doInclude(PageContextImpl.java:836)
 	at lucee.runtime.listener.ModernAppListener._onRequest(ModernAppListener.java:216)
 	at lucee.runtime.listener.MixedAppListener.onRequest(MixedAppListener.java:42)
 	at lucee.runtime.PageContextImpl.execute(PageContextImpl.java:2415)
 	at lucee.runtime.PageContextImpl._execute(PageContextImpl.java:2405)
 	at lucee.runtime.PageContextImpl.executeCFML(PageContextImpl.java:2380)
 	at lucee.runtime.engine.Request.exe(Request.java:43)
 	at lucee.runtime.engine.CFMLEngineImpl._service(CFMLEngineImpl.java:1117)
 	at lucee.runtime.engine.CFMLEngineImpl.serviceCFML(CFMLEngineImpl.java:1063)
 	at lucee.loader.engine.CFMLEngineWrapper.serviceCFML(CFMLEngineWrapper.java:102)
 	at lucee.loader.servlet.CFMLServlet.service(CFMLServlet.java:51)
 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
 	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
 	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
 	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
 	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
 	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
 	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
 	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
 	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:412)
 	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
 	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
 	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1385)
 	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
 	at java.lang.Thread.run(Thread.java:748)
 Caused by: java.lang.NoClassDefFoundError: org/apache/fontbox/ttf/TrueTypeFont
 	... 48 more
 Caused by: java.lang.ClassNotFoundException: org.apache.fontbox.ttf.TrueTypeFont
 	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1292)
 	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1121)
 	... 48 more

If someone knows of a cfm specific tutorial on this, or has worked with pdfbox before, any help or direction would be appreciated.

Hi Hugh

I think the problem is that you’re relying on the required java classes being available in your Lucee installation. Lucee does use PDFBox, but it may not be the right version and you can’t rely on all the dependencies being there.

I use PDFBox for a number of things via a standalone wrapper library I built which uses JavaLoader to ensure the right classes are loaded with no conflicts.

I’m afraid it’s not public, but Matthew Clemente has a project on Github you might want to take a look at: