How to set HTML to clipboard in C#?

前端 未结 4 1595
谎友^
谎友^ 2020-11-29 12:54

I want to put rich text in HTML on the clipboard so when the users paste to Word, it will include the source HTML formatting.

Using the Clipboard.SetText method does

相关标签:
4条回答
  • 2020-11-29 13:34

    As Arthur had mentioned I used the code at Setting HTML/Text to Clipboard revisited

    I had to add linefeeds to the Header to get it to work (in this case VB)

    Private Const Header As String = "Version:0.9" & vbCrLf & "StartHTML:<<<<<<<<1" & vbCrLf & "EndHTML:<<<<<<<<2" & vbCrLf & "StartFragment:<<<<<<<<3" & vbCrLf & "EndFragment:<<<<<<<<4" & vbCrLf & "StartSelection:<<<<<<<<3" & vbCrLf & "EndSelection:<<<<<<<<4"
    

    Hope this helps

    0 讨论(0)
  • 2020-11-29 13:35

    I found some code: https://www.experts-exchange.com/questions/21966855/Create-a-hyperlink-in-VB-net-copy-to-clipboard-Should-be-able-to-paste-hyperlink-in-Microsoft-Word-Excel.html

    This code handles the problems of updating the start and end indexes.

    Converted to c#:

    public void AddHyperlinkToClipboard(string link, string description)
    {
        const string sContextStart = "<HTML><BODY><!--StartFragment -->";
        const string sContextEnd = "<!--EndFragment --></BODY></HTML>";
        const string m_sDescription = "Version:1.0" + Constants.vbCrLf + "StartHTML:aaaaaaaaaa" + Constants.vbCrLf + "EndHTML:bbbbbbbbbb" + Constants.vbCrLf + "StartFragment:cccccccccc" + Constants.vbCrLf + "EndFragment:ffffdffffdffffdd" + Constants.vbCrLf;
    
        string sHtmlFragment = "<A HREF=" + Strings.Chr(34) + link + Strings.Chr(34) + ">" + description + "</A>";
    
        string sData = m_sDescription + sContextStart + sHtmlFragment + sContextEnd;
        sData = sData.Replace("aaaaaaaaaa", m_sDescription.Length.ToString().PadLeft(10, '0'));
        sData = sData.Replace("bbbbbbbbbb", sData.Length.ToString().PadLeft(10, '0'));
        sData = sData.Replace("cccccccccc", (m_sDescription + sContextStart).Length.ToString().PadLeft(10, '0'));
        sData = sData.Replace("ffffdffffdffffdd", (m_sDescription + sContextStart + sHtmlFragment).Length.ToString().PadLeft(10, '0'));
        sData.Dump();
        Clipboard.SetDataObject(new DataObject(DataFormats.Html, sData), true );
    }
    
    0 讨论(0)
  • 2020-11-29 13:36

    Arthur is right about the header, but the important thing to note here is that the data isn't going to be on the clipboard as plain text. You have to use CF_HTML. You can read about that at MSDN: http://msdn.microsoft.com/en-us/library/aa767917(v=vs.85).aspx To be proper, you'd have a CF_TEXT showing simply: "Hello!", and then CF_HTML with the HTML header and data, as in Arthur's example.

    0 讨论(0)
  • 2020-11-29 13:41

    When setting HTML text, you need to provide a header with additional information to what fragment of the html you actually want to paste while being able to provide additional styling around it:

    Version:0.9
    StartHTML:000125
    EndHTML:000260
    StartFragment:000209
    EndFragment:000222
    <HTML>
    <head>
    <title>HTML clipboard</title>
    </head>
    <body>
    <!–StartFragment–><b>Hello!</b><!–EndFragment–>
    </body>
    </html>
    

    With the header (and correct indexes), calling Clipboard.SetText with TextDataFormat.Html will do the trick.

    To handle HTML and plain text pastes, you can’t use the Clipboard.SetText method, as it clears the clipboard each time it’s called; you need to create a DataObject instance, call its SetData method once with HTML and once with plain text, and then set the object to clipboard using Clipboard.SetDataObject.

    Update

    See "Setting HTML/Text to Clipboard revisited" for more details and ClipboardHelper implementation.

    0 讨论(0)
提交回复
热议问题