我正在尝试使用 MAC 从剪贴板获取字体信息.Below是从剪贴板获取值的代码。
CODE
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
DataFlavor dfTxt = DataFlavor.stringFlavor;
boolean hasTransferableRTFText = (contents != null)
&& contents.isDataFlavorSupported(dfRTF);
boolean hasTransferableTxtText = (contents != null)
&& contents.isDataFlavorSupported(dfTxt);
if (hasTransferableRTFText) {
try {
result = streamToString((InputStream)contents.getTransferData(dfRTF));
System.out.println("dfRTF "+result);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (hasTransferableTxtText) {
try {
result = (String)contents.getTransferData(dfTxt);
System.out.println("dfTxt "+result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
并且代码返回 MAC 解码后的值如下:
dfRTF {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\b\fs24 \cf0 Hello WORLD}
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\b\fs24 \cf0 Hello WORLD}
我知道\f0\b\fs24 \cf0 Hello WORLD}
包含\b
表示 BOLD
的字体信息。
如何对其进行编码以获得该特定值,例如我想知道它是 BOLDED还是NO。请提供建议。
答案 0 :(得分:0)
如果我没有找到你需要的东西,你可以尝试将其转换为html并使用像jsoup这样的库来提取所需的信息。
这是一个提取到剪贴板中找到的第一个粗体文本的示例
package rtfTest;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.swing.JEditorPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class TestRtf{
public static void main (String[] args) {
TestRtf t = new TestRtf();
t.readClipboard() ;
}
@SuppressWarnings("resource")
static String streamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
public static String rtfToHtml(Reader rtf) throws IOException { // From http://www.codeproject.com/Tips/136483/Java-How-to-convert-RTF-into-HTML
JEditorPane p = new JEditorPane();
p.setContentType("text/rtf");
EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
try {
kitRtf.read(rtf, p.getDocument(), 0);
kitRtf = null;
EditorKit kitHtml = p.getEditorKitForContentType("text/html");
Writer writer = new StringWriter();
kitHtml.write(writer, p.getDocument(), 0, p.getDocument().getLength());
return writer.toString();
} catch (BadLocationException e) {
e.printStackTrace();
}
return null;
}
public void readClipboard () {
String result;
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
DataFlavor dfTxt = DataFlavor.stringFlavor;
boolean hasTransferableRTFText = (contents != null)
&& contents.isDataFlavorSupported(dfRTF);
boolean hasTransferableTxtText = (contents != null)
&& contents.isDataFlavorSupported(dfTxt);
if (hasTransferableRTFText) {
try {
// result = streamToString((InputStream)contents.getTransferData(dfRTF));
// Convert rtf to html
result = rtfToHtml(new StringReader(streamToString((InputStream)contents.getTransferData(dfRTF))));
// Example of text extraction from html
// Parse html
Document doc = Jsoup.parse(result);
// Select first bold text
Element firstBoldElt = doc.select("b").first();
String firstBoldText = firstBoldElt.text();
System.out.println(firstBoldText);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (hasTransferableTxtText) {
try {
result = (String)contents.getTransferData(dfTxt);
System.out.println("dfTxt "+ result);
} catch (Exception ex) {
ex.printStackTrace();
}
}}}
答案 1 :(得分:0)
为了识别所有粗体元素,你可以使用doc.outerHtml()方法返回整个html。
假设在我的剪贴板中我复制了以下数据,如
<强> Bold1 Bold2 强> Bold3 的 Bold4 强>
现在当你使用doc.outerHtml()时,你会得到以下回复
<html><head><style><!--p.DefaultaParagraphaFont {bold:normal;italic:;underline:;}--></style></head><body><p class="default"> <span style="color: #000000; font-size: 11pt; font-family: Calibri"> <b>Bold1</b> </span> </p> <p class="default"> <span style="color: #000000; font-size: 11pt; font-family: Calibri"> <b>Bold2</b> </span> </p> <p class="default"> <span style="color: #000000; font-size: 11pt; font-family: Calibri"> Bold3 </span> </p> <p class="default"> <span style="color: #000000; font-size: 11pt; font-family: Calibri"> <b>Bold4</b> </span> </p></body></html>
所以现在你只需要放一些解析逻辑来读取粗体元素。