示例#1
0
        /// <summary>
        /// Using the Tedds application, create a document with the given filename.
        /// Then perform the provided action with the tedds document.
        /// </summary>
        /// <param name="fileName">File name</param>
        /// <param name="saver">Action to perform with the created Tedds document</param>
        private void SaveWithTedds(string fileName, Action <ITeddsDocument> saver)
        {
            IApplication    application = null;
            ITeddsDocuments documents   = null;
            ITeddsDocument  document    = null;

            try
            {
                //Launch Tedds application, or get hold of an already running instance
                application = new Application();
#if DEBUG
                //If Tedds was not already running, make it visible for debugging purposes
                application.Visible = true;
#endif

                documents = application.Documents;
                //Create a new Tedds document
                document = documents.Add3(Path.GetFileNameWithoutExtension(fileName), CalcFileName, CalcItemName, OutputVariablesXml, OutputRtf);
                //Perform the given save action on the document
                saver(document);

                if (!application.Visible)
                {
                    document.Close(false);
                    //Once the document has been closed this reference is no longer valid and does not need to be freed later
                    document = null;
                }

                StatusText = "Tedds document created successfully";
            }
            catch (COMException ex)
            {
                StatusText = $"Exception occurred: {ex.Message}";
            }
            finally
            {
                //Release objects to close down Tedds application
                if (document != null)
                {
                    Marshal.ReleaseComObject(document);
                }
                if (documents != null)
                {
                    Marshal.ReleaseComObject(documents);
                }
                if (application != null)
                {
                    Marshal.ReleaseComObject(application);
                }
            }
        }
        /// <summary>
        /// Convert Tedds document file to PDF. Output file is created in the same location as the input file.
        /// </summary>
        /// <param name="teddsApp">Tedds application object.</param>
        /// <param name="filePath">Full path of Tedds document file or a directory of files to convert.</param>
        /// <param name="options">Conversion options.</param>
        /// <param name="cancel">Returns true if user cancels operation.</param>
        public static void FileConvertTedToPdf(IApplication teddsApp, string fileName, ref ConvertOptions options, ref bool cancel)
        {
            //Create output file in the same location as the input file but with the PDF file extension
            string outputFileName = Path.ChangeExtension(fileName, FileExtension_Pdf);

            //Verify whether output file already exists
            if (File.Exists(outputFileName))
            {
                if (options.overwrite == false)
                {
                    return;
                }

                if (options.overwrite != true && !PromptToOverwrite(outputFileName, ref options, ref cancel))
                {
                    return;
                }
            }

            bool            closeDocument = false;
            ITeddsDocument  document      = null;
            ITeddsDocuments documents     = null;

            try
            {
                documents = teddsApp.Documents;

                //Determine if file is already open
                try { document = documents[fileName]; }
                catch (COMException) { }

                if (document == null)
                {
                    document      = documents.Open(fileName);
                    closeDocument = true;
                }
                if (document != null)
                {
                    document.SaveAsPdf(outputFileName);
                    if (closeDocument)
                    {
                        document.Close();
                        //Closing the document explicitly destroys the object
                        //Do not try to release it at the end of this method
                        document = null;
                    }
                }
                Console.WriteLine($"Saved '{fileName}'\n   as '{outputFileName}'");
            }
            catch (COMException e)
            {
                Console.WriteLine($"Error converting document '{fileName}'\n{e}");
            }
            finally
            {
                //Explicitely release references to COM objects
                if (document != null)
                {
                    Marshal.ReleaseComObject(document);
                }
                if (documents != null)
                {
                    Marshal.ReleaseComObject(documents);
                }
            }
        }