Calling ApacheFOP from .NET

This is an follow-up comment to the article IKVM, which describes ‘calling Apache FOP from C# or VB.NET’.

Just some additional comments.

1.Along with other steps specified in the article, while compiling fop.jar into .NET assembley, dont forget to add reference to the .dll file avalon-framework-cvs-20020806.dll, otherwise you will get an error saying
java.lang.NoClassDefFoundError: org.apache.avalon.framework.logger.ConsoleLogger .

ikvmc -target:library
-reference:IKVM.GNU.Classpath.dll -reference:xml-apis.dll
-reference:batik.dll -reference:avalon-framework-cvs-20020806.dll
fop.jar

2. Atleast in my PC, the fop.jar file from apache site itself is working fine. You dont need to download the patched fop.jar

3. Calling Apache FOP from C# web application

This is a sample code snippet which shows how to call Apache FOP from C#.NET, the below code will convert a XML file to PDF. The xml file is an pre generated xsl:fo file.


using org.apache.fop.apps;
using ikvm.lang;

private void Page_Load(object sender, System.EventArgs e)
{
try{

//Put user code to initialize the page here
//Load assemblies that are not directly
//referenced since no CLASSPATH is defined
AppDomain.CurrentDomain.Load(“xercesImpl-2.2.1”);
AppDomain.CurrentDomain.Load(“batik”);
java.lang.System.setProperty(“javax.xml.parsers.SAXParserFactory”,”org.apache.xerces.jaxp.SAXParserFactoryImpl”);

java.io.ByteArrayOutputStream bArrayOS = new java.io.ByteArrayOutputStream();
// specify the .fo file location (relative or absolute)
org.xml.sax.InputSource isrc = new org.xml.sax.InputSource(“/files/xslt/sample.fo”);

Driver driver = new Driver(isrc, bArrayOS);
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();

bArrayOS.close();
byte[] bArray = bArrayOS.toByteArray();

Response.ClearHeaders();
Response.Clear();
Response.ContentType = “application/octet-stream”;
Response.AddHeader(“Content-Disposition”, “attachment;
filename=foo.pdf”);
Response.Flush();
Response.BinaryWrite(bArray);
Response.End();

}catch(Exception ex){ Response.Write(ex.ToString());}

}


Anyway a C# port for Apache FOP would be much better for debugging purpose.

Related Posts

Comments (2)

Did you try embedding a picture?
I failed doing that in the IKVM-FOP-Version.
Yes, I got the path/url right. (The error message for nothing to be found at the given path/url is different).

I have something like

(or somejpg.jpg)

I get the error message:
[ERROR] Error in XObject : Error while loading image file:somejpg.jpg : class java.lang.ClassCastException – null

(same for somegif.gif)

It works perfectly fine when running in pure java.

If it works for you: Which builds of IKVM/GNU ClassPath are you using exactly?

I tried it before and got the same error. Its seems to appear because of batik.dll. Didn’t find enough time to debug it. If you happen to ride of it, plz let me know as well.
Thank you.

Leave a comment