如何在FopFactory中将MemoryStream传输到StreamSource?

时间:2016-11-10 17:10:46

标签: java apache-fop

我在使用连接包IKVM和FOP运行的FopFactory中使用以下代码:

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();
    }
}

如何将MemoryStream inpFile传输到FopFactory中的javax.xml.transform.stream.StreamSource?

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我从这里使用了课程DotNetInputStream。在我的代码中,我补充说:

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

并在FopFactory中将InputStream传输到StreamSource:

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