Nineor Nineor - 23 days ago 6
Java Question

How to transfer MemoryStream to StreamSource in FopFactory?

I am using the following code in FopFactory running with connected packages IKVM and FOP:

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

public void PDF(MemoryStream inpFile, 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(inpFile);
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();
}
}


How to transfer MemoryStream inpFile to javax.xml.transform.stream.StreamSource in FopFactory?

Answer

I found a solution to my problem. I used the class DotNetInputStream from here. In my code, I added:

java.io.InputStream streamFile = new DotNetInputStream(inpFile);

And transfer InputStream to StreamSource in FopFactory:

javax.xml.transform.Source src = new javax.xml.transform.stream.StreamSource(streamFile);