Example ASP and Java

In this chapter…

…we will discuss ASP and Java examples.

Apart from Perl, whose sample script appears in the previous appendix, ASP and Java are two more popular technologies used today. Here are some sample code fragments and notes to help you get started using FDFMerge Lite or FDFMerge within either ASP or Java. These samples are provided “as is” and Appligent does not provide scripting support other than the provision of these examples.

If you have trouble running FDFMerge Lite or FDFMerge within a script, try to run the software directly from the command line. This will tell you whether the problem is with the script or with FDFMerge Lite or FDFMerge.

Note: The scripts on this page demonstrate running FDFMerge but can be modified to call FDFMerge Lite or any of our other applications.

FDFMerge with ASP

This first example uses Windows Scripting Host to call FDFMerge. Windows Scripting Host comes with most current versions of Windows. This example states where FDFMerge is located, sets up the command to be used, and sets up a shell for running FDFMerge.

'Built the Executable statement
sFdfMergeAppl = sFdfMerge & " -r " & sFdfMergeRegNum
sFdfMergeParms = " -s -p -l " & sLogFile & " -o " & sOutPDFFile
sFdfMergeParms = sFdfMergeParms & " " &InPdfFile & " " & sInFdfFile
sFdfMergeCmd = sFdfMergeAppl & " " & sFdfMergeParms

'Create the Shell object to run the FDFMerge command line.
Set wShell = Server.CreateObject("WScript.Shell")

'Execute the FDFMerge command line and get the return code.
iReturn = wShell.Run( sFdfMergeCmd, 10, True )
Set wShell = Nothing

FDFMerge in a Java Servlet

The following is a sample Java servlet that runs FDFMerge. This example has been adapted from a Sun example. Users may wish to visit Sun’s Java website for more information. Keep the following tips in mind when using this example:

  • Use the full path for the fdfmerge executable and for each of the argument files (out.pdf, in.pdf, and in.fdf).
  • Give the target directory full permissions to be certain that the Servlet can execute and write without restriction.
  • Since FDFMerge outputs to STDOUT when the -p option is included, the Servlet can report the results in addition to creating the target PDF file.
  • By capturing this output in a string variable, “Initializing…done.” can be included in the generated HTML.
  • In the Windows environment directory paths are separated using the backslash (\) character. Since the backslash (\) is also an escape character you must use double backslashes (\\) for directory paths on Windows.

The sample Java servlet is shown below.

// Example Servlet -- Sample execution of Appligent FDFMerge
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
* This is a simple, Sun-provided, example of an HTTP Servlet.
* The example has been modified to demo Appligent Merge execution!
* It responds to the GET and HEAD methods of the HTTP protocol.
public class FDFMerge_Servlet_27 extends HttpServlet
* Handle the GET and HEAD methods by building a simple web page.
* Include the execution and output of Appligent FDFMerge
* HEAD is just like GET, except that the server returns only the
* headers (including content length) not the body we write.
public void doGet (HttpServletRequest request,
HttpServletResponse response)
  throws ServletException, IOException
   PrintWriter out;
   String title = "Example Apache JServ Servlet --
    Executing Appligent FDFMerge!";
   // set content type and other response header fields first
   // then write the data of the response
   out = response.getWriter();
  // define the FDFMerge command-line components
  String fdfmerge_cmd = "/usr/apache/servlets/fdfmerge -p -s -o ";
  String fdf_file = " /usr/apache/servlets/gen_srvlt_frm1.fdf ";
  String pdf_infile = " /usr/apache/servlets/GenDBform01.pdf ";
  String pdf_outfile = " /usr/apache/servlets/test_srvlt_27_1200.pdf ";
  String fdf_result = "FDF Result:= ";
  String fdf_error = "FDF ERROR MESSAGE:= ";
  if (fdfmerge_cmd !=null && fdf_file !=null
   && pdf_infile !=null && pdf_outfile !=null) {
   Runtime rt = Runtime.getRuntime();
    String exec_strg = fdfmerge_cmd +
     pdf_outfile +
     pdf_infile +
  // DEBUG: use a standard OS cmd like 'cat' or 'type' to test the Servlet
  // String exec_strg = "cat /usr/apache/servlets/TEST_STDOUT_1.txt";
   Process p = rt.exec(exec_strg);   // Start the cmd
   InputStream os = p.getInputStream();   // Read bytes from cmd
   InputStreamReader osr = new InputStreamReader(os);
   // Convert to chars
   BufferedReader br = new BufferedReader(osr);  // Read Lines of chars 
     String s = br.readLine();
    fdf_result = "std_out> " + s;
    catch(IOException e)
   // System.err.println("Caught ioexception in
     // testdatamerg.runFDFtest: " + e);
   fdf_error = "std_err> " + "Caught ioexception in
    testdatamerg.runFDFtest!!!" + e;
   // System.out.println(e.toString());
  } else {
   fdf_result = "Error: Null FDFMerge cmd or arguments!";
    out.println("</TITLE></HEAD><BODY bgcolor="#FFFFFF">");
    out.println("<H1>" + title + "</H1>");
    out.println("<H2> ApacheJServ 1.1.2 - Sample Appligent
     FDFMerge Servlet!<br>");
   out.println("FDF_RESULT> " + fdf_result);
   out.println("FDF_ERROR> " + fdf_error);