Using ApacheFOP v1.0 in .NET application

2020-02-20 02:30发布

问题:

Has anyone successfully complied the Apache FOP v1.0 library to a .NET DLL? I am using the IKVM syntax found at http://onjava.com/pub/a/onjava/2004/08/18/ikvm.html; however, the compiled DLL seems to be incomplete. For example, I cannot instantiate FopFactory object as:

using org.apache.fop.apps;

namespace Utils
{
     public class PdfRender
     {
          public void Render()
          {
            FOUserAgent foUserAgent = fop.getUserAgent();
            FopFactory fopFactory = FopFactory.newInstance();
          }
     }
}

回答1:

(Courtesy of the folks on the FOP Users Group)

Prerequisite: IKVM 0.44.0.5 installed.

  1. Download FOP 1.0 from http://xmlgraphics.apache.org/fop/1.0/index.html#download
  2. Copy all JAR files to C:\Fop\Build\
  3. Open a command prompt and run the following: ikvmc -target:library -reference:IKVM.OpenJDK.Core.dll -recurse:C:\Fop\Build\\*.jar -version:1.0 -out:C:\Fop\fop.dll
  4. In your Visual Studio project, add references to fop.dll, IKVM.OpenJDK.*.dll and IKVM.Runtime.dll


回答2:

I tried the approach suggested by ClayB without any success.

I used the combinations of IKVM 0.44 and 0.46 with FOP 0.95 and 1.0. But nothing worked! There're some java libs are missing and causing errors. There's also an .net exception occured: could not load type in System.Security namespace. I even tried to compile each .jar file to a dll as suggested by Avik Sengupta at http://onjava.com/pub/a/onjava/2004/08/18/ikvm.html, but got stuck on lib\batik-all-1.7.jar. Unless anyone have got any further workrounds, I'm convinced this approach is no longer working with the latest kits.

However, I found a different approach which allows one to call FOP from .net. I hope this will help someone:

try {
    ProcessStartInfo p = new ProcessStartInfo(fopPath + "fop.bat");
    p.Arguments = String.Format("{0} {1}", foFilePath, pdfFilePath);
    p.WindowStyle = ProcessWindowStyle.Hidden;
    p.WorkingDirectory = fopPath;
    Process proc = new System.Diagnostics.Process();
    proc.StartInfo = p;

    proc.Start();
    proc.WaitForExit();
    }
catch() {
}

Please read the original article at: http://www.ptperalta.net/index.php/technology/using-apache-fop-in-net.html



回答3:

I know this is an old thread but it still took some research to get this working. It is now available with NuGet in Visual Studio 2013. The NuGet package is called crispin.fop. In my code below, I pass in an "fop" file and the new PDF file I want created and "voila", it is created.

    using org.apache.fop.tools;
    using org.apache.fop.apps;
    using org.xml.sax;
    using java.io;

    public void GeneratePDF(string foFile, string pdfFile)
    {
        OutputStream os = new BufferedOutputStream(new FileOutputStream(new java.io.File(pdfFile)));

        try
        {
            FopFactory fopFactory = FopFactory.newInstance();
            Fop fop = fopFactory.newFop("application/pdf", os);
            FOUserAgent foUserAgent = fop.getUserAgent();
            javax.xml.transform.TransformerFactory factory = javax.xml.transform.TransformerFactory.newInstance();
            javax.xml.transform.Transformer transformer = factory.newTransformer();
            javax.xml.transform.Source src = new javax.xml.transform.stream.StreamSource(new java.io.File(foFile));
            javax.xml.transform.Result res = new javax.xml.transform.sax.SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);
        }

        catch (Exception ex)
        {                
            throw ex;
        }

        finally
        {
            os.close();
        }
    }


回答4:

ClayB's worked for me. Please keep in mind point 2 "Copy all JAR files to C:\Fop\Build\".
"all" is very important. See here the list of jar's I needed:
- fop.jar
- avalon-framework-4.2.0.jar
- batik-all-1.7.jar
- commons-io-1.3.1.jar
- commons-logging-1.0.4.jar
- serializer-2.7.0.jar
- xalan-2.7.0.jar
- xercesImpl-2.7.1.jar
- xml-apis-1.3.04.jar
- xml-apis-ext-1.3.04.jar
- xmlgraphics-commons-1.4.jar
All the jar's other than fop you will find in folder "lib".



回答5:

ClatB`s worked here too. Just remmeber to add ALL IKVM libs to your project (Worked with Fop 1.1 and IKVM 7.2.4630.5).