Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Generate PDF Using C#

, 1 Feb 2009 CPOL
Using OpenOffice to convert different document types to PDF.
article_src.zip
Solution
!Ref
cli_basetypes.dll
cli_cppuhelper.dll
cli_types.dll
cli_ure.dll
OpenOfficeService
ClassDiagram1.cd
OpenOfficeService.csproj.user
Properties
Settings.settings
OpenOfficeService.Objects
Properties
OpenOfficeWrapper
Properties
Settings.settings
PDFWeb
App_Data
ExcelSheet.xls
PDFWeb.csproj.user
Properties
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

Predrag Tomasevic
Chief Technology Officer Atama Group
United States United States

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 2 Feb 2009
Article Copyright 2008 by Predrag Tomasevic
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid