Пример #1
0
        /// <summary>
        /// Save as ODF.
        /// </summary>
        public override bool ExportOdf()
        {
            // check if Word12 converter is installed
            if (_word12SaveFormat == -1)
            {
                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfConverterNotInstalled"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }

            LateBindingObject doc = _application.Invoke("ActiveDocument");

            // the second test deals with blank documents
            // (which are in a 'saved' state and have no extension yet(?))
            if (!doc.GetBool("Saved") ||
                doc.GetString("FullName").IndexOf('.') < 0 ||
                doc.GetString("FullName").IndexOf("http://") == 0 ||
                doc.GetString("FullName").IndexOf("https://") == 0 ||
                doc.GetString("FullName").IndexOf("ftp://") == 0
                )
            {
                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfSaveDocumentBeforeExport"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
            else
            {
                System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();

                sfd.AddExtension = true;
                sfd.DefaultExt   = "odt";
                sfd.Filter       = this._addinLib.GetString(this.OdfFileType) + this.ExportOdfFileFilter
                                   + this._addinLib.GetString(ODF_FILE_TYPE_OTT) + EXPORT_ODF_FILE_FILTER_OTT
                                   + this._addinLib.GetString(ALL_FILE_TYPE) + this.ExportAllFileFilter;
                sfd.InitialDirectory             = doc.GetString("Path");
                sfd.OverwritePrompt              = true;
                sfd.SupportMultiDottedExtensions = true;
                sfd.Title = this._addinLib.GetString(EXPORT_LABEL);
                string ext = '.' + sfd.DefaultExt;
                sfd.FileName = doc.GetString("FullName").Substring(0, doc.GetString("FullName").LastIndexOf('.')); // +ext;

                // process the chosen documents
                if (System.Windows.Forms.DialogResult.OK == sfd.ShowDialog())
                {
                    // name of the file to create
                    string odfFileName = sfd.FileName;
                    // multi dotted extensions support
                    // Note: sfd.FilterIndex is 1-based
                    if (!odfFileName.ToLower().EndsWith(".odt") && sfd.FilterIndex == 1)
                    {
                        odfFileName += ".odt";
                    }
                    else if (!odfFileName.ToLower().EndsWith(".ott") && sfd.FilterIndex == 2)
                    {
                        odfFileName += ".ott";
                    }
                    // name of the document to convert
                    string sourceFileName = doc.GetString("FullName");
                    // name of the temporary Word12 file created if current file is not already a Word12 document
                    string tempDocxName = null;
                    // store current cursor
                    WdCursorType currentCursor = (WdCursorType)_application.Invoke("System").GetInt32("Cursor");
                    // display hourglass
                    this._application.Invoke("System").SetInt32("Cursor", (int)WdCursorType.wdCursorWait);


                    if (!(doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLDocument ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLDocumentMacroEnabled ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLTemplate ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLTemplateMacroEnabled))
                    {
                        try
                        {
                            // if file is not currently in Word12 format
                            // 1. Create a copy
                            // 2. Open it and do a "Save as Word12" (copy needed not to perturb current openened document
                            // 3. Convert the Word12 copy to ODF
                            // 4. Remove both temporary created files

                            // duplicate the file to keep current file "as is"
                            string tempCopyName = Path.GetTempFileName() + Path.GetExtension((string)sourceFileName);
                            File.Copy((string)sourceFileName, (string)tempCopyName);

                            //BUG FIX #1743469
                            FileInfo fi = new FileInfo(tempCopyName);
                            if (fi.IsReadOnly)
                            {
                                fi.IsReadOnly = false;
                            }
                            //BUG FIX #1743469

                            // open the duplicated file
                            bool confirmConversions = false;
                            bool readOnly           = false;
                            bool addToRecentFiles   = false;
                            bool isVisible          = false;

                            LateBindingObject newDoc = OpenDocument(tempCopyName, confirmConversions, readOnly, addToRecentFiles, isVisible, false);

                            newDoc.Invoke("Windows").Invoke("Item", 1).SetBool("Visible", false);

                            // generate docx file from the duplicated file (under a temporary file)
                            string outputExtension =
                                tempDocxName = this._addinLib.GetTempPath((string)sourceFileName, ".docx");

                            SaveDocumentAs(newDoc, tempDocxName);

                            // close and remove the duplicated file
                            newDoc.Invoke("Close", WdSaveOptions.wdDoNotSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, Type.Missing);

                            //BUG FIX #1743469
                            try
                            {
                                File.Delete((string)tempCopyName);
                            }
                            catch (Exception ex)
                            {
                                //If delete does not work, don't stop the rest of the process
                                //The tempFile will be deleted by the system
                                System.Diagnostics.Trace.WriteLine(ex.ToString());
                            }
                            //BUG FIX #1743469

                            // Now the file to be converted is
                            sourceFileName = tempDocxName;
                        }
                        catch (Exception ex)
                        {
                            _application.Invoke("System").SetInt32("Cursor", (int)currentCursor);

                            String lMsg;

                            lMsg = _addinLib.GetString("OdfExportErrorTryDocxFirst");
                            System.Windows.Forms.MessageBox.Show(lMsg, DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                            System.Diagnostics.Trace.WriteLine(ex.ToString());
                            return(false);
                        }
                    }

                    ConversionOptions options = new ConversionOptions();
                    options.InputFullName         = sourceFileName;
                    options.OutputFullName        = odfFileName;
                    options.InputFullNameOriginal = doc.GetString("FullName");
                    options.ConversionDirection   = ConversionDirection.DocxToOdt;
                    options.Generator             = this.GetGenerator();
                    options.DocumentType          = Path.GetExtension(odfFileName).ToUpper().Equals(".OTT") ? DocumentType.Template : DocumentType.Document;
                    options.ShowProgress          = true;
                    options.ShowUserInterface     = true;

                    this._addinLib.OoxToOdf(sourceFileName, odfFileName, options);

                    if (tempDocxName != null && File.Exists((string)tempDocxName))
                    {
                        this._addinLib.DeleteTempPath((string)tempDocxName);
                    }
                    _application.Invoke("System").SetInt32("Cursor", (int)currentCursor);
                }
                return(true);
            }
        }
Пример #2
0
        private void SaveTemplate(TemplateType type)
        {
            WaitingForm  frmWaiting     = null;
            WdCursorType originalCursor = WdCursorType.wdCursorNormal;

            try
            {
                originalCursor = Wkl.MainCtrl.CommonCtrl.CommonProfile.App.System.Cursor;
                TemplateInfo templateInfo = Wkl.MainCtrl.CommonCtrl.CommonProfile.CurrentTemplateInfo;

                if (templateInfo.IsProntoDoc)
                {
                    string filter = string.Empty;
                    switch (type)
                    {
                    case TemplateType.Pdw:
                        filter = PdwFilter;
                        break;

                    case TemplateType.Pdh:
                        filter = PdhFilter;
                        break;

                    case TemplateType.Pdz:
                        filter = PdzFilter;
                        break;

                    //HACK:FORM CONTROLS - TEMPORARY INSTEAD OF TemplateType.Pdm
                    case TemplateType.Pdm:
                        filter = PdmFilter;
                        break;

                    default:
                        break;
                    }

                    SaveFileDialog saveDialog = new SaveFileDialog {
                        Filter = filter
                    };

                    if (saveDialog.ShowDialog() == DialogResult.OK)
                    {
                        // set wating cursor
                        Wkl.MainCtrl.CommonCtrl.CommonProfile.App.System.Cursor = WdCursorType.wdCursorWait;
                        frmWaiting = new WaitingForm();
                        frmWaiting.Show();

                        Context.ContextManager contextMgr = new Context.ContextManager();
                        contextMgr.SaveAsTemplate(Wkl.MainCtrl.CommonCtrl.CommonProfile.ActiveDoc, saveDialog.FileName);

                        if (File.Exists(Wkl.MainCtrl.CommonCtrl.CommonProfile.ActiveDoc.FullName))
                        {
                            MessageBox.Show(MessageUtils.Expand(Properties.Resources.ipm_M006,
                                                                Wkl.MainCtrl.CommonCtrl.CommonProfile.ActiveDoc.FullName));
                        }
                    }
                }
                else
                {
                    MessageBox.Show(MessageUtils.Expand(Properties.Resources.ipm_NotIsProntoDoc,
                                                        Properties.Resources.ipe_NotIsProntoDoc));
                }
            }
            catch (BaseException baseExp)
            {
                ManagerException mgrExp = new ManagerException(ErrorCode.ipe_SavePdwError);
                mgrExp.Errors.Add(baseExp);

                LogUtils.LogManagerError(mgrExp);
            }
            finally
            {
                try
                {
                    if (frmWaiting != null)
                    {
                        frmWaiting.Close();
                        frmWaiting.Dispose();
                    }

                    Wkl.MainCtrl.CommonCtrl.CommonProfile.App.System.Cursor = originalCursor;
                }
                catch { }
            }
        }