Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have the formatting data included in your data, only if the text in the clipboard is in Rich Formatted Text (RTF). In this case, when you checked and the content of clipboard contained RTF text, you can check its font and other formatting information. You can use this sample code as a starting point.</p> <pre><code>import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; public class ClipboardTest { public static void main(String[] args) { System.out.println(getClipboardData()); } public static String getClipboardData() { String result = ""; Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); // odd: the Object param of getContents is not currently used Transferable contents = clipboard.getContents(null); DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text"); DataFlavor dfTxt = DataFlavor.stringFlavor; boolean hasTransferableRTFText = (contents != null) &amp;&amp; contents.isDataFlavorSupported(dfRTF); boolean hasTransferableTxtText = (contents != null) &amp;&amp; contents.isDataFlavorSupported(dfTxt); if (hasTransferableRTFText) { try { result = streamToString((InputStream)contents .getTransferData(dfRTF)); } catch (Exception ex) { ex.printStackTrace(); } } else if (hasTransferableTxtText) { try { result = (String)contents .getTransferData(dfTxt); } catch (Exception ex) { ex.printStackTrace(); } } return result; } private static String streamToString(InputStream transferData) { return slurp(transferData, 1024); } public static String slurp(final InputStream is, final int bufferSize) { final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); try { final Reader in = new InputStreamReader(is, "UTF-8"); try { for (;;) { int rsz = in.read(buffer, 0, buffer.length); if (rsz &lt; 0) break; out.append(buffer, 0, rsz); } } finally { in.close(); } } catch (UnsupportedEncodingException ex) { /* ... */ } catch (IOException ex) { /* ... */ } return out.toString(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload