示例#1
0
        public void TestMethod1()
        {
            var startingProcesses = Process.GetProcessesByName("soffice");
            var startingCount     = startingProcesses.Length;

            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();
            OpenOfficeUtility.StartOpenOffice();

            var endingProcesses = Process.GetProcessesByName("soffice");
            var endingCount     = endingProcesses.Length;

            Console.WriteLine($"Starting count: {startingCount}");
            Console.WriteLine($"Ending count: {endingCount}");

            Assert.IsTrue(endingCount >= startingCount);
            Assert.IsTrue((endingCount == startingCount) || (endingCount == startingCount + 1));
        }
        /// <summary>
        /// Converts the file to PDF.
        /// </summary>
        /// <param name="inputFile"> The input file. </param>
        /// <param name="outputFile"> The output file. </param>
        /// <exception cref="System.InvalidProgramException">
        /// These aren't the file types you're looking for. Invalid file type for source file. or
        /// These aren't the file types you're looking for. Invalid file type for source file. Must
        /// be PDF.
        /// </exception>
        public static void ConvertFileToPdf(string inputFile, string outputFile)
        {
            PrintStart(inputFile, outputFile);

            if (!inputFile.IsValidExtension())
            {
                throw new InvalidProgramException($"These aren't the file types you're looking for. Invalid file type for source file.");
            }

            if (!outputFile.IsPdf())
            {
                throw new InvalidProgramException($"These aren't the file types you're looking for. Invalid file type for source file. Must be PDF.");
            }

            var xComponent = OpenOfficeUtility.GetComponent();

            try
            {
                LogUtility.Log("Starting Open Office...");
                OpenOfficeUtility.StartOpenOffice();

                LogUtility.Log("...Getting a ComponentContext");
                var xLocalContext = OpenOfficeUtility.GetComponentContext();

                LogUtility.Log("...Getting a MultiServiceFactory");
                var xRemoteFactory = OpenOfficeUtility.GetMultiServiceFactory(xLocalContext);

                LogUtility.Log("...Getting a ComponentLoader");
                var aLoader = OpenOfficeUtility.GetComponentLoader(xRemoteFactory);

                LogUtility.Log("...Loading source file");
                xComponent = OpenOfficeUtility.InitDocument(aLoader, inputFile.ConvertPath(), "_blank");

                LogUtility.Log("...wait for loading");
                while (xComponent == null)
                {
                    Thread.Sleep(1000);
                }

                LogUtility.Log("...saving/exporting the document");
                OpenOfficeUtility.SaveDocument(xComponent, inputFile, outputFile.ConvertPath());
            }
            finally
            {
                LogUtility.Log("...disposing ComponentLoader");
                PrintEnd();
                if (xComponent != null)
                {
                    xComponent.dispose();
                }
            }
        }