10

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();
          }
     }
}
Clay Benoit
  • 223
  • 1
  • 2
  • 11
  • 1
    did you try http://fonet.codeplex.com/ instead? – Mauricio Scheffer Oct 18 '10 at 17:14
  • see also: http://stackoverflow.com/questions/1099485/generating-pdf-in-net-using-xsl-fo – Mauricio Scheffer Oct 18 '10 at 17:16
  • We are currently using nFop, but would like to move to ApacheFOP v1.0 to leverage the 508 accessibility features. – Clay Benoit Oct 18 '10 at 17:46
  • The article is very old. The most of it will not work with the current version of IKVM. On the mailing list from IKVM some people has use fop. Your question are missing many critical information like which error do you have receive, which IKVM version do you use, etc. – Horcrux7 Oct 19 '10 at 15:44
  • Correct - the article is old, but I didn't have luck finding anything else. I have tried to translate the commands and syntax in the "old" article using IKVM 0.44.0.5. There are multiple warnings during the IKVMC execution, but no errors. When I added the generated DLLs to my C#.NET project, I do not have access to the FopFactory and other classes documented in the ApacheFOP library. – Clay Benoit Oct 20 '10 at 18:27

6 Answers6

8

(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
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
Clay Benoit
  • 223
  • 1
  • 2
  • 11
  • +1. This worked for me as well using FOP 1.0 and IKVM 0.46.0.1. During the conversion I had a lot of warnings scroll by but everything seems to work. Using the example code from the [FOP embedding instructions](http://xmlgraphics.apache.org/fop/1.0/embedding.html) worked with very little modification (needed necessary USING statements, and specified the MIME type using a string). – Steve Kalemkiewicz Oct 13 '11 at 21:45
  • You can ignore the `-reference` switch, that caused errors for me. But it further worked with latest fop trunk of today and latest ikvm. Performance looks to be almost equal, .net a bit slower but using less memory. I used a one line commandline app that just calls the converted fops Main.main method. – Peterdk Jul 17 '12 at 15:23
6

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

binjiezhao
  • 567
  • 8
  • 12
4

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();
        }
    }
  • The `newInstance` method in `FopFactory fopFactory = FopFactory.newInstance();` expects at least one parameter. Probably a breaking change that is introduced afterwards – Memet Olsen Jan 26 '16 at 08:39
1

I know this is an old post, but this post does not have a solution for .Net Core. I have created an open-source pure port of Apache FOP version 2.8 that runs in .Net Core. Check it out:

FOP.NetCore
https://github.com/sorcerdon/FOP.NetCore

Here is the nuget:

NuGet version (FOP.NetCore)

Since this is a pure port, that means that it can do anything that Apache FOP can do.

Simcha Khabinsky
  • 1,970
  • 2
  • 19
  • 34
0

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).

Styr
  • 1
0

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".

jahuer1
  • 377
  • 4
  • 15
  • Note, that you need the jars in `lib` not `lib\build`. This caused some class not found when running the .net version of fop. – Peterdk Jul 17 '12 at 15:25