4

我正在尝试使用MAC从Clipboard获取字体信息。下面是从Clipboard获取值的代码。

代码

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。请指教。

4

2 回答 2

0

为了识别所有粗体元素,您可以使用返回整个 html 的 doc.outerHtml() 方法。

假设在我的剪贴板中,我复制了以下数据,例如

粗体 1 粗体 2 粗体 3 粗体 4

现在,当您使用 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>

所以现在你只需要放一些解析逻辑来读取粗体元素。

于 2013-10-19T14:55:13.090 回答
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();
            }
        }}}
于 2013-10-16T18:38:57.323 回答