コード例 #1
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Use syncronous call to ghostscript to get the
         * number of pages in a PDF file */
        public int GetPageCount(String fileName)
        {
            gsParamState_t gsparams = new gsParamState_t();
            gsParamState_t result;

            gsparams.args = new List <string>();

            gsparams.args.Add("gs");
            gsparams.args.Add("-dNODISPLAY");
            gsparams.args.Add("-dNOPAUSE");
            gsparams.args.Add("-dBATCH");
            gsparams.args.Add("-I%rom%Resource/Init/");
            //gsparams.args.Add("-q");
            gsparams.args.Add("-sFile=\"" + fileName + "\"");
            gsparams.args.Add("--permit-file-read=\"" + fileName + "\"");
            gsparams.args.Add("-c");
            gsparams.args.Add("\"File (r) file runpdfbegin pdfpagecount = quit\"");
            gsparams.task = GS_Task_t.GET_PAGE_COUNT;
            m_params      = gsparams;

            result = gsFileSync(gsparams);

            if (result.result == GS_Result_t.gsOK)
            {
                return(m_params.num_pages);
            }
            else
            {
                return(-1);
            }
        }
コード例 #2
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Close the display device and delete the instance */
        public gsParamState_t DisplayDeviceClose()
        {
            int            code       = 0;
            gsParamState_t out_params = new gsParamState_t();

            out_params.result = GS_Result_t.gsOK;

            try
            {
                int code1 = GSAPI.gsapi_exit(dispInstance);
                if ((code == 0) || (code == gsConstants.E_QUIT))
                {
                    code = code1;
                }

                GSAPI.gsapi_delete_instance(dispInstance);
                dispInstance = IntPtr.Zero;
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
                out_params.result = GS_Result_t.gsFAILED;
            }
            return(out_params);
        }
コード例 #3
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Launch a thread rendering a set of pages with the display device.  For use with languages
         * that can be indexed via pages which include PDF and XPS */
        public gsStatus gsDisplayDeviceRenderPages(String fileName, int first_page, int last_page, double zoom)
        {
            gsParamState_t gsparams = new gsParamState_t();
            int            format   = (gsConstants.DISPLAY_COLORS_RGB |
                                       gsConstants.DISPLAY_DEPTH_8 |
                                       gsConstants.DISPLAY_LITTLEENDIAN);
            int resolution = (int)(72.0 * zoom + 0.5);

            gsparams.args = new List <string>();
            gsparams.args.Add("gs");
            gsparams.args.Add("-dNOPAUSE");
            gsparams.args.Add("-dBATCH");
            gsparams.args.Add("-I%rom%Resource/Init/");
            gsparams.args.Add("-dSAFER");
            gsparams.args.Add("-r" + resolution);
            gsparams.args.Add("-sDEVICE=display");
            gsparams.args.Add("-dDisplayFormat=" + format);
            gsparams.args.Add("-dFirstPage=" + first_page);
            gsparams.args.Add("-dLastPage=" + last_page);
            gsparams.args.Add("-f");
            gsparams.args.Add(fileName);
            gsparams.task     = GS_Task_t.DISPLAY_DEV_PDF;
            gsparams.currpage = first_page - 1;
            m_params.currpage = first_page - 1;

            return(RunGhostscriptAsync(gsparams));
        }
コード例 #4
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Launch a thread rendering all the pages with the display device
         * to collect thumbnail images or full resolution.   */
        public gsStatus gsDisplayDeviceRenderAll(String fileName, double zoom, bool aa, GS_Task_t task)
        {
            gsParamState_t gsparams = new gsParamState_t();
            int            format   = (gsConstants.DISPLAY_COLORS_RGB |
                                       gsConstants.DISPLAY_DEPTH_8 |
                                       gsConstants.DISPLAY_BIGENDIAN);
            int resolution = (int)(72.0 * zoom + 0.5);

            gsparams.args = new List <string>();
            gsparams.args.Add("gs");
            gsparams.args.Add("-dNOPAUSE");
            gsparams.args.Add("-dBATCH");
            gsparams.args.Add("-I%rom%Resource/Init/");
            gsparams.args.Add("-dSAFER");
            gsparams.args.Add("-r" + resolution);
            if (aa)
            {
                gsparams.args.Add("-dTextAlphaBits=4");
                gsparams.args.Add("-dGraphicsAlphaBits=4");
            }
            gsparams.args.Add("-sDEVICE=display");
            gsparams.args.Add("-dDisplayFormat=" + format);
            gsparams.args.Add("-f");
            gsparams.args.Add(fileName);
            gsparams.task     = task;
            gsparams.currpage = 0;
            m_params.currpage = 0;
            return(RunGhostscriptAsync(gsparams));
        }
コード例 #5
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Callback as worker progresses in run string case */
        private void gsProgressChanged(gsParamState_t Params, int percent)
        {
            /* Callback with progress */
            gsThreadCallBack info = new gsThreadCallBack(false, percent, Params);

            Gtk.Application.Invoke(delegate {
                ProgressCallBack(info);
            });
        }
コード例 #6
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Callback upon worker all done */
        private void gsCompleted(gsParamState_t Params)
        {
            gsThreadCallBack info = new gsThreadCallBack(true, 100, Params);

            m_worker_busy = false;
            Gtk.Application.Invoke(delegate {
                ProgressCallBack(info);
            });
        }
コード例 #7
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Call the appropriate worker thread based upon the task
         * that we have to do */
        private gsStatus RunGhostscriptAsync(gsParamState_t Params)
        {
            try
            {
                if (m_worker_busy)
                {
                    return(gsStatus.GS_BUSY);
                }

                switch (Params.task)
                {
                case GS_Task_t.PS_DISTILL:
                    m_worker = new Thread(gsBytesAsync);
                    break;

                case GS_Task_t.DISPLAY_DEV_NON_PDF:
                case GS_Task_t.DISPLAY_DEV_PDF:
                case GS_Task_t.DISPLAY_DEV_THUMBS_NON_PDF:
                case GS_Task_t.DISPLAY_DEV_THUMBS_PDF:
                    m_worker = new Thread(DisplayDeviceAsync);
                    break;

                case GS_Task_t.SAVE_RESULT:
                case GS_Task_t.CREATE_XPS:
                default:
                    m_worker = new Thread(gsFileAsync);
                    break;
                }

                var arguments = new List <object>();
                arguments.Add(Params);
                arguments.Add(this);
                m_worker_busy = true;
                m_worker.Start(arguments);

                return(gsStatus.GS_READY);
            }
            catch (OutOfMemoryException)
            {
                Console.WriteLine("Memory allocation failed during gs rendering\n");
                return(gsStatus.GS_ERROR);
            }
        }
コード例 #8
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Launch a thread rendering all the pages with the display device
         * to distill an input PS file and save as a PDF. */
        public gsStatus DistillPS(String fileName, int resolution)
        {
            gsParamState_t gsparams = new gsParamState_t();

            gsparams.args = new List <string>();

            gsparams.inputfile = fileName;
            gsparams.args.Add("gs");
            gsparams.args.Add("-dNOPAUSE");
            gsparams.args.Add("-dBATCH");
            gsparams.args.Add("-I%rom%Resource/Init/");
            gsparams.args.Add("-dSAFER");
            gsparams.args.Add("-sDEVICE=pdfwrite");
            gsparams.outputfile = Path.GetTempFileName();
            gsparams.args.Add("-o" + gsparams.outputfile);
            gsparams.task = GS_Task_t.PS_DISTILL;

            return(RunGhostscriptAsync(gsparams));
        }
コード例 #9
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
 public gsThreadCallBack(bool completed, int progress, gsParamState_t param)
 {
     m_completed = completed;
     m_progress  = progress;
     m_param     = param;
 }
コード例 #10
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Worker task for using display device */
        private void DisplayDeviceAsync(object data)
        {
            int            code           = 0;
            List <object>  genericlist    = data as List <object>;
            gsParamState_t gsparams       = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = gsparams;
            GCHandle       argPtrsStable  = new GCHandle();
            int            num_params     = gsparams.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            bool           cleanup        = true;

            gsparams.result = GS_Result_t.gsOK;

            try
            {
                code = GSAPI.gsapi_new_instance(out dispInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_new_instance error");
                }

                code = GSAPI.gsapi_set_stdio(dispInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_set_stdio error");
                }

                code = GSAPI.gsapi_set_arg_encoding(dispInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_set_arg_encoding error");
                }

                code = GSAPI.gsapi_set_display_callback(dispInstance, ptr_display_struct);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_set_display_callback error");
                }

                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((gsparams.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + gsparams.args[k];
                }
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(dispInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_init_with_args error");
                }
            }

            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                cleanup         = false;
                Result          = gsparams;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                cleanup         = false;
                Result          = gsparams;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                if (dispInstance != IntPtr.Zero)
                {
                    GSAPI.gsapi_delete_instance(dispInstance);
                }
                dispInstance = IntPtr.Zero;
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                if (dispInstance != IntPtr.Zero)
                {
                    GSAPI.gsapi_delete_instance(dispInstance);
                }
                dispInstance = IntPtr.Zero;
            }
            finally
            {
                if (cleanup)
                {
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();
                    Result = gsparams;

                    if (gsparams.result == GS_Result_t.gsOK && (gsparams.task == GS_Task_t.DISPLAY_DEV_NON_PDF ||
                                                                gsparams.task == GS_Task_t.DISPLAY_DEV_THUMBS_NON_PDF))
                    {
                        gsParamState_t result = DisplayDeviceClose();
                        if (gsparams.result == 0)
                        {
                            gsparams.result = result.result;
                        }
                    }
                }
            }
            gsCompleted(Result);
            return;
        }
コード例 #11
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Processing with gsapi_run_string for callback progress */
        private void gsBytesAsync(object data)
        {
            List <object>  genericlist    = data as List <object>;
            gsParamState_t Params         = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = Params;
            int            num_params     = Params.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            GCHandle       argPtrsStable  = new GCHandle();

            Byte[] Buffer = new Byte[gsConstants.GS_READ_BUFFER];

            int        code     = 0;
            int        exitcode = 0;
            var        Feed     = new GCHandle();
            var        FeedPtr  = new IntPtr();
            FileStream fs       = null;
            bool       cleanup  = true;

            try
            {
                /* Open the file */
                fs = new FileStream(Params.inputfile, FileMode.Open);
                var len = (int)fs.Length;

                code = GSAPI.gsapi_new_instance(out gsInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_new_instance error");
                }
                code = GSAPI.gsapi_set_stdio(gsInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_set_stdio error");
                }
                code = GSAPI.gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_set_arg_encoding error");
                }

                /* Now convert our Strings to char* and get pinned handles to these.
                 * This keeps the c# GC from moving stuff around on us */
                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((Params.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + Params.args[k];
                }

                /* Also stick the array of pointers into memory that will not be GCd */
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_init_with_args error");
                }

                /* Pin 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;

                    GSAPI.gsapi_run_string_begin(gsInstance, 0, ref exitcode);
                    if (exitcode < 0)
                    {
                        code = exitcode;
                        throw new GhostscriptException("gsBytesAsync: gsapi_run_string_begin error");
                    }

                    while ((count = fs.Read(Buffer, 0, gsConstants.GS_READ_BUFFER)) > 0)
                    {
                        GSAPI.gsapi_run_string_continue(gsInstance, FeedPtr, count, 0, ref exitcode);
                        if (exitcode < 0)
                        {
                            code = exitcode;
                            throw new GhostscriptException("gsBytesAsync: gsapi_run_string_continue error");
                        }

                        total = total + count;
                        perc  = 100.0 * (double)total / (double)len;
                        gsProgressChanged(Params, (int)perc);
                    }
                    GSAPI.gsapi_run_string_end(gsInstance, 0, ref exitcode);
                    if (exitcode < 0)
                    {
                        code = exitcode;
                        throw new GhostscriptException("gsBytesAsync: gsapi_run_string_end error");
                    }
                }
            }
            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            catch (Exception except)
            {
                /* Could be a file io issue */
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            finally
            {
                if (cleanup)
                {
                    fs.Close();

                    /* Free pinned items */
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();
                    Feed.Free();

                    /* gs clean up */
                    int code1 = GSAPI.gsapi_exit(gsInstance);
                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        code = code1;
                    }

                    GSAPI.gsapi_delete_instance(gsInstance);
                    Params.return_code = code;

                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        Params.result = GS_Result_t.gsOK;
                        Result        = Params;
                    }
                    else
                    {
                        Params.result = GS_Result_t.gsFAILED;
                        Result        = Params;
                    }
                    gsInstance = IntPtr.Zero;
                }
            }
            gsCompleted(Result);
            return;
        }
コード例 #12
0
ファイル: ghostmono.cs プロジェクト: cenx1/ghostpdl
        /* Process command line with gsapi_init_with_args */
        private void gsFileAsync(object data)
        {
            List <object>  genericlist    = data as List <object>;
            gsParamState_t Params         = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = Params;
            int            num_params     = Params.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            GCHandle       argPtrsStable  = new GCHandle();
            int            code           = 0;
            bool           cleanup        = true;

            try
            {
                code = GSAPI.gsapi_new_instance(out gsInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_new_instance error");
                }
                code = GSAPI.gsapi_set_stdio(gsInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_set_stdio error");
                }
                code = GSAPI.gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_set_arg_encoding error");
                }

                /* Now convert our Strings to char* and get pinned handles to these.
                 * This keeps the c# GC from moving stuff around on us */
                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((Params.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + Params.args[k];
                }

                /* Also stick the array of pointers into memory that will not be GCd */
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_init_with_args error");
                }
            }
            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            finally
            {
                if (cleanup)
                {
                    /* 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();

                    int code1 = GSAPI.gsapi_exit(gsInstance);
                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        code = code1;
                    }

                    GSAPI.gsapi_delete_instance(gsInstance);
                    Params.return_code = code;

                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        Params.result = GS_Result_t.gsOK;
                        Result        = Params;
                    }
                    else
                    {
                        Params.result = GS_Result_t.gsFAILED;
                        Result        = Params;
                    }
                    gsInstance = IntPtr.Zero;
                }
            }

            /* Completed. */
            gsCompleted(Result);
            return;
        }