Page 1 of 1

Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Wed Jul 03, 2013 6:23 am
by hardywang
I downloaded the entire library with all dependencies, I tried to convert them into .Net assembly by using ikvm, the command I ran was

Code: Select all
ikvmc -target:library docx4j-2.8.1.jar antlr-2.7.7.jar antlr-runtime-3.3.jar avalon-framework-api-4.3.1.jar avalon-framework-impl-4.3.1.jar commons-codec-1.3.jar commons-io-1.3.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar fop-1.0.jar itext-2.1.7.jar jaxb-svg11-1.0.2.jar jaxb-xmldsig-core-1.0.0.jar jaxb-xslfo-1.0.1.jar log4j-1.2.15.jar poi-3.8.jar poi-scratchpad-3.8.jar serializer-2.7.1.jar stringtemplate-3.2.1.jar wmf2svg-0.9.0.jar xalan-2.7.1.jar xhtmlrenderer-1.0.0.jar xml-apis-1.3.04.jar xmlgraphics-commons-1.4.jar


I have included all jar files I can find in directory, but the conversion still missed whole bunches of other libraries.

Anyone has ever converted it to .Net successfully?

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Wed Jul 03, 2013 12:30 pm
by jason
hardywang wrote:Anyone has ever converted it to .Net successfully?


Sure, for several years Cisco has been shipping a product which relies on IKVM'd docx4j.

To get started, you can try something like:

ikvm-7.3.4830.0\bin\ikvmc.exe -out:docx4j.dll -target:library *.jar

It may give a lot of warnings. Ignore those for now, and try using your docx4j.dll in a .NET project.

I'd suggest you start with something simple, such as, in C#:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
using System;
using org.docx4j.openpackaging.packages;
using org.docx4j.openpackaging.parts;
using org.docx4j.openpackaging.parts.WordprocessingML;
using org.docx4j.wml;

namespace MergeDocx.NET
{
    class Test1
    {
        static void Main(string[] args)
        {
            string template = @"C:\Users\jason\Documents\HelloWorld.docx";

            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                    .load(new java.io.File(template));

            MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

            org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart
                    .getJaxbElement();
            Body body = wmlDocumentEl.getBody();

            Console.WriteLine(body.getClass().getName() );
        }

    }
}
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Make sure you have all the IKVM dlls referenced from your .NET project, including IKVM.AWT.WinForms

I see you also posted this question on StackOverflow. Please take the time to go back there to mark this as the answer.

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Thu Jul 04, 2013 12:10 am
by hardywang
Yup, it worked... but extremely sluggish. I used your command to generate DLL, reference it in my C# project, plus all ikvm DLLs.

I have following code to use it

Code: Select all
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(workingFile));
            // Fonts identity mapping – best on Microsoft Windows
            wordMLPackage.setFontMapper(new IdentityPlusMapper());
            // Set up converter
            PdfConversion c = new Conversion(wordMLPackage);
            // Write to output stream
            java.io.OutputStream os = new java.io.FileOutputStream(workingFile + ".pdf");
            PdfSettings ps = new PdfSettings();
            c.output(os, ps);


It runs, my output window displays

INFO org.docx4j.utils.Log4jConfigurator .configure line 48 - Since your log4j configuration (if any) was not found, docx4j has configured log4j automatically.
WARN org.docx4j.XmlUtils .<clinit> line 164 - Using default SAXParserFactory: null
WARN org.docx4j.XmlUtils .<clinit> line 189 - Using default DocumentBuilderFactory: null
INFO org.docx4j.jaxb.NamespacePrefixMapperUtils .getPrefixMapper line 56 - Using NamespacePrefixMapperSunInternal, which is suitable for Java 6
INFO org.docx4j.jaxb.Context .<clinit> line 59 - Using Java 6/7 JAXB implementation
INFO org.docx4j.jaxb.Context .<clinit> line 77 - loading Context jc


Then it hangs there for looooooong time (maybe after 10 or 15 minutes) to move on from first line to second. But the PDF file was never completed created.

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Thu Jul 04, 2013 2:18 am
by hardywang
I did some further testing to capture how much time it takes to execute the codes I listed above. You can see the gap between step 1 and 2, then between step 5 and 6.

// ---- start @ 03/07/2013 10:43:09 AM ---
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(workingFile));

// ---- start @ 03/07/2013 10:52:55 AM ---
wordMLPackage.setFontMapper(new IdentityPlusMapper());

// ---- start @ 03/07/2013 10:52:58 AM ---
PdfConversion c = new Conversion(wordMLPackage);

// ---- start @ 03/07/2013 10:52:58 AM ---
java.io.OutputStream os = new java.io.FileOutputStream(workingFile + ".pdf");

// ---- start @ 03/07/2013 10:52:58 AM ---
PdfSettings ps = new PdfSettings();
c.output(os, ps);

// ---- stop @ 03/07/2013 11:14:33 AM ---


But the good news is I got my PDF file after 30 minutes waiting. :oops:

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Thu Jul 04, 2013 7:39 am
by jason
It'll be faster if you "start without debugging" in Visual Studio.

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Fri Jul 05, 2013 12:11 am
by hardywang
Yes, that is true. Very interesting result. In a typical .net project the debug or not debug does not make major differences locally. But this one is huge.

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Fri Mar 07, 2014 8:55 am
by jason

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Fri Sep 09, 2016 11:07 am
by Maitredede
Hi,

I am trying to compile docx4j with latest version of IKVM.

I choosed to rely on maven to download latest version of docx4j and its dependencies.
I have made a simple pom.xml project with dependencies on docx4j, and a build script.

I have downloaded latest version of IKVM as specified on their blog : ikvmbin-8.1.5717.0.zip

Are there any dependecies I should add to my pom.xml to have a fully working assembly ? (not yet tested my result as time of writing)

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Fri Sep 09, 2016 1:30 pm
by Maitredede
I have made a simple test with the recompiled version : load a pptx, convert it to SVG.

First, with debugger, it take HUGE time, due to "an unfortunate philosophical difference between Java and .NET". So tested without debugger.

Code: Select all
PresentationMLPackage prez = (PresentationMLPackage)OpcPackage.load(new ikvm.io.InputStreamWrapper(inputstream));
string svg = SvgExporter.svg(prez, prez.getMainPresentationPart().getSlide(0));


Some "log4j" lines complaining about missing configuration...
Seams to work fine.

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Fri Sep 09, 2016 3:40 pm
by jason
See https://github.com/plutext/docx4j/blob/ ... d.xml#L394 for an ant task, now used with docx4j 3.3.1.

I have used ikvm-8.1.5717.0 successfully.

Soon docx4j.NET 3.3.1 will be in Nuget. https://www.nuget.org/packages/docx4j.NET/ is currently v3.2.0

Regarding logging, you can configure things so docx4j logs are routed through https://github.com/plutext/slf4j-net-commons-logging

Re: Convert docx4j 2.8.1 to .Net assemblies by using ikvm

PostPosted: Tue Sep 13, 2016 3:14 pm
by Maitredede
I also wonder why you have dependency along DocumentFormat.OpenXml :)
(issue on Github)...