Beispiel #1
0
        // Opens the specified stream using the preview handler COM type with the provided GUID and displays the result in this PreviewHandlerHost.
        public bool Open(Stream stream, Guid previewHandler)
        {
            UnloadPreviewHandler();

            if (stream == null)
            {
                ErrorMessage = "No file loaded.";
                return(false);
            }

            ErrorMessage = "";

            if (previewHandler != Guid.Empty)
            {
                try
                {
                    if (previewHandler != m_currentPreviewHandlerGUID)
                    {
                        m_currentPreviewHandlerGUID = previewHandler;

                        // need to instantiate a different COM type (file format has changed)
                        if (m_currentPreviewHandler != null)
                        {
                            Marshal.FinalReleaseComObject(m_currentPreviewHandler);
                        }

                        // use reflection to instantiate the preview handler type
                        Type _comType = Type.GetTypeFromCLSID(m_currentPreviewHandlerGUID);
                        m_currentPreviewHandler = Activator.CreateInstance(_comType);
                    }

                    if (m_currentPreviewHandler is IInitializeWithStream)
                    {
                        // must wrap the stream to provide compatibility with IStream
                        m_currentPreviewHandlerStream = stream;
                        StreamWrapper _wrapped = new StreamWrapper(m_currentPreviewHandlerStream);
                        ((IInitializeWithStream)m_currentPreviewHandler).Initialize(_wrapped, 0);
                    }

                    if (m_currentPreviewHandler is IPreviewHandler)
                    {
                        // bind the preview handler to the control's bounds and preview the content
                        Rectangle _r = ClientRectangle;
                        ((IPreviewHandler)m_currentPreviewHandler).SetWindow(Handle, ref _r);
                        ((IPreviewHandler)m_currentPreviewHandler).DoPreview();

                        return(true);
                    }
                }
                catch (Exception _ex)
                {
                    ErrorMessage = "Preview could not be generated.\n"; // +_ex.Message;
                }
            }
            else
            {
                ErrorMessage = "No preview available.";
            }

            return(false);
        }
Beispiel #2
0
        // Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost.
        public bool Open(string filename)
        {
            UnloadPreviewHandler();

            if (String.IsNullOrEmpty(filename))
            {
                ErrorMessage = "No file loaded.";
                return(false);
            }

            // 目前64bit PDF Reader 有問題
            if (isWin64AndPDF(filename))
            {
                ErrorMessage = "No file loaded.";
                return(false);
            }

            // try to get GUID for the preview handler
            Guid _guid = GetPreviewHandlerGUID(filename);

            ErrorMessage = "";

            if (_guid != Guid.Empty)
            {
                try
                {
                    if (_guid != m_currentPreviewHandlerGUID)
                    {
                        m_currentPreviewHandlerGUID = _guid;

                        // need to instantiate a different COM type (file format has changed)
                        if (m_currentPreviewHandler != null)
                        {
                            Marshal.FinalReleaseComObject(m_currentPreviewHandler);
                        }

                        // use reflection to instantiate the preview handler type
                        Type _comType = Type.GetTypeFromCLSID(m_currentPreviewHandlerGUID);
                        m_currentPreviewHandler = Activator.CreateInstance(_comType);
                    }

                    if (m_currentPreviewHandler is IInitializeWithFile)
                    {
                        // some handlers accept a filename
                        ((IInitializeWithFile)m_currentPreviewHandler).Initialize(filename, 0);
                    }
                    else if (m_currentPreviewHandler is IInitializeWithStream)
                    {
                        if (File.Exists(filename))
                        {
                            // other handlers want an IStream (in this case, a file stream)
                            m_currentPreviewHandlerStream = File.Open(filename, FileMode.Open);
                            StreamWrapper stream = new StreamWrapper(m_currentPreviewHandlerStream);
                            ((IInitializeWithStream)m_currentPreviewHandler).Initialize(stream, 0);
                        }
                        else
                        {
                            ErrorMessage = "File not found.";
                        }
                    }

                    if (m_currentPreviewHandler is IPreviewHandler)
                    {
                        // bind the preview handler to the control's bounds and preview the content
                        Rectangle _r = ClientRectangle;
                        ((IPreviewHandler)m_currentPreviewHandler).SetWindow(Handle, ref _r);
                        ((IPreviewHandler)m_currentPreviewHandler).DoPreview();

                        return(true);
                    }
                }
                catch (Exception _ex)
                {
                    ErrorMessage = "Preview could not be generated.\n"; // +_ex.Message;
                }
            }
            else
            {
                ErrorMessage = "No preview available.";
            }

            return(false);
        }