Hello, to convert files from XSL-FO format to PDF format, use the following code that works with connected IKVM and FOP packages:

using org.apache.fop.tools; using org.apache.fop.apps; using org.xml.sax; using java.io; public void PDF(string 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(new java.io.File(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(); } } 

The variable inpFile passes the path to the XSL-FO file. How to make inpFile transfer a file via a MemoryStream to a StreamSource?

UPD: I mean, how to implement stream transfer when the parameter value in the method has already been assigned the type Memorystream, i.e. worth PDF (Memorystream inpFile, string pdfFile). And if you simply pass it to javax.xml.transform.stream.StreamSource (inpFile), then an error is output because This class requires the string value of the parameter.

    1 answer 1

    Like so

     Source source = new StreamSource(new StringReader(foString)); transformer.transform(source, res); 
    • Not exactly what I meant - explained in the first message. - Nineor
    • What is not happy? In the above example, you pass to the SteramSource constructor the contents of the file as a string, which can be formed in any way you like. There is no need for this file. - Igor Kudryashov
    • Because in a string data type you can put an array of only a certain length. What to do with files that contain text of greater length is unknown (although it is unlikely that files of such length will occur, but ideally you need to foresee this). When the array size in MemoryStream is limited only by RAM ... - Nineor
    • "array of only a certain length" - I do not know how it is in C #, but in Java it is about 2GB. Well, if the file really exists somewhere in the file system, then you can still build a StreamSource from the file read stream. And on account of MemoryStream I can not help, because I do not own C #, but there is no such stream in Java. - Igor Kudryashov