Page 1 of 1

Docx4NET not working in Class Libary Project

PostPosted: Fri Nov 25, 2016 6:19 pm
by nablatest@gmail.com
I have downloaded and executed the project described in http://www.docx4java.org/blog/2014/09/docx-to-pdf-in-c-net/
It works fine in a Console Application. I extracted the code into a method however when I run the code in a Class Library Project, I get the following error at the line:
Code: Select all
org.docx4j.Docx4J.toPDF(wordMLPackage, fos);

Message: "The type initializer for 'org.docx4j.fonts.IdentityPlusMapper' threw an exception."
Inner Exception: "No dir configured for temp fonts! Either set system property user.home to a writable dir, or configure docx4j property 'docx4j.openpackaging.parts.WordprocessingML.ObfuscatedFontPart.tmpFontDir'

Here is my custom method:
Code: Select all
public static string ConvertWordToPDF(string wordPath)
        {
            string projectDir = Directory.GetParent(
                Directory.GetParent(
                Environment.CurrentDirectory.ToString()).ToString()).ToString() + "\\";

            string fileIN = wordPath;
            string fileOUT = Path.ChangeExtension(wordPath, "pdf");

            // Programmatically configure Common Logging
            // (alternatively, you could do it declaratively in app.config)
            NameValueCollection commonLoggingproperties = new NameValueCollection();
            commonLoggingproperties["showDateTime"] = "false";
            commonLoggingproperties["level"] = "INFO";
            LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(commonLoggingproperties);


            ILog log = LogManager.GetCurrentClassLogger();
            log.Info("Hello from Common Logging");

            // Necessary, if slf4j-api and slf4j-NetCommonLogging are separate DLLs
            ikvm.runtime.Startup.addBootClassPathAssembly(
                System.Reflection.Assembly.GetAssembly(
                    typeof(org.slf4j.impl.StaticLoggerBinder)));

            // Configure to find docx4j.properties
            // .. add as URL the dir containing docx4j.properties (not the file itself!)
            Plutext.PropertiesConfigurator.setDocx4jPropertiesDir(projectDir + @"src\samples\resources\");

            // OK, do it..
            var s = new com.sun.org.apache.xerces.@internal.jaxp.SAXParserFactoryImpl();
            var t = new com.sun.org.apache.xalan.@internal.xsltc.trax.TransformerFactoryImpl();
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                    .load(new java.io.File(fileIN));

            java.io.FileOutputStream fos = new java.io.FileOutputStream(new java.io.File(fileOUT));

            org.docx4j.Docx4jProperties.setProperty("org.docx4j.openpackaging.parts.WordprocessingML.ObfuscatedFontPart.tmpFontDir", projectDir);
            org.docx4j.Docx4J.toPDF(wordMLPackage, fos);

            fos.close();

            return fileOUT;
        }