예제 #1
0
        static void Main(string[] args)
        {
            // set up logging
            clog = LoggingConfigurator.configureLogging();
            clog.Info("Hello from Common Logging");

            // create a dir to save the output docx
            string projectDir = System.IO.Directory.GetParent(
                System.IO.Directory.GetParent(
                    Environment.CurrentDirectory.ToString()).ToString()).ToString() + "\\";

            System.IO.Directory.CreateDirectory(projectDir + "OUT");

            string fileIN  = projectDir + @"src\samples\resources\sample-docx.docx";
            string fileOUT = projectDir + @"OUT\Anon.docx";

            WordprocessingMLPackage pkg = Docx4J.load(new java.io.File(fileIN));
            // or
            // WordprocessingMLPackage pkg = Plutext.Docx4NET.WordprocessingMLPackageFactory.createWordprocessingMLPackage(fileIN);


            // Anonymize/densensitize it
            Anonymize       anon   = new Anonymize(pkg);
            AnonymizeResult result = anon.go();


            // Report
            clog.Info("\n\n REPORT for " + fileIN + "\n\n");
            if (result.isOK())
            {
                clog.Info("document successfully anonymised.");
            }
            else
            {
                clog.Info("document partially anonymised; please check " + fileOUT);

                if (result.getUnsafeParts().size() > 0)
                {
                    clog.Info("The following parts may leak info:");
                    foreach (Part p in result.getUnsafeParts())
                    {
                        clog.Info(p.getPartName().getName() + ", of type " + p.getClass().getName());
                    }
                }

                // unsafe objects
                clog.Info(result.reportUnsafeObjects());
            }

            clog.Info("\n\n .. end REPORT for " + fileIN + "\n\n");


            // save result to file
            Docx4J.save(pkg, new java.io.File(fileOUT));
            clog.Info("\n\n saved " + fileOUT + "\n\n");
        }
예제 #2
0
        static void Main(string[] args)
        {
            // set up logging
            clog = LoggingConfigurator.configureLogging();
            clog.Info("Hello from Common Logging");

            // create a dir to save the output docx
            string projectDir = System.IO.Directory.GetParent(
                System.IO.Directory.GetParent(
                    Environment.CurrentDirectory.ToString()).ToString()).ToString() + "\\";

            System.IO.Directory.CreateDirectory(projectDir + "OUT");

            // docx4j.properties .. add as URL the dir containing docx4j.properties
            Plutext.PropertiesConfigurator.setDocx4jPropertiesDir(projectDir + @"src\samples\resources\");


            // create WordprocessingMLPackage, representing the docx
            // and add some content to it
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
            MainDocumentPart        documentPart  = wordMLPackage.getMainDocumentPart();

            populateWithContent(documentPart);

            // Now add a ToC
            TocGenerator tocGenerator = new TocGenerator(wordMLPackage);

            // you should install your own local instance, and point to that in docx4j.properties

            tocGenerator.generateToc(0, " TOC \\o \"1-3\" \\h \\z \\u ", false);

            // Save the docx
            string fileOUT = projectDir + @"OUT\TocSample_Generated.docx";

            Docx4J.save(wordMLPackage, new java.io.File(fileOUT), Docx4J.FLAG_SAVE_ZIP_FILE);


            if (update)
            {
                documentPart.addStyledParagraphOfText("Heading2", "Hello 12");
                fillPageWithContent(documentPart, "Hello 12");
                documentPart.addStyledParagraphOfText("Heading1", "Hello 21");
                fillPageWithContent(documentPart, "Hello 21");
                documentPart.addStyledParagraphOfText("Heading2", "Hello 22");
                fillPageWithContent(documentPart, "Hello 22");
                documentPart.addStyledParagraphOfText("Heading3", "Hello 23");
                fillPageWithContent(documentPart, "Hello 23");

                tocGenerator.updateToc(false);

                fileOUT = projectDir + @"OUT\TocSample_Updated.docx";
                Docx4J.save(wordMLPackage, new java.io.File(fileOUT), Docx4J.FLAG_SAVE_ZIP_FILE);
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            // set up logging
            clog = LoggingConfigurator.configureLogging();
            clog.Info("Hello from Common Logging");

            // create a dir to save the output docx
            string projectDir = System.IO.Directory.GetParent(
                System.IO.Directory.GetParent(
                    Environment.CurrentDirectory.ToString()).ToString()).ToString() + "\\";
            string fileOUT = projectDir + @"OUT\HelloWorld.docx";

            System.IO.Directory.CreateDirectory(projectDir + "OUT");


            // docx4j.properties .. add as URL the dir containing docx4j.properties
            Plutext.PropertiesConfigurator.setDocx4jPropertiesDir(projectDir + @"src\samples\resources\");


            // create WordprocessingMLPackage, representing the docx
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
            MainDocumentPart        mdp           = wordMLPackage.getMainDocumentPart();


            // add content
            mdp.addParagraphOfText("hello world");  // a convenience method

            // the more generic pattern is:

            org.docx4j.wml.ObjectFactory wmlObjectFactory = org.docx4j.jaxb.Context.getWmlObjectFactory();
            P p = wmlObjectFactory.createP();     // but you can just do = new P();

            mdp.getContent().add(p);
            // Create object for r
            R r = wmlObjectFactory.createR();

            p.getContent().add(r);

            // Create object for t (wrapped in JAXBElement)
            Text        text        = wmlObjectFactory.createText();
            JAXBElement textWrapped = wmlObjectFactory.createRT(text);     // instead of JAXBElement<org.docx4j.wml.Text>

            // here Text text = new Text() would actually have been fine.

            text.setValue("hello world 2");
            r.getContent().add(textWrapped);


            // save to file
            Docx4J.save(wordMLPackage, new java.io.File(fileOUT), Docx4J.FLAG_SAVE_ZIP_FILE);
        }
예제 #4
0
        static void Main(string[] args)
        {
            // set up logging
            clog = LoggingConfigurator.configureLogging();
            clog.Info("Hello from Common Logging");

            string projectDir = System.IO.Directory.GetParent(
                System.IO.Directory.GetParent(
                    Environment.CurrentDirectory.ToString()).ToString()).ToString() + "\\";

            // the docx 'template'
            String input_DOCX = projectDir + @"src\samples\resources\ContentControlBind\binding-simple.docx";

            // the instance data
            String input_XML = projectDir + @"src\samples\resources\ContentControlBind\binding-simple-data.xml";

            // resulting docx
            String OUTPUT_DOCX = projectDir + @"OUT_ContentControlsMergeXML.docx";

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

            // Load input_template.docx
            WordprocessingMLPackage wordMLPackage = org.docx4j.Docx4J.load(new java.io.File(input_DOCX));

            // Open the xml stream
            java.io.FileInputStream xmlStream = new java.io.FileInputStream(new java.io.File(input_XML));

            // Do the binding:
            // FLAG_NONE means that all the steps of the binding will be done,
            // otherwise you could pass a combination of the following flags:
            // FLAG_BIND_INSERT_XML: inject the passed XML into the document
            // FLAG_BIND_BIND_XML: bind the document and the xml (including any OpenDope handling)
            // FLAG_BIND_REMOVE_SDT: remove the content controls from the document (only the content remains)
            // FLAG_BIND_REMOVE_XML: remove the custom xml parts from the document

            //Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_NONE);
            //If a document doesn't include the Opendope definitions, eg. the XPathPart,
            //then the only thing you can do is insert the xml
            //the example document binding-simple.docx doesn't have an XPathPart....
            Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML & Docx4J.FLAG_BIND_BIND_XML);

            //Save the document
            Docx4J.save(wordMLPackage, new java.io.File(OUTPUT_DOCX), Docx4J.FLAG_NONE);
            clog.Info("Saved: " + OUTPUT_DOCX);
        }
예제 #5
0
        static void Main(string[] args)
        {
            string projectDir = System.IO.Directory.GetParent(
                System.IO.Directory.GetParent(
                    Environment.CurrentDirectory.ToString()).ToString()).ToString() + "\\";

            // the docx 'template'
            String input_DOCX = projectDir + @"src\samples\resources\ContentControlBind\binding-simple.docx";

            // the instance data
            String input_XML = projectDir + @"src\samples\resources\ContentControlBind\binding-simple-data.xml";

            // resulting docx
            String OUTPUT_DOCX = projectDir + @"OUT_ContentControlsMergeXML.docx";


            // 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\");

            // Load input_template.docx
            WordprocessingMLPackage wordMLPackage = org.docx4j.Docx4J.load(new java.io.File(input_DOCX));

            // Open the xml stream
            java.io.FileInputStream xmlStream = new java.io.FileInputStream(new java.io.File(input_XML));

            // Do the binding:
            // FLAG_NONE means that all the steps of the binding will be done,
            // otherwise you could pass a combination of the following flags:
            // FLAG_BIND_INSERT_XML: inject the passed XML into the document
            // FLAG_BIND_BIND_XML: bind the document and the xml (including any OpenDope handling)
            // FLAG_BIND_REMOVE_SDT: remove the content controls from the document (only the content remains)
            // FLAG_BIND_REMOVE_XML: remove the custom xml parts from the document

            //Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_NONE);
            //If a document doesn't include the Opendope definitions, eg. the XPathPart,
            //then the only thing you can do is insert the xml
            //the example document binding-simple.docx doesn't have an XPathPart....
            Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML & Docx4J.FLAG_BIND_BIND_XML);

            //Save the document
            Docx4J.save(wordMLPackage, new java.io.File(OUTPUT_DOCX), Docx4J.FLAG_NONE);
            log.Info("Saved: " + OUTPUT_DOCX);
        }
예제 #6
0
        static void Main(string[] args)
        {
            ILog log = configureLogging();

            log.Info("Hello from Common Logging");

            string projectDir = System.IO.Directory.GetParent(
                System.IO.Directory.GetParent(
                    Environment.CurrentDirectory.ToString()).ToString()).ToString() + "\\";

            // resulting docx
            String OUTPUT_DOCX = projectDir + @"OUT_XHTMLFragment.docx";


            // 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);

            ikvm.runtime.Startup.addBootClassPathAssembly(
                System.Reflection.Assembly.GetAssembly(
                    typeof([email protected])));

            // Configure to find docx4j.properties
            // .. add as URL the dir containing docx4j.properties (not the file itself!)
            // and docx4j-ImportXHTML.properties (assumed to be in the same dir)
            Plutext.PropertiesConfigurator.setDocx4jPropertiesDir(projectDir + @"src\samples\resources\");
            // Workaround  to prevent ClassNotFoundException,
            // at IKVM.NativeCode.java.lang.Class.forName0
            // caused by Class.forName("org.docx4j.convert.in.xhtml.FSColorToHexString")
            // in docx4j code.
            ikvm.runtime.Startup.addBootClassPathAssembly(
                System.Reflection.Assembly.GetAssembly(
                    typeof([email protected])));

            String xhtml = "<ul>" +
                           "<li>Outer 1 </li>" +
                           "<li>Outer 2 </li>" +
                           "<ul>" +
                           "<li>Inner 1 </li>" +
                           "<li>Inner 2 </li>" +
                           "</ul>" +
                           "<li>Outer 3 </li>" +
                           "</ul>";

            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

            XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

            wordMLPackage.getMainDocumentPart().getContent().addAll(
                XHTMLImporter.convert(xhtml, null));

            Console.WriteLine(
                org.docx4j.XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));

            //Save the document
            Docx4J.save(wordMLPackage, new java.io.File(OUTPUT_DOCX), Docx4J.FLAG_NONE);
            log.Info("Saved: " + OUTPUT_DOCX);
        }