Page 1 of 1

Inserting WMF images

PostPosted: Tue May 03, 2011 12:02 am
by ppa_waw
Hi,

I have modified AddImage.java sample to add WMF image (by simply pointing to load sample WMF file instead of PDF as it is in original sample) and I cannot made it to work.
Sample runs and than it spits with exception:
Code: Select all
org.docx4j.openpackaging.parts.WordprocessingML.MetafileWmfPart cannot be cast to org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage


If I change sample file to use MetafileWmfPart instead of BinaryPartAbstractImage than there is a problem as MetafileWmfPart class has no methods createImagePart or createImageInline. I think this is because MetafileWmfPart extends MetafilePart. Similar class MetafileEmfPart extends BinaryPartAbstractImage. Is it possible to change MetafileWmfPart to also extend BinaryPartAbstractImage? Or some other refactoring is needed as createImagePart returns BinaryPartAbstractImage, so such method of MetafileWmfPart should return MetafileWmfPart


Thanks,
Piotr

Re: Inserting WMF images

PostPosted: Wed May 04, 2011 12:36 am
by jason
How about:

Index: src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafileEmfPart.java
===================================================================
--- src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafileEmfPart.java (revision 1474)
+++ src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafileEmfPart.java (working copy)
@@ -53,7 +53,7 @@
to import EMF
*
*/
-public class MetafileEmfPart extends BinaryPartAbstractImage {
+public class MetafileEmfPart extends MetafilePart {

public MetafileEmfPart(PartName partName) throws InvalidFormatException {
super(partName);
Index: src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafilePart.java
===================================================================
--- src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafilePart.java (revision 1474)
+++ src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafilePart.java (working copy)
@@ -3,7 +3,7 @@
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.parts.PartName;

-public abstract class MetafilePart extends BinaryPart {
+public abstract class MetafilePart extends BinaryPartAbstractImage {

public MetafilePart(PartName partName) throws InvalidFormatException {
super(partName);

Re: Inserting WMF images

PostPosted: Wed May 04, 2011 1:35 am
by ppa_waw
Perfect, there is only one remaining problem. In sample I have:
Code: Select all
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

converting this line to:
Code: Select all
MetafileWmfPart imagePart = MetafileWmfPart.createImagePart(wordMLPackage, bytes);

triggers error:
Code: Select all
Type mismatch: cannot convert from BinaryPartAbstractImage to MetafileEmfPart

Problem I guess is that createImagePart method from BinaryPartAbstractImage creates BinaryPartAbstractImage object and not MetafileWmfPart object

May I ask to take a look into this? Many thanks :)

Piotr

Re: Inserting WMF images

PostPosted: Wed May 04, 2011 9:44 am
by jason
Is there any particular reason you want to do that?

Why don't you just leave it as is, ie:
Code: Select all
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

Re: Inserting WMF images

PostPosted: Wed May 04, 2011 8:43 pm
by ppa_waw
OK, it works, I am getting nice word document with WMF pictures inside.

Many thanks for your help,
Piotr