Dear Team,

I have created one class in C# to convert ".doc, .docx, etc" documents to PDF 
tried using open office version 4.0.0.

Note : Software's (Open Office 4.0.0 / Open office SDK ) are not installed on 
my PC . I am only using required 5 dll's along with sopen.exe.

When I run the application it is giving error at

public static void ConvertToPdf(string inputFile, string outputFile)
        {
            if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == 
null)
                throw new InvalidProgramException("Unknown file type for 
OpenOffice. File = " + inputFile);
                StartOpenOffice();                          //Get a 
ComponentContext
                var xLocalContext = Bootstrap.bootstrap();

==> Throwing error detailed below

System.Runtime.InteropServices.SEHException (0x80004005): External component 
has thrown an exception.
   at 
cppu.defaultBootstrap_InitialComponentContext(Reference<com::sun::star::uno::XComponentContext>*
 )
   at uno.util.Bootstrap.defaultBootstrap_InitialComponentContext(String 
ini_file, IDictionaryEnumerator bootstrap_parameters)

I tried with many few solutions but not able to resolve this issue and found 
email addresses on the link 
https://issues.apache.org/ooo/show_bug.cgi?format=multiple&id=122007.

Please provide the solution to this problem.

Detailed code is as given below.


public static void ConvertToPdf(string inputFile, string outputFile)
        {
            if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == 
null)
                throw new InvalidProgramException("Unknown file type for 
OpenOffice. File = " + inputFile);
            StartOpenOffice();                          //Get a ComponentContext
            var xLocalContext = Bootstrap.bootstrap();  //Get 
MultiServiceFactory
            var xRemoteFactory = 
(XMultiServiceFactory)xLocalContext.getServiceManager();     //Get a 
CompontLoader
            var aLoader = (XComponentLoader) 
xRemoteFactory.createInstance("com.sun.star.frame.Desktop");     //Load the 
sourcefile
            XComponent xComponent = null;
            try
            {
                xComponent = InitDocument(aLoader,PathConverter(inputFile), 
"_blank");         //Wait for loading
                while (xComponent == null)
                {
                    Thread.Sleep(1000);
                }          // save/export the document

                SaveDocument(xComponent, inputFile, PathConverter(outputFile));
            }
            finally
            {
                if (xComponent != null) xComponent.dispose();
            }
        }



        private static void StartOpenOffice()
        {
            var ps = Process.GetProcessesByName("soffice.exe");
            if (ps.Length != 0)
                throw new InvalidProgramException("OpenOffice not found. Is 
OpenOffice installed?");
            if (ps.Length > 0)
                return;
            string sOfficePath = ConfigSettings.OpenOfficeExePath;
            var p = new Process {
                 StartInfo = { Arguments = "-headless -nofirststartwizard", 
FileName = sOfficePath +"soffice.exe", CreateNoWindow = true }
                             };
            var result = p.Start();
            if (result == false)
             throw new InvalidProgramException("OpenOffice failed to start.");
        }


        private static XComponent InitDocument(XComponentLoader aLoader, string 
file, string target)
        {
            var openProps = new PropertyValue[1];
            openProps[0] = new PropertyValue { Name = "Hidden", Value = new 
Any(true) };
            var xComponent = aLoader.loadComponentFromURL(file, target, 0, 
openProps);
            return xComponent;
        }

        private static void SaveDocument(XComponent xComponent, string 
sourceFile, string destinationFile)
        {     var propertyValues = new PropertyValue[2];     // Setting the 
flag for overwriting
            propertyValues[1] = new PropertyValue {Name = "Overwrite", Value = 
new Any(true)};
            //// Setting the filter name
            propertyValues[0] = new PropertyValue
                {
                    Name = "FilterName",Value = new 
Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile)))
                };
            ((XStorable) xComponent).storeToURL(destinationFile, 
propertyValues);
        }

        private static string PathConverter(string file)
        {
            if (string.IsNullOrEmpty(file))
            throw new NullReferenceException("Null or empty path passed to 
OpenOffice");
            return String.Format("file:///{0}", file.Replace(@"\", "/"));
        }

        public static string ConvertExtensionToFilterType(string extension)
        {
            switch (extension)
            {
                case ".doc":
                case ".docx":
                case ".txt":
                case ".rtf":
                case ".html":
                case ".htm":
                case ".xml":
                case ".odt":
                case ".wps":
                case ".wpd":
                    return "writer_pdf_Export";
                case ".xls":
                case ".xlsb":
                case ".xlsx":
                case ".ods":
                    return "calc_pdf_Export";
                case ".ppt":
                case ".pptx":
                case ".odp":
                return "impress_pdf_Export";
                default:
                    return null;
            }
        }







Best Regards,
Satish Shinde
Desk: +91 20 4014 2481
CUG:  5514 2481

Reply via email to