private void PDF_Click(object sender, RoutedEventArgs e) { PDF.IsEnabled = false; string filename = ConfigurationManager.AppSettings["file"].Split('.')[0]; string fileextension = "." + ConfigurationManager.AppSettings["file"].Split('.')[1]; string saved_file = String.Format(@"./{0}_{1}_{2}{3}", filename, DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString().Replace(':', '.'), fileextension); string pdf_file = String.Format(@"./{0}_{1}_{2}.pdf", filename, DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString().Replace(':', '.'), fileextension); SaveFile sf = new SaveFile(saved_file, document); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("./report.xslt"); xslt.Transform(saved_file, "./report.xml"); xslt.Load("./pdf.xslt"); xslt.Transform("./report.xml", "./report.fo"); FopFactory fopFactory = FopFactory.newInstance(new java.net.URI(".")); //in this stream we will get the generated pdf file OutputStream o = new DotNetOutputMemoryStream(); try { Fop fop = fopFactory.newFop("application/pdf", o); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); //read the template from disc Source src = new StreamSource(new File("report.fo")); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); } finally { o.close(); } using (System.IO.FileStream fs = System.IO.File.Create(pdf_file)) { //write from the .NET MemoryStream stream to disc the generated pdf file var data = ((DotNetOutputMemoryStream)o).Stream.GetBuffer(); fs.Write(data, 0, data.Length); } PDF.IsEnabled = true; }
/// <summary> /// Get the Area Tree from Antenna House Formatter /// </summary> /// <param name="inputStream">XSL:FO tree as ByteArryInputStream</param> /// <param name="configFileName">FOP configuration file name: Setting file for Antenna House Formatter rendering</param> /// <returns></returns> protected XPathDocument GetAreaTrea(ByteArrayInputStream inputStream, string configFileName) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); FOUserAgent userAgent = new FOUserAgent(FopFactory.newInstance()); FopFactoryConfigurator configurator = new FopFactoryConfigurator(userAgent.getFactory()); if (string.IsNullOrEmpty(configFileName) == false) { userAgent.getFactory().setUserConfig(configFileName); } Fop fop = userAgent.getFactory().newFop(MimeConstants.__Fields.MIME_FOP_AREA_TREE, userAgent, outStream); TransformerFactory tf = new TransformerFactoryImpl(); Transformer transformer = tf.newTransformer(); Source src = new StreamSource(inputStream); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); return(outStream.ToXPathDocument()); }
/// <summary> /// Start this from directory \samples\samples\fop\ /// with \samples\lib populated with jni4net.j.jar and jni4net.n.dll /// and with \samples\samples\fop\lib populated with FOP jar files. /// </summary> private static void Main() { FixStartupDirectory(); // automaticaly setup Java classpath to find jni4net.j var setup = new BridgeSetup(true); // setup Java classpath to find FOP libraries setup.AddAllJarsClassPath("lib"); // we don't need to call back from Java setup.BindStatic = false; // now we create JVM and bind jni4net core Bridge.CreateJVM(setup); // now we bind all proxies of FOP objects // which are compiled in this assembly Bridge.RegisterAssembly(typeof(Program).Assembly); const string inFileName = "data/jni4net.fo"; const string outFileName = "data/jni4net.pdf"; //Below is just plain Copy&Paste of FOP basic sample java code OutputStream output = null; try { // Step 1: Construct a FopFactory // (reuse if you plan to render multiple documents!) FopFactory fopFactory = FopFactory.newInstance(); // Step 2: Set up output stream. output = new BufferedOutputStream(new FileOutputStream(new File(outFileName))); // Step 3: Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants_.MIME_PDF, output); // Step 4: Setup JAXP using identity transformer TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // identity transformer // Step 5: Setup input and output for XSLT transformation // Setup input stream Source src = new StreamSource(new File(inFileName)); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Step 6: Start XSLT transformation and FOP processing transformer.transform(src, res); } finally { // Clean-up if (output != null) { output.close(); } } }
public static void GeneratePdf(string foFile, string pdfFile) { OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new java.io.File(pdfFile))); try { FopFactory fopFactory = FopFactory.newInstance(new java.net.URI(".")); Fop fop = fopFactory.newFop("application/pdf", outputStream); javax.xml.transform.TransformerFactory factory = javax.xml.transform.TransformerFactory.newInstance(); javax.xml.transform.Transformer transformer = factory.newTransformer(); javax.xml.transform.Source source = new javax.xml.transform.stream.StreamSource(new java.io.File(foFile)); javax.xml.transform.Result result = new javax.xml.transform.sax.SAXResult(fop.getDefaultHandler()); transformer.transform(source, result); } finally { outputStream.close(); } }