M
mortenalver
I am trying to put RTF text on the clipboard - in such a way that e.g.
Word will paste it as formatted text. Below is a runnable program that
shows what I'm doing. You need an RTF file which you give as argument
to the program. The program will read the RTF file and put the text on
the clipboard. I'm trying to use a Transferable with a
"application/rtf" DataFlavor. Still, the text always gets pasted as
plain text.
I'd be happy to get a pointer to what I'm doing wrong here.
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.io.*;
class RtfTest implements ClipboardOwner {
// Provide the path to an rtf file as the first argument
public static void main(String[] args) {
new RtfTest(args[0]);
}
public RtfTest(String filename) {
try {
BufferedReader in = new BufferedReader
(new FileReader(new File(filename)));
StringBuffer s = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
s.append(line);
}
in.close();
RtfSelection sel = new RtfSelection(s.toString());
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(sel, this);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void lostOwnership(Clipboard cl, Transferable content) {
}
class RtfSelection implements Transferable {
DataFlavor rtfFlavor;
DataFlavor[] supportedFlavors;
private String content;
public RtfSelection(String s) {
content = s;
try {
rtfFlavor = new DataFlavor
("application/rtf; class=java.io.InputStream");
supportedFlavors = new DataFlavor[]
{rtfFlavor, DataFlavor.stringFlavor};
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(rtfFlavor) ||
flavor.equals(DataFlavor.stringFlavor);
}
public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors() {
System.out.println("..");
return supportedFlavors;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (flavor.equals(DataFlavor.stringFlavor)) {
System.out.println("Delivering string data.");
return content;
}
else if (flavor.equals(rtfFlavor)) {
System.out.println("Delivering rtf data.");
byte[] byteArray = content.getBytes();
return new ByteArrayInputStream(byteArray);
}
throw new UnsupportedFlavorException(flavor);
}
}
}
Word will paste it as formatted text. Below is a runnable program that
shows what I'm doing. You need an RTF file which you give as argument
to the program. The program will read the RTF file and put the text on
the clipboard. I'm trying to use a Transferable with a
"application/rtf" DataFlavor. Still, the text always gets pasted as
plain text.
I'd be happy to get a pointer to what I'm doing wrong here.
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.io.*;
class RtfTest implements ClipboardOwner {
// Provide the path to an rtf file as the first argument
public static void main(String[] args) {
new RtfTest(args[0]);
}
public RtfTest(String filename) {
try {
BufferedReader in = new BufferedReader
(new FileReader(new File(filename)));
StringBuffer s = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
s.append(line);
}
in.close();
RtfSelection sel = new RtfSelection(s.toString());
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(sel, this);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void lostOwnership(Clipboard cl, Transferable content) {
}
class RtfSelection implements Transferable {
DataFlavor rtfFlavor;
DataFlavor[] supportedFlavors;
private String content;
public RtfSelection(String s) {
content = s;
try {
rtfFlavor = new DataFlavor
("application/rtf; class=java.io.InputStream");
supportedFlavors = new DataFlavor[]
{rtfFlavor, DataFlavor.stringFlavor};
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(rtfFlavor) ||
flavor.equals(DataFlavor.stringFlavor);
}
public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors() {
System.out.println("..");
return supportedFlavors;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (flavor.equals(DataFlavor.stringFlavor)) {
System.out.println("Delivering string data.");
return content;
}
else if (flavor.equals(rtfFlavor)) {
System.out.println("Delivering rtf data.");
byte[] byteArray = content.getBytes();
return new ByteArrayInputStream(byteArray);
}
throw new UnsupportedFlavorException(flavor);
}
}
}