Exemplo n.º 1
0
        public bool OpenFile(string filePath, bool addToJumpList = true)
        {
            if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
            {
                OpenFileDialog ofd = new OpenFileDialog();
                if (!string.IsNullOrEmpty(filePath))
                {
                    if (!Directory.Exists(filePath))
                    {
                        filePath = Path.GetDirectoryName(filePath);
                    }
                    if (Directory.Exists(filePath))
                    {
                        ofd.InitialDirectory = filePath;
                    }
                }
                ofd.Filter = "All C1 Documents (*.c1dx;*.c1d;*.c1mdx)|*.c1dx;*.c1d;*.c1mdx|C1 Open XML Documents (*.c1dx)|*.c1dx|C1 Documents (*.c1d)|*.c1d|C1 Open XML Multi Documents (*.c1mdx)|*.c1mdx|All Files (*.*)|*.*";
                bool res = ofd.ShowDialog() == DialogResult.OK;
                filePath = ofd.FileName;
                ofd.Dispose();
                if (!res)
                {
                    return(false);
                }
            }

            object doc = null;
            string ext = Path.GetExtension(filePath).ToLowerInvariant();

            try
            {
                if (ext == ".c1dx")
                {
                    var dx = new C1PrintDocument();
                    dx.Load(filePath, C1DocumentFormatEnum.C1dx);
                    doc = dx;
                }
                else if (ext == ".c1d")
                {
                    var dx = new C1PrintDocument();
                    dx.Load(filePath, C1DocumentFormatEnum.C1d);
                    doc = dx;
                }
                else if (ext == ".c1mdx")
                {
                    var mdx = new C1MultiDocument();
                    mdx.Load(filePath);
                    doc = mdx;
                }
                else
                {
                    MessageBox.Show("Can't open file with \"" + ext + "\" extension.", "C1PrintDocument Viewer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (doc != null)
            {
                Text         = Path.GetFileNameWithoutExtension(filePath);
                rpc.Document = doc;
                if (addToJumpList && CheckFileRegistration())
                {
                    atb.JumpList.AddToRecentCategory(filePath);
                }
                return(true);
            }
            return(false);
        }