Example #1
0
        private void gsProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            /* Callback with progress */
            gsParams_t  Value = new gsParams_t();
            gsEventArgs info  = new gsEventArgs(false, e.ProgressPercentage, Value);

            gsUpdateMain(this, info);
        }
Example #2
0
        public gsStatus CreateXPS(String fileName, int resolution, int num_pages)
        {
            gsParams_t gsparams = new gsParams_t();

            gsparams.init_file       = null;
            gsparams.init_string     = null;
            gsparams.device          = gsDevice_t.xpswrite;
            gsparams.outputfile      = null;
            gsparams.resolution      = resolution;
            gsparams.inputfile       = fileName;
            gsparams.task            = GS_Task_t.CREATE_XPS;
            gsparams.num_pages       = num_pages;
            gsparams.options         = "-dNOCACHE";
            gsparams.need_multi_page = false;
            gsparams.pages           = null;
            gsparams.firstpage       = -1;
            gsparams.lastpage        = -1;
            gsparams.currpage        = -1;
            return(RunGhostscript(gsparams));
        }
Example #3
0
        public gsStatus DistillPS(String fileName, int resolution)
        {
            gsParams_t gsparams = new gsParams_t();;

            gsparams.init_file       = null;
            gsparams.init_string     = null;
            gsparams.device          = gsDevice_t.pdfwrite;
            gsparams.devicename      = null;
            gsparams.outputfile      = null;
            gsparams.resolution      = resolution;
            gsparams.inputfile       = fileName;
            gsparams.num_pages       = -1;
            gsparams.task            = GS_Task_t.PS_DISTILL;
            gsparams.options         = "";
            gsparams.need_multi_page = false;
            gsparams.pages           = null;
            gsparams.firstpage       = -1;
            gsparams.lastpage        = -1;
            gsparams.currpage        = -1;
            return(RunGhostscript(gsparams));
        }
Example #4
0
        public gsStatus Convert(String fileName, String options, String device,
                                String outputFile, int num_pages, int resolution,
                                bool multi_page_needed, System.Collections.IList pages,
                                int firstpage, int lastpage, String init_file, String init_string)
        {
            gsParams_t gsparams = new gsParams_t();

            gsparams.init_file       = init_file;
            gsparams.init_string     = init_string;
            gsparams.devicename      = device;
            gsparams.outputfile      = outputFile;
            gsparams.inputfile       = fileName;
            gsparams.task            = GS_Task_t.SAVE_RESULT;
            gsparams.num_pages       = num_pages;
            gsparams.options         = options;
            gsparams.resolution      = resolution;
            gsparams.need_multi_page = multi_page_needed;
            gsparams.pages           = pages;
            gsparams.firstpage       = firstpage;
            gsparams.lastpage        = lastpage;
            gsparams.currpage        = 1;
            return(RunGhostscript(gsparams));
        }
Example #5
0
        private gsStatus RunGhostscript(gsParams_t Params)
        {
            try
            {
                if (m_worker != null && m_worker.IsBusy)
                {
                    m_worker.CancelAsync();
                    return(gsStatus.GS_BUSY);
                }
                if (m_worker == null)
                {
                    m_worker = new BackgroundWorker();
                    m_worker.WorkerReportsProgress      = true;
                    m_worker.WorkerSupportsCancellation = true;
                    m_worker.RunWorkerCompleted        += new RunWorkerCompletedEventHandler(gsCompleted);
                    m_worker.ProgressChanged           += new ProgressChangedEventHandler(gsProgressChanged);
                }

                if (Params.task == GS_Task_t.PS_DISTILL)
                {
                    m_worker.DoWork += new DoWorkEventHandler(gsWork2);
                }
                else
                {
                    m_worker.DoWork += new DoWorkEventHandler(gsWork1);
                }

                m_params = Params;
                m_worker.RunWorkerAsync(Params);
                return(gsStatus.GS_READY);
            }
            catch (OutOfMemoryException e)
            {
                Console.WriteLine("Memory allocation failed during gs rendering\n");
                return(gsStatus.GS_ERROR);
            }
        }
Example #6
0
 public gsEventArgs(bool completed, int progress, gsParams_t param)
 {
     m_completed = completed;
     m_progress  = progress;
     m_param     = param;
 }
Example #7
0
		/* GS Result*/
		public void GSResult(gsParams_t gs_result)
		{
			if (gs_result.result == GS_Result_t.gsCANCELLED)
			{
				xaml_DistillGrid.Visibility = System.Windows.Visibility.Collapsed;
				return;
			}
			if (gs_result.result == GS_Result_t.gsFAILED)
			{
				xaml_DistillGrid.Visibility = System.Windows.Visibility.Collapsed;
				ShowMessage(NotifyType_t.MESS_STATUS, "GS Failed Conversion");
				return;
			}
			switch (gs_result.task)
			{
				case GS_Task_t.CREATE_XPS:
					xaml_DistillGrid.Visibility = System.Windows.Visibility.Collapsed;
					PrintXPS(gs_result.outputfile);
					break;

				case GS_Task_t.PS_DISTILL:
					xaml_DistillGrid.Visibility = System.Windows.Visibility.Collapsed;
					m_origfile = gs_result.inputfile;
					OpenFile2(gs_result.outputfile);
					break;

				case GS_Task_t.SAVE_RESULT:
					ShowMessage(NotifyType_t.MESS_STATUS, "GS Completed Conversion");
					break;
			}
		}
Example #8
0
        public gsStatus Convert(String fileName, String options, String device, 
								String outputFile, int num_pages, int resolution,
								bool multi_page_needed, System.Collections.IList pages, 
								int firstpage, int lastpage, String init_file, String init_string)
        {
            gsParams_t gsparams = new gsParams_t();

            gsparams.init_file = init_file;
            gsparams.init_string = init_string;
            gsparams.devicename = device;
            gsparams.outputfile = outputFile;
            gsparams.inputfile = fileName;
            gsparams.task = GS_Task_t.SAVE_RESULT;
            gsparams.num_pages = num_pages;
            gsparams.options = options;
            gsparams.resolution = resolution;
            gsparams.need_multi_page = multi_page_needed;
            gsparams.pages = pages;
            gsparams.firstpage = firstpage;
            gsparams.lastpage = lastpage;
            gsparams.currpage = 1;
            return RunGhostscript(gsparams);
        }
Example #9
0
 private void gsProgressChanged(object sender, ProgressChangedEventArgs e)
 {
     /* Callback with progress */
     gsParams_t Value = new gsParams_t();
     gsEventArgs info = new gsEventArgs(false, e.ProgressPercentage, Value);
     gsUpdateMain(this, info);
 }
Example #10
0
        private gsStatus RunGhostscript(gsParams_t Params)
        {
            try
            {
                if (m_worker != null && m_worker.IsBusy)
                {
                    m_worker.CancelAsync();
                    return gsStatus.GS_BUSY;
                }
                if (m_worker == null)
                {
                    m_worker = new BackgroundWorker();
                    m_worker.WorkerReportsProgress = true;
                    m_worker.WorkerSupportsCancellation = true;
                    m_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(gsCompleted);
                    m_worker.ProgressChanged += new ProgressChangedEventHandler(gsProgressChanged);
                }

                if (Params.task == GS_Task_t.PS_DISTILL)
                    m_worker.DoWork += new DoWorkEventHandler(gsWork2);
                else
                    m_worker.DoWork += new DoWorkEventHandler(gsWork1);

                m_params = Params;
                m_worker.RunWorkerAsync(Params);
                return gsStatus.GS_READY;
            }
            catch (OutOfMemoryException e)
            {
                Console.WriteLine("Memory allocation failed during gs rendering\n");
                return gsStatus.GS_ERROR;
            }
        }
Example #11
0
        /* Callback */
        private void gsCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            gsParams_t Value;
            gsEventArgs info;
            gsParams_t Params;

            try
            {
                Params = (gsParams_t)e.Result;
            }
            catch(System.Reflection.TargetInvocationException ee)
            {
                /* Something went VERY wrong with GS */
                /* Following is to help debug these issues */
                /* var inner = ee.InnerException;
                var message = ee.Message;
                var inner_message = inner.Message;
                String bound = "\n************\n";
                gsIOUpdateMain(this, bound, bound.Length);
                gsIOUpdateMain(this, message, message.Length);
                gsIOUpdateMain(this, bound, bound.Length);
                gsIOUpdateMain(this, inner_message, inner_message.Length);
                gsIOUpdateMain(this, bound, bound.Length);
                var temp = inner.Source;
                gsIOUpdateMain(this, bound, bound.Length);
                gsIOUpdateMain(this, temp, temp.Length);
                var method = inner.TargetSite;
                gsIOUpdateMain(this, bound, bound.Length);
                var method_name = method.Name;
                gsIOUpdateMain(this, method_name, method_name.Length);
                var stack = inner.StackTrace;
                gsIOUpdateMain(this, bound, bound.Length);
                gsIOUpdateMain(this, stack, stack.Length); */
                String output = "Ghostscript DLL Invalid Access.";
                gsDLLProblemMain(this, output);
                return;
            }

            if (Params.task == GS_Task_t.PS_DISTILL)
                m_worker.DoWork -= new DoWorkEventHandler(gsWork2);
            else
                m_worker.DoWork -= new DoWorkEventHandler(gsWork1);

            if (e.Cancelled)
            {
                Value = new gsParams_t();
                Value.result = GS_Result_t.gsCANCELLED;
                info = new gsEventArgs(true, 100, Value);
            }
            else
            {
                Value = (gsParams_t)e.Result;
                info = new gsEventArgs(true, 100, Value);
            }
            gsUpdateMain(this, info);
        }
Example #12
0
        public gsStatus DistillPS(String fileName, int resolution)
        {
            gsParams_t gsparams = new gsParams_t(); ;

            gsparams.init_file = null;
            gsparams.init_string = null;
            gsparams.device = gsDevice_t.pdfwrite;
            gsparams.devicename = null;
            gsparams.outputfile = null;
            gsparams.resolution = resolution;
            gsparams.inputfile = fileName;
            gsparams.num_pages = -1;
            gsparams.task = GS_Task_t.PS_DISTILL;
            gsparams.options = "";
            gsparams.need_multi_page = false;
            gsparams.pages = null;
            gsparams.firstpage = -1;
            gsparams.lastpage = -1;
            gsparams.currpage = -1;
            return RunGhostscript(gsparams);
        }
Example #13
0
        public gsStatus CreateXPS(String fileName, int resolution, int num_pages)
        {
            gsParams_t gsparams = new gsParams_t();

            gsparams.init_file = null;
            gsparams.init_string = null;
            gsparams.device = gsDevice_t.xpswrite;
            gsparams.outputfile = null;
            gsparams.resolution = resolution;
            gsparams.inputfile = fileName;
            gsparams.task = GS_Task_t.CREATE_XPS;
            gsparams.num_pages = num_pages;
            gsparams.options = "-dNOCACHE";
            gsparams.need_multi_page = false;
            gsparams.pages = null;
            gsparams.firstpage = -1;
            gsparams.lastpage = -1;
            gsparams.currpage = -1;
            return RunGhostscript(gsparams);
        }
Example #14
0
        /* A standard command line approach to using gs API */
        private void gsWork1(object sender, DoWorkEventArgs e)
        {
            gsParams_t    gsparams   = (gsParams_t)e.Argument;
            String        out_file   = gsparams.outputfile;
            String        in_file    = gsparams.inputfile;
            int           num_params = 8;    /* base number */
            int           rend_count = 1;
            String        options;
            int           count;
            List <String> optionlist;

            optionlist = GetOptions(gsparams.options);
            num_params = num_params + optionlist.Count;
            if (gsparams.pages != null)
            {
                rend_count = gsparams.pages.Count;
                num_params = num_params + 2;
            }
            if (gsparams.init_file != null)
            {
                num_params = num_params + 1;
            }
            if (gsparams.init_string != null)
            {
                num_params = num_params + 2;
            }

            var argParam = new GCHandle[num_params];
            var argPtrs  = new IntPtr[num_params];

            String[]      strParams      = new String[num_params];
            List <byte[]> CharacterArray = new List <byte[]>(num_params);
            GCHandle      argPtrsStable;

            /* New instance */
            int code = tc_gsapi_new_instance(out gsInstance, IntPtr.Zero);

            if (code < 0)
            {
                gsparams.result = GS_Result_t.gsFAILED;
                e.Result        = gsparams;
                return;
            }

            code = tc_gsapi_set_stdio(gsInstance, RaiseStdInCallback,
                                      RaiseStdOutCallback, RaiseStdErrCallback);
            code = tc_gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);

            if (code == 0)
            {
                for (int jj = 0; jj < rend_count; jj++)
                {
                    strParams[0] = "gs";                       /* This does not matter */
                    strParams[1] = "-dNOPAUSE";
                    strParams[2] = "-dBATCH";
                    if (gsparams.devicename != null)
                    {
                        strParams[3] = "-sDEVICE=" + gsparams.devicename;
                    }
                    else
                    {
                        strParams[3] = "-sDEVICE=" + Enum.GetName(typeof(gsDevice_t), gsparams.device);
                    }
                    strParams[4] = "-r" + gsparams.resolution;
                    /* Create temp file if file not specified */
                    if (out_file == null)
                    {
                        out_file            = Path.GetTempFileName();
                        gsparams.outputfile = out_file;
                    }
                    count = 5;
                    /* Add in the options */
                    for (int kk = 0; kk < optionlist.Count; kk++)
                    {
                        strParams[count] = optionlist[kk];
                        count++;
                    }
                    /* We have discontinuous page selection */
                    if (gsparams.pages != null)
                    {
                        String firstpage, lastpage;
                        options = gsparams.options;
                        SelectPage curr_page = (SelectPage)(gsparams.pages[jj]);
                        firstpage        = "-dFirstPage=" + curr_page.Page;
                        lastpage         = "-dLastPage=" + curr_page.Page;
                        strParams[count] = firstpage;
                        count++;
                        strParams[count] = lastpage;
                        count++;
                        /* Look for file extension. */
                        string extension    = System.IO.Path.GetExtension(out_file);
                        int    len          = extension.Length;
                        String new_out_file = out_file.Substring(0, out_file.Length - len);
                        strParams[count] = "-o" + new_out_file + "_page" + curr_page.Page + extension;
                    }
                    else
                    {
                        if (gsparams.need_multi_page)
                        {
                            /* Look for file extension. */
                            string extension    = System.IO.Path.GetExtension(out_file);
                            int    len          = extension.Length;
                            String new_out_file = out_file.Substring(0, out_file.Length - len);
                            strParams[count] = "-o" + new_out_file + "_page%d" + extension;
                        }
                        else
                        {
                            strParams[count] = "-o" + out_file;
                        }
                    }
                    if (gsparams.init_string != null)
                    {
                        count++;
                        strParams[count] = "-c";
                        count++;
                        strParams[count] = gsparams.init_string;
                    }
                    count++;
                    strParams[count] = "-f";
                    if (gsparams.init_file != null)
                    {
                        count++;
                        strParams[count] = gsparams.init_file;
                    }
                    count++;
                    strParams[count] = in_file;

                    /* Now convert our Strings to char* and get pinned handles to these.
                     * This keeps the c# GC from moving stuff around on us */
                    for (int k = 0; k < num_params; k++)
                    {
                        CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes(strParams[k].ToCharArray()));
                        argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                        argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    }
                    /* Also stick the array of pointers into memory that will not be GCd */
                    argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                    code = tc_gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                    /* All the pinned items need to be freed so the GC can do its job */
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();
                    /* Free the character array list in case we have multiple runs */
                    CharacterArray.Clear();

                    if (code < 0)
                    {
                        break;
                    }
                }
            }

            int code1 = tc_gsapi_exit(gsInstance);

            if ((code == 0) || (code == gsConstants.E_QUIT))
            {
                code = code1;
            }

            tc_gsapi_delete_instance(gsInstance);
            if ((code == 0) || (code == gsConstants.E_QUIT))
            {
                gsparams.result = GS_Result_t.gsOK;
                e.Result        = gsparams;
                return;
            }

            gsparams.result = GS_Result_t.gsFAILED;
            e.Result        = gsparams;
            return;
        }
Example #15
0
        /* Callback */
        private void gsCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            gsParams_t  Value;
            gsEventArgs info;
            gsParams_t  Params;

            try
            {
                Params = (gsParams_t)e.Result;
            }
            catch (System.Reflection.TargetInvocationException ee)
            {
                /* Something went VERY wrong with GS */
                /* Following is to help debug these issues */

                /* var inner = ee.InnerException;
                 * var message = ee.Message;
                 * var inner_message = inner.Message;
                 * String bound = "\n************\n";
                 * gsIOUpdateMain(this, bound, bound.Length);
                 * gsIOUpdateMain(this, message, message.Length);
                 * gsIOUpdateMain(this, bound, bound.Length);
                 * gsIOUpdateMain(this, inner_message, inner_message.Length);
                 * gsIOUpdateMain(this, bound, bound.Length);
                 * var temp = inner.Source;
                 * gsIOUpdateMain(this, bound, bound.Length);
                 * gsIOUpdateMain(this, temp, temp.Length);
                 * var method = inner.TargetSite;
                 * gsIOUpdateMain(this, bound, bound.Length);
                 * var method_name = method.Name;
                 * gsIOUpdateMain(this, method_name, method_name.Length);
                 * var stack = inner.StackTrace;
                 * gsIOUpdateMain(this, bound, bound.Length);
                 * gsIOUpdateMain(this, stack, stack.Length); */
                String output = "Ghostscript DLL Invalid Access.";
                gsDLLProblemMain(this, output);
                return;
            }

            if (Params.task == GS_Task_t.PS_DISTILL)
            {
                m_worker.DoWork -= new DoWorkEventHandler(gsWork2);
            }
            else
            {
                m_worker.DoWork -= new DoWorkEventHandler(gsWork1);
            }

            if (e.Cancelled)
            {
                Value        = new gsParams_t();
                Value.result = GS_Result_t.gsCANCELLED;
                info         = new gsEventArgs(true, 100, Value);
            }
            else
            {
                Value = (gsParams_t)e.Result;
                info  = new gsEventArgs(true, 100, Value);
            }
            gsUpdateMain(this, info);
        }
Example #16
0
        /* Feeding gs piecemeal so that we can have some progress callback */
        /* Used only for PS Distill */
        private void gsWork2(object sender, DoWorkEventArgs e)
        {
            gsParams_t Params     = (gsParams_t)e.Argument;
            String     out_file   = Params.outputfile;
            String     in_file    = Params.inputfile;
            int        num_params = 6;

            if (Params.options.Length > 0)
            {
                num_params = num_params + 1;
            }

            int exitcode = 0;
            var argParam = new GCHandle[num_params];
            var argPtrs  = new IntPtr[num_params];
            var Feed     = new GCHandle();
            var FeedPtr  = new IntPtr();

            String[]      strParams      = new String[num_params];
            List <byte[]> CharacterArray = new List <byte[]>(num_params);
            GCHandle      argPtrsStable;

            Byte[]           Buffer = new Byte[gsConstants.GS_READ_BUFFER];
            BackgroundWorker worker = sender as BackgroundWorker;

            /* Open the file */
            var fs  = new FileStream(in_file, FileMode.Open);
            var len = (int)fs.Length;
            /* New instance */
            int code = tc_gsapi_new_instance(out gsInstance, IntPtr.Zero);

            if (code < 0)
            {
                Params.result = GS_Result_t.gsFAILED;
                e.Result      = Params;
                return;
            }

            code = tc_gsapi_set_stdio(gsInstance, RaiseStdInCallback,
                                      RaiseStdOutCallback, RaiseStdErrCallback);
            code = tc_gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);

            if (code == 0)
            {
                strParams[0] = "gs";                   /* This does not matter */
                strParams[1] = "-dNOPAUSE";
                strParams[2] = "-dBATCH";
                if (Params.devicename != null)
                {
                    strParams[3] = "-sDEVICE=" + Params.devicename;
                }
                else
                {
                    strParams[3] = "-sDEVICE=" + Enum.GetName(typeof(gsDevice_t), Params.device);
                }
                strParams[4] = "-r" + Params.resolution;
                /* Create temp file if file not specified */
                if (out_file == null)
                {
                    out_file          = Path.GetTempFileName();
                    Params.outputfile = out_file;
                }
                if (Params.options.Length > 0)
                {
                    strParams[5] = Params.options;
                    strParams[6] = "-o" + out_file;
                }
                else
                {
                    strParams[5] = "-o" + out_file;
                }

                /* Now convert our Strings to char* and get pinned handles to these.
                 * This keeps the c# GC from moving stuff around on us */
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes(strParams[k].ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                }
                /* Also stick the array of pointers into memory that will not be GCd */
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                code = tc_gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());

                /* First pin the data buffer */
                Feed    = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
                FeedPtr = Feed.AddrOfPinnedObject();

                /* Now start feeding the input piece meal and do a call back
                 * with our progress */
                if (code == 0)
                {
                    int    count;
                    double perc;
                    int    total = 0;

                    tc_gsapi_run_string_begin(gsInstance, 0, ref exitcode);
                    while ((count = fs.Read(Buffer, 0, gsConstants.GS_READ_BUFFER)) > 0)
                    {
                        tc_gsapi_run_string_continue(gsInstance, FeedPtr, count, 0, ref exitcode);
                        if (exitcode < 0)
                        {
                            code = exitcode;
                            break;
                        }
                        total = total + count;
                        perc  = 100.0 * (double)total / (double)len;
                        worker.ReportProgress((int)perc);
                        if (worker.CancellationPending == true)
                        {
                            e.Cancel = true;
                            break;
                        }
                    }
                    tc_gsapi_run_string_end(gsInstance, 0, ref exitcode);
                    if (code == 0)
                    {
                        code = exitcode;
                    }
                }

                /* All the pinned items need to be freed so the GC can do its job */
                for (int k = 0; k < num_params; k++)
                {
                    argParam[k].Free();
                }
                argPtrsStable.Free();
                Feed.Free();
            }

            int code1 = tc_gsapi_exit(gsInstance);

            if ((code == 0) || (code == gsConstants.E_QUIT))
            {
                code = code1;
            }

            tc_gsapi_delete_instance(gsInstance);
            if ((code == 0) || (code == gsConstants.E_QUIT))
            {
                Params.result = GS_Result_t.gsOK;
                e.Result      = Params;
                return;
            }
            Params.result = GS_Result_t.gsFAILED;
            e.Result      = Params;
            return;
        }
Example #17
0
 public gsEventArgs(bool completed, int progress, gsParams_t param)
 {
     m_completed = completed;
     m_progress = progress;
     m_param = param;
 }