Click here to Skip to main content
12,505,655 members (51,926 online)
Click here to Skip to main content

Stats

361.6K views
9.2K downloads
258 bookmarked
Posted

Generate PDF Using C#

, 1 Feb 2009 CPOL
Using OpenOffice to convert different document types to PDF.
using System;
using System.Diagnostics;

namespace OpenOfficeWrapper
{
    /// <summary>
    /// Controls life-cycle of OpenOffice process
    /// </summary>
    public class OfficeController : IDisposable
    {
        private static DateTime _lastUsedTime = DateTime.MaxValue;

        private static System.Timers.Timer _checkTimer = null;

        /// <summary>
        /// Start OpenOffice process if needed
        /// </summary>
        public static void StartOpenOffice()
        {
            _lastUsedTime = DateTime.Now;
            initCheckTimer();

            Process[] ps = Process.GetProcessesByName(Properties.Settings.Default.ProcessName);
            if (ps == null || ps.Length == 0)
            {
                Process p = Process.Start(Properties.Settings.Default.PathToOpenOffice, "-headless -nologo -norestore");
                //spent some time to start
                System.Threading.Thread.Sleep(3000);
            }
        }

        /// <summary>
        /// Initializes timer that checks for usage of OpenOffice process
        /// </summary>
        private static void initCheckTimer()
        {
            if (_checkTimer == null)
            {
                _checkTimer = new System.Timers.Timer(Properties.Settings.Default.CheckIntervalInSeconds * 1000);
                _checkTimer.Elapsed += new System.Timers.ElapsedEventHandler(_checkTimer_Elapsed);
                _checkTimer.Start();
            }
        }

        /// <summary>
        /// Performs checking of OpenOffice usage, kills it if not used
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void _checkTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Process[] ps = Process.GetProcessesByName(Properties.Settings.Default.ProcessName);
            double secondsIdle = DateTime.Now.Subtract(_lastUsedTime).TotalSeconds;
            if (ps != null && ps.Length > 0 && secondsIdle > Properties.Settings.Default.SecondsIdleAllowed)
            {
                ps[0].Kill();
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            // stop timer
            _checkTimer.Stop();

            // stop soffice process
            Process[] ps = Process.GetProcessesByName(Properties.Settings.Default.ProcessName);
            if (ps != null && ps.Length > 0)
                ps[0].Kill();
            
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

lepipele
Chief Technology Officer
United States United States
If you liked this article, consider reading other articles by me. For republishing article on other websites, please contact me by leaving a comment.

Bitcoin wallet:
1ARXmh58S6aRiZPCbneDCzrB1HUU6PLmDQ

Blogs I enjoy:
http://blog.codinghorror.com/

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 2 Feb 2009
Article Copyright 2008 by lepipele
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid