using System;
using System.IO;
using Ecrion.Ultrascale;

namespace DesktopPrintSample
{
    /// <summary>
    /// This sample will convert an input document (XSL-FO, WordML, DOCX, etc.) to 
    /// one of the supported output formats (PDF, AFP, Postscript, WordML, Silverlight, etc)
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.Error.WriteLine("Usage: DesktopPrintSample.exe input_file printer_name");
                return;
            }

            String printerName = null;

            if (args.Length == 2)
                printerName = args[1];

            Console.Out.WriteLine(String.Format("Printing '{0}' to '{1}' ...", args[0], printerName != null ? printerName: "default printer"));

            try
            {
                // Upon aquiring a license, this is where the license key should be supplied to the engine.
                //Ecrion.Ultrascale.Settings.LicenseKey = "...";

                

                RenderingParameters renderParams = new RenderingParameters();

                renderParams.PreloadDocument = true;                
                renderParams.OutputFormat = Engine.OutputFormat.PRN;
                renderParams.BaseUrl = Path.GetDirectoryName(Path.GetFullPath(args[0]));
                renderParams.PrinterSettings.PrinterName = printerName;

                Engine engine = new Engine();

                Engine.InputFormat inputFormat = Engine.InputFormat.XSLFO;    // Change the input format here according to your input file
                Stream inputStm = new FileStream(args[0], FileMode.Open, FileAccess.Read);
                XmlDataSource dataSource = new XmlDataSource(inputStm, inputFormat);

                engine.Render(dataSource, null, renderParams);
                Console.Out.WriteLine("Printing succeeded!");
            }
            catch (Exception e)
            {   // Catch and display any errors that may occur
                Console.Error.WriteLine("Error occured: " + e.Message);
            }
        }
    }
}
