Skip to main content

How to export vector graphics files to the system clipboard?

5 replies [Last post]
mtk7801
Offline
Joined: 2006-05-10
Points: 0

I am using the freehep vector graphics package for java (available at java.freehep.org) and want to copy the information of an EMF file to the Windows system clipboard. I am pretty sure it is possible, since it is talked about in the description of class VectorGraphicsTransferable (http://java.freehep.org/doc/release/v1.2.2/doc/api/index.html), but I have found no description this far how to do it. I am somewhat familiar with clipboard use in Java and know how to copy jpg/gif/png files to the system clipboard. Using the freehep package it is also very easy to paint vector graphics directly from within a Java program to the clipboard:

VectorGraphicsTransferable imageSelection = new VectorGraphicsTransferable(frame);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null);

But how to transfer the contents of a vector graphics *file* to the clipboard from within a Java program? Is there any simple way to do it or do you have to use a lot of "low level" code? Anyone knows??

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rosmord
Offline
Joined: 2006-05-05
Points: 0

Well, the short answer about copying EMF on the clipboard in a java-only way is : it's not currently possible. On m/s windows, metafiles are not copied on the clipboard like other data is: what is copied is simply a pointer to them.

Of course, it's possible to do it through JNI. There is even a quite complete library on sale for this (google for it).

But there is another solution if you need free software. You can't copy vector graphics, but you can copy text. Even complex text. Even RTF text.

So, the solution is the following:
a) create a WMF or EMF file containing your picture.
b) embed it in a RTF document
c) copy the whole on the clipboard.

I do it in one of my softwares, called JSesh (it's free and opensource, so you can have a look. I intend to extract the relevant classes from it and to make them a standalone library one of these days). Result: it can copy and paste graphics to word and openoffice swriter, on linux, windows, and the mac!

regards,

S. Rosmorduc

mtk7801
Offline
Joined: 2006-05-10
Points: 0

Thanks for your help!

vossp2
Offline
Joined: 2004-07-02
Points: 0

WMFWriter is relative inexpensive and reliable in our experience. You simply substitute a WMFGraphics object in ordinary calls to exsting "void paint(Graphics g)" methods.

leouser
Offline
Joined: 2005-12-12
Points: 0

you mean transfer a file from the Filesystem? You might want to try to write a Transferable that offers the DataFlavor.javaFileListFlavor

which has this description:
To transfer a list of files to/from Java (and the underlying platform) a DataFlavor of this type/subtype and representation class of java.util.List is used. Each element of the list is required/guaranteed to be of type java.io.File.

------------
leouser

mtk7801
Offline
Joined: 2006-05-10
Points: 0

Basically, I would like to from within a Java program put the contents of an EMF file on the system clipboard so that I can then paste the EMF image into MS Word. Preferrably, the resulting doc-file should be exactly the same as if I had imported the EMF file directly in MS Word with "import image". I tried experimenting a little with DataFlavor.javaFileListFlavor, but all I seem to be able to do with that is to export a list of files to the clipboard, which doesn't yield anything when pasting into another program like Word. For jpg/gif/png files the situation is easier, because then you can

1. Create an image from the file:
Image image = toolkit.createImage(fileName);

2. Connect the image to a transferable ImageSelection object, where ImageSelection is easily implemented because the built-in Dataflavor.imageFlavor represents Image objects

3. Export the image to the clipboard:
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null);

However, using this syntax and then pasting into Word will make the doc-file much bigger than if you had imported the image directly in Word because Java seems to remove the image compression when putting the image on the system clipboard. I have not yet figured out any way to export jpg/gif/png images to the clipboard and keeping the compression intact, so that the resulting doc-file will not get any bigger than if you had imported directly in Word. I assume it may be hard to achieve without digging into Windows details.
Anyway, when it comes to vector image files, like EMF files, 1 above does not work. I am not even sure it is possible to create an Image object from an EMF file. If it is possible, 2 and 3 should work unchanged. If it is not possible, one should be able to create an object of some other type than Image from the EMF file and then connect this object to some customized DataFlavor object. How this is done I have no idea about. I am pretty sure it could be done with some of the classes from the FreeHep package, and maybe even with standard Java classes, but there seems to be a jungle of classes to browse through, and without any clear guidelines it feels like looking for a needle in a haystack.