Esempio n. 1
0
        /// <summary>
        /// Convert word file to pdf
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        /// <returns>Error message.</returns>
        internal String ConvertToPdf(string sourcePath, string targetPath)
        {
            Application app = null;
            Workbook    wkb = null;

            OfficeUtils.KillOfficeProcess("EXCEL");
            try
            {
                app = new Application();
                wkb = app.Workbooks.Open(sourcePath);
                wkb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, targetPath);
                wkb.Close(false);
                wkb = null;
                app.Quit();
                app = null;
                return(String.Empty);
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat(ex, "Error converting {0} - {1}", sourcePath, ex.Message);

                if (wkb != null)
                {
                    this.Close(wkb);
                }
                if (app != null)
                {
                    this.Close(app);
                }
                return($"Error converting {sourcePath} - {ex.Message}");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Convert word file to pdf
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        /// <returns>Error message.</returns>
        internal String ConvertToPdf(string sourcePath, string targetPath)
        {
            Application app = null;
            Document    doc = null;

            OfficeUtils.KillOfficeProcess("WINWORD");
            try
            {
                app = new Application();
                doc = app.Documents.Open(sourcePath);
                doc.SaveAs2(targetPath, WdSaveFormat.wdFormatPDF);
                _logger.InfoFormat("File {0} converted to pdf.", sourcePath);
                doc.Close();
                doc = null;
                app.Quit();
                app = null;
                return(String.Empty);
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat(ex, "Error converting {0} - {1}", sourcePath, ex.Message);

                if (doc != null)
                {
                    this.Close(doc);
                }
                if (app != null)
                {
                    this.Close(app);
                }
                return($"Error converting {sourcePath} - {ex.Message}");
            }
        }
Esempio n. 3
0
        public MicrosoftOfficePdfOutOfProcessJob(
            WordConverter wordConverter,
            PowerPointConverter powerPointConverter,
            ExcelConverter excelConverter,
            ILogger logger)
        {
            base.PipelineId = "office";
            base.QueueName = "office";
            _wordConverter = wordConverter;
            _powerPointConverter = powerPointConverter;
            _excelConverter = excelConverter;
            _logger = logger;
            OfficeUtils.Logger = logger;

            //var config = ConfigurationManager.AppSettings["threadNumber"];
            //_logger.InfoFormat("Configuration ThreadNumber is {0}", config);
            //if (String.IsNullOrEmpty(config) || !Int32.TryParse(config, out _threadNumber))
            //{
            //    _logger.Info("Configuration ThreadNumber wrong the job will default to a single thread");
            //    _threadNumber = 1;
            //}

            //It is not safe to have more than one thread running office automation.
            _threadNumber = 1; 

            OfficeUtils.KillStaleOfficeProgram();
            _cleanupTimer = new Timer();
            _cleanupTimer.Elapsed += (s, e) => OfficeUtils.KillStaleOfficeProgram();
            _cleanupTimer.Interval = 1000 * 60 * 10;
            _cleanupTimer.Start();
        }
        /// <summary>
        /// Convert power point to pdf
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        /// <returns>Error message, string empty if succeeded</returns>
        internal String ConvertToPdf(string sourcePath, string targetPath)
        {
            Application  app          = null;
            Presentation presentation = null;

            OfficeUtils.KillOfficeProcess("POWERPNT");
            try
            {
                app = new Application();
                //app.Visible = MsoTriState.msoFalse;
                //app.WindowState = PpWindowState.ppWindowMinimized;
                presentation = app.Presentations.Open(sourcePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

                presentation.ExportAsFixedFormat(
                    targetPath,
                    PpFixedFormatType.ppFixedFormatTypePDF,
                    PpFixedFormatIntent.ppFixedFormatIntentScreen);

                presentation.Close();
                presentation = null;
                app.Quit();
                app = null;

                return(String.Empty);
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat(ex, "Error converting {0} - {1}", sourcePath, ex.Message);

                if (presentation != null)
                {
                    this.Close(presentation);
                }
                if (app != null)
                {
                    this.Close(app);
                }
                return($"Error converting {sourcePath} - {ex.Message}");
            }
        }