Calling Aspose.Word from Java

From Larry:

Roman,

I have another question for you: I am trying to use the Aspose.Word COM
interface from Java using a package called Jacob. Jacob provides support
identical to the old com.ms.com package. This approach is very similar
to the COM support provided for C++. However, I’m having problems due to
Aspose.Word’s extensive use of arrays. For example, when I call
MailMerge.GetFieldNames(), I am uncertain of what gets returned. Likewise,
when I call MailMerge.Execute(String[], Object[]), I am uncertain of what I
should try to pass in. I’m trying to call using my Invoke() method and
passing everything using my Variant and SafeArrray classes (which are very
similar to the COleVariant and COleSafeArray wrapper classes in C++). Any
idea’s as to how I should proceed?

Hi Larry,

I will try to help, but at the moment we have limited resources to delve deep into COM and Java support. Aspose.Word was not really designed with these clients in mind and we only introduced a few small changes to support scripting clients such as ASP and VBScript assuming little effort will satisfy a number of clients.

You need to look at how .NET COM Interop marshals the arrays and other parameters.

MSDN Interop Marshaling

MSDN Default Marshaling for Arrays

I must reiterate the warning that that Aspose.Word does not explicitly define interfaces and relies on .NET to automatically generate them. This means it will work fine with scripting (late bound clients), but there could be versioning problems with vtable bound clients. We have not defined the interfaces explicitly because of the intention to put minimum effort into COM support by our components, but we might reconsider this later.

If anyone else is enterested in better COM and even Java support by Aspose products, please express your interest in the forums.

Your references were most helpful! Here’s a snipett of Java code that calls an Aspose.Word routine which returns a “string array” (I imagine that the C++ code would be similar):

SafeArray saMergeFields = Dispatch.call( mailMerge, “GetFieldNames” ).toSafeArray( false );
String[] arrayStr = mailMergeFields.toStringArray();

Here’s some Java code which calls an Aspose.Word routine and passes in a “string array” and an “object array”:

SafeArray saFields = new SafeArray( Variant.VariantString, aMergeFieldData.size() );
SafeArray saValues = new SafeArray( Variant.VariantVariant, aMergeFieldData.size() );

// – SNIP - Load safearrays & get mailMerge IDispatch interface

Object mergedDocument = Dispatch.call( mailMerge, “Execute”, saFields, saValues ).toDispatch();

thanks for your help,

Larry

Hi Larry,

Thank you for the code example, I hope it will be very useful to other customers. We were caught a little off guard with high demand for Aspose.Word to support COM clients.

By the way, can you deal with enumerated types from your code? I think it will be a bit complicated to work with Aspose.Word enums using late binding.

Can you import a typelibrary (I mean in general) in your enviroment and use early binding and enums defined in the typelibrary?

We will consider defining explicit interfaces and including a type library in the installer.

We were caught a little off guard with high demand for Aspose.Word to support COM clients.

Many of us are still back here in the dark ages . . . ;^)

By the way, can you deal with enumerated types from your code? I think it will be a bit complicated to work with Aspose.Word enums using late binding.
Can you import a typelibrary (I mean in general) in your enviroment and use early binding and enums defined in the typelibrary?

No. Jacob only provides IDispatch support. There’s supposed to be a companion package called Jacobgen that is supposed to generate “wrapper classes”, etc. directly from a type library. However, we haven’t been able to get it to work yet. Of course, since we had been calling MS Word’s scripting interface, it wasn’t much of a problem . . .

Larry

Aspose.Word for Java that is just written in pure Java has been released.