Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a generic way to manipulate what gets copied to the clipboard from a FlowDocumentReader.</p> <p>The basic logic is, intercept CTRL+C with the FlowDocumentReader.KeyUp event, get the data that was just copied to the clipboard, modify it, then insert the modified data back to the clipboard.</p> <pre><code>private void flowDocumentReader1_KeyUp(object sender, KeyEventArgs e) { // Check for CTRL+C if (e.Key == Key.C &amp;&amp; Keyboard.Modifiers == ModifierKeys.Control) { // Get clipboard information IDataObject clipboardDataObject = Clipboard.GetDataObject(); // Get list of available formats currently in clipboard string[] formatsInClipboard = clipboardDataObject.GetFormats(); // Choose a format to use string desiredFormat = "Text"; // You can select Text, UnicodeText, Rich Text Format, etc. // Check for availability of desired format if (Array.IndexOf(formatsInClipboard, desiredFormat) &gt;= 0) { // Get copied text string initialClipboardText = Clipboard.GetData(desiredFormat).ToString(); // Update copied text string newClipboardText = InsertNameAndTimestamps(initialClipboardText); // Insert updated text to clipboard Clipboard.SetData(desiredFormat, newClipboardText); } } } // Sample text modification, modify however you like private string InsertNameAndTimestamps(string initialClipboardText) { return "[MODIFIED TEXT]" + initialClipboardText + "[MODIFIED TEXT]"; } </code></pre> <p>This example is simplified by just wrapping the copied text in "[MODIFIED TEXT]" which makes it easy to see that it works by copying something in the FlowDocumentReader and then pasting it in Notepad.</p> <p>It is also simplified by using plain text, but if you feel like mucking about with Rich Text Format, you have that option as well.</p> <p><strong>Edit:</strong> If you are unable to determine what and where to insert into the copied text, you will have to process FlowDocumentReader.Selection manually (since the default copy behavior does not copy UIElements to the clipboard).</p> <p>Assuming your FlowDocument looks like this:</p> <pre><code>&lt;FlowDocumentReader xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="flowDocumentReader1" KeyUp="flowDocumentReader1_KeyUp"&gt; &lt;FlowDocument&gt; &lt;BlockUIContainer&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock&gt;qwe&lt;/TextBlock&gt; &lt;TextBlock Grid.Column="1" TextAlignment="Right"&gt;00:15:37&lt;/TextBlock&gt; &lt;/Grid&gt; &lt;/BlockUIContainer&gt; &lt;Paragraph&gt;Hi&lt;/Paragraph&gt; &lt;BlockUIContainer&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock&gt;&lt;/TextBlock&gt; &lt;TextBlock Grid.Column="1" TextAlignment="Right"&gt;00:15:48&lt;/TextBlock&gt; &lt;/Grid&gt; &lt;/BlockUIContainer&gt; &lt;Paragraph&gt;How are you?&lt;/Paragraph&gt; &lt;/FlowDocument&gt; &lt;/FlowDocumentReader&gt; </code></pre> <p>You could process it like this:</p> <pre><code>private void flowDocumentReader1_KeyUp(object sender, KeyEventArgs e) { // Check for CTRL+C if (e.Key == Key.C &amp;&amp; Keyboard.Modifiers == ModifierKeys.Control) { StringBuilder stringBuilder = new StringBuilder(); // Prevent duplicate processing BlockUIContainer lastBlockUIContainer = null; // Get start of selection TextPointer currentPointer = flowDocumentReader1.Selection.Start; // Process selected text while (currentPointer != null &amp;&amp; currentPointer.GetOffsetToPosition(flowDocumentReader1.Selection.End) &gt; 0) { // Check for BlockUIContainer BlockUIContainer blockUIContainer = currentPointer.GetAdjacentElement(LogicalDirection.Backward) as BlockUIContainer; if (blockUIContainer != null &amp;&amp; blockUIContainer != lastBlockUIContainer) { // Get text from Grid Grid grid = blockUIContainer.Child as Grid; if (grid != null) { string replacementText = String.Format("{0}\t{1}", (grid.Children[0] as TextBlock).Text, (grid.Children[1] as TextBlock).Text ); stringBuilder.AppendLine(replacementText); } // GetAdjacentElement can return the container from multiple TextPointers, so only process once lastBlockUIContainer = blockUIContainer; } else { // Get text at current position string text = currentPointer.GetTextInRun(LogicalDirection.Forward); if (!String.IsNullOrEmpty(text)) { stringBuilder.AppendLine(text); } } // Move to next TextPointer currentPointer = currentPointer.GetNextContextPosition(LogicalDirection.Forward); } // Insert updated text to clipboard string newClipboardText = stringBuilder.ToString(); Clipboard.SetData("Text", newClipboardText); } } </code></pre> <p>Happy coding!</p>
 

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