c# 将html添加进剪贴板(带格式)
调用:
ClipboardHelper.CopyToClipboard("<h1>hello world</h1>", "");
/// <summary>
/// Helper to encode and set HTML fragment to clipboard.<br/>
/// See http://theartofdev.com/2014/06/12/setting-htmltext-to-clipboard-revisited/.<br/>
/// <seealso cref="CreateDataObject"/>.
/// </summary>
/// <remarks>
/// The MIT License (MIT) Copyright (c) 2014 Arthur Teplitzki.
/// </remarks>
public static class ClipboardHelper
{
#region Fields and Consts /// <summary>
/// The string contains index references to other spots in the string, so we need placeholders so we can compute the offsets. <br/>
/// The <![CDATA[<<<<<<<]]>_ strings are just placeholders. We'll back-patch them actual values afterwards. <br/>
/// The string layout (<![CDATA[<<<]]>) also ensures that it can't appear in the body of the html because the <![CDATA[<]]> <br/>
/// character must be escaped. <br/>
/// </summary>
private const string Header = @"Version:0.9
StartHTML:<<<<<<<<1
EndHTML:<<<<<<<<2
StartFragment:<<<<<<<<3
EndFragment:<<<<<<<<4
StartSelection:<<<<<<<<3
EndSelection:<<<<<<<<4"; /// <summary>
/// html comment to point the beginning of html fragment
/// </summary>
public const string StartFragment = "<!--StartFragment-->"; /// <summary>
/// html comment to point the end of html fragment
/// </summary>
public const string EndFragment = @"<!--EndFragment-->"; /// <summary>
/// Used to calculate characters byte count in UTF-8
/// </summary>
private static readonly char[] _byteCount = new char[]; #endregion /// <summary>
/// Create <see cref="DataObject"/> with given html and plain-text ready to be used for clipboard or drag and drop.<br/>
/// Handle missing <![CDATA[<html>]]> tags, specified start\end segments and Unicode characters.
/// </summary>
/// <remarks>
/// <para>
/// Windows Clipboard works with UTF-8 Unicode encoding while .NET strings use with UTF-16 so for clipboard to correctly
/// decode Unicode string added to it from .NET we needs to be re-encoded it using UTF-8 encoding.
/// </para>
/// <para>
/// Builds the CF_HTML header correctly for all possible HTMLs<br/>
/// If given html contains start/end fragments then it will use them in the header:
/// <code><![CDATA[<html><body><!--StartFragment-->hello <b>world</b><!--EndFragment--></body></html>]]></code>
/// If given html contains html/body tags then it will inject start/end fragments to exclude html/body tags:
/// <code><![CDATA[<html><body>hello <b>world</b></body></html>]]></code>
/// If given html doesn't contain html/body tags then it will inject the tags and start/end fragments properly:
/// <code><![CDATA[hello <b>world</b>]]></code>
/// In all cases creating a proper CF_HTML header:<br/>
/// <code>
/// <![CDATA[
/// Version:1.0
/// StartHTML:000000177
/// EndHTML:000000329
/// StartFragment:000000277
/// EndFragment:000000295
/// StartSelection:000000277
/// EndSelection:000000277
/// <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
/// <html><body><!--StartFragment-->hello <b>world</b><!--EndFragment--></body></html>
/// ]]>
/// </code>
/// See format specification here: http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/clipboard/htmlclipboard.asp
/// </para>
/// </remarks>
/// <param name="html">a html fragment</param>
/// <param name="plainText">the plain text</param>
public static DataObject CreateDataObject(string html, string plainText)
{
html = html ?? String.Empty;
var htmlFragment = GetHtmlDataString(html); // re-encode the string so it will work correctly (fixed in CLR 4.0)
if (Environment.Version.Major < && html.Length != Encoding.UTF8.GetByteCount(html))
htmlFragment = Encoding.Default.GetString(Encoding.UTF8.GetBytes(htmlFragment)); var dataObject = new DataObject();
dataObject.SetData(DataFormats.Html, htmlFragment);
dataObject.SetData(DataFormats.Text, plainText);
dataObject.SetData(DataFormats.UnicodeText, plainText);
return dataObject;
} /// <summary>
/// Clears clipboard and sets the given HTML and plain text fragment to the clipboard, providing additional meta-information for HTML.<br/>
/// See <see cref="CreateDataObject"/> for HTML fragment details.<br/>
/// </summary>
/// <example>
/// ClipboardHelper.CopyToClipboard("Hello <b>World</b>", "Hello World");
/// </example>
/// <param name="html">a html fragment</param>
/// <param name="plainText">the plain text</param>
public static void CopyToClipboard(string html, string plainText)
{
var dataObject = CreateDataObject(html, plainText);
Clipboard.SetDataObject(dataObject, true);
} /// <summary>
/// Generate HTML fragment data string with header that is required for the clipboard.
/// </summary>
/// <param name="html">the html to generate for</param>
/// <returns>the resulted string</returns>
private static string GetHtmlDataString(string html)
{
var sb = new StringBuilder();
sb.AppendLine(Header);
sb.AppendLine(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"); // if given html already provided the fragments we won't add them
int fragmentStart, fragmentEnd;
int fragmentStartIdx = html.IndexOf(StartFragment, StringComparison.OrdinalIgnoreCase);
int fragmentEndIdx = html.LastIndexOf(EndFragment, StringComparison.OrdinalIgnoreCase); // if html tag is missing add it surrounding the given html (critical)
int htmlOpenIdx = html.IndexOf("<html", StringComparison.OrdinalIgnoreCase);
int htmlOpenEndIdx = htmlOpenIdx > - ? html.IndexOf('>', htmlOpenIdx) + : -;
int htmlCloseIdx = html.LastIndexOf("</html", StringComparison.OrdinalIgnoreCase); if (fragmentStartIdx < && fragmentEndIdx < )
{
int bodyOpenIdx = html.IndexOf("<body", StringComparison.OrdinalIgnoreCase);
int bodyOpenEndIdx = bodyOpenIdx > - ? html.IndexOf('>', bodyOpenIdx) + : -; if (htmlOpenEndIdx < && bodyOpenEndIdx < )
{
// the given html doesn't contain html or body tags so we need to add them and place start/end fragments around the given html only
sb.Append("<html><body>");
sb.Append(StartFragment);
fragmentStart = GetByteCount(sb);
sb.Append(html);
fragmentEnd = GetByteCount(sb);
sb.Append(EndFragment);
sb.Append("</body></html>");
}
else
{
// insert start/end fragments in the proper place (related to html/body tags if exists) so the paste will work correctly
int bodyCloseIdx = html.LastIndexOf("</body", StringComparison.OrdinalIgnoreCase); if (htmlOpenEndIdx < )
sb.Append("<html>");
else
sb.Append(html, , htmlOpenEndIdx); if (bodyOpenEndIdx > -)
sb.Append(html, htmlOpenEndIdx > - ? htmlOpenEndIdx : , bodyOpenEndIdx - (htmlOpenEndIdx > - ? htmlOpenEndIdx : )); sb.Append(StartFragment);
fragmentStart = GetByteCount(sb); var innerHtmlStart = bodyOpenEndIdx > - ? bodyOpenEndIdx : (htmlOpenEndIdx > - ? htmlOpenEndIdx : );
var innerHtmlEnd = bodyCloseIdx > - ? bodyCloseIdx : (htmlCloseIdx > - ? htmlCloseIdx : html.Length);
sb.Append(html, innerHtmlStart, innerHtmlEnd - innerHtmlStart); fragmentEnd = GetByteCount(sb);
sb.Append(EndFragment); if (innerHtmlEnd < html.Length)
sb.Append(html, innerHtmlEnd, html.Length - innerHtmlEnd); if (htmlCloseIdx < )
sb.Append("</html>");
}
}
else
{
// handle html with existing start\end fragments just need to calculate the correct bytes offset (surround with html tag if missing)
if (htmlOpenEndIdx < )
sb.Append("<html>");
int start = GetByteCount(sb);
sb.Append(html);
fragmentStart = start + GetByteCount(sb, start, start + fragmentStartIdx) + StartFragment.Length;
fragmentEnd = start + GetByteCount(sb, start, start + fragmentEndIdx);
if (htmlCloseIdx < )
sb.Append("</html>");
} // Back-patch offsets (scan only the header part for performance)
sb.Replace("<<<<<<<<1", Header.Length.ToString("D9"), , Header.Length);
sb.Replace("<<<<<<<<2", GetByteCount(sb).ToString("D9"), , Header.Length);
sb.Replace("<<<<<<<<3", fragmentStart.ToString("D9"), , Header.Length);
sb.Replace("<<<<<<<<4", fragmentEnd.ToString("D9"), , Header.Length); return sb.ToString();
} /// <summary>
/// Calculates the number of bytes produced by encoding the string in the string builder in UTF-8 and not .NET default string encoding.
/// </summary>
/// <param name="sb">the string builder to count its string</param>
/// <param name="start">optional: the start index to calculate from (default - start of string)</param>
/// <param name="end">optional: the end index to calculate to (default - end of string)</param>
/// <returns>the number of bytes required to encode the string in UTF-8</returns>
private static int GetByteCount(StringBuilder sb, int start = , int end = -)
{
int count = ;
end = end > - ? end : sb.Length;
for (int i = start; i < end; i++)
{
_byteCount[] = sb[i];
count += Encoding.UTF8.GetByteCount(_byteCount);
}
return count;
}
}
c# 将html添加进剪贴板(带格式)的更多相关文章
- CentOS自带mysql配置(密码更改、端口开放访问、添加进系统启动项)
前些天虚拟机安装好了CentOS6.1,但是自己想远程连接自带的mysql发现不知道如何改密码,于是谷歌一下,把结果记录下来,方便后期自己使用: 方法一: # /etc/init.d/mysql st ...
- ArcGIS中添加进自定义的ttf字符标记符号
原文:ArcGIS中添加进自定义的ttf字符标记符号 ArcGIS系统中的样式可能不能满足实际生产需要,为了实现快速制图,可自定义一些样式,以便重复利用. 1. 制作的符号库 使用 FontCre ...
- ubuntu 应用添加进环境变量
BG:公司同事使用的电脑系统大多为windows ,有部分mac和Ubuntu(我就是那个部分Ubuntu),某些情况为了统一格式,便下载了一些解压即可使用的软件,但是每次点开文件夹然后点开程序很繁琐 ...
- eclipse中添加进新的java项目中文乱码
eclipse中添加进新的java项目中文乱码 添加学习的一些项目进eclipse中,结果其中的中文注释都变成了乱码 右击项目,点最下面的属性,出来新得弹框 在文本文件编码部分可以发现是GBK格式,选 ...
- JS019. 原生JS使用new Blob()实现带格式导出Word、Excel(提供无编程基础将页面上表格导出到本地的方法)
导出效果 代码实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Java导出带格式的Excel数据到Word表格
前言 在Word中创建报告时,我们经常会遇到这样的情况:我们需要将数据从Excel中复制和粘贴到Word中,这样读者就可以直接在Word中浏览数据,而不用打开Excel文档.在本文中,您将学习如何使用 ...
- 如何通过Java导出带格式的 Excel 数据到 Word 表格
在Word中制作报表时,我们经常需要将Excel中的数据复制粘贴到Word中,这样则可以直接在Word文档中查看数据而无需打开另一个Excel文件.但是如果表格比较长,内容就会存在一定程度的丢失,无法 ...
- (转)sscanf() - 从一个字符串中读进与指定格式相符的数据
(转)sscanf() - 从一个字符串中读进与指定格式相符的数据 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, stri ...
- linux 给用户添加进新的组
给用户user1添加一个新的组group1 usermod -G group1 #给当前登录用户所在组设置为 group1 注意:上面的命令有个问题需要知道,这个操作是重置用户所在组,也就是会让当前用 ...
随机推荐
- 关于RTP中的时间戳问题
关于RTP中的时间戳问题 时间戳单位:时间戳计算的单位不为秒之类的单位,而是由采样频率所代替的单位,这样做的目的就是为了是时间戳单位更为精准.比如说一个音频的采样频率为8000HZ,那么我们可以把时间 ...
- 【转】maven常见问题问答
转自:http://www.iteye.com/topic/973166 前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必 ...
- 算法笔记_097:蓝桥杯练习 算法提高 P1001(Java)
目录 1 问题描述 2 解决方案 1 问题描述 当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体来说,首先以字符串的形式输入两个整 ...
- VScode eslint-plugin-vue 自动修复eslint报错
1.安装插件 npm i -g eslint-plugin-vue 2.修改项目跟路径下的文件:.eslintrc | .eslint.js 3.添加eslint 和 vetur 插件 4.修改vsc ...
- Java学习笔记1、常用dos命令
cd 改变当前目录 sys 制作DOS系统盘 copy 拷贝文件 del 删除文件 deltree 删除目录树 dir 列文件名 diskcopy 制磁盘 edit 文本编辑 format ...
- sql查询备份或还原等操作的大概完成时间
查询出来的还需完成时间还算比较准确 --查询备份或还原等操作的大概完成时间 select command ,percent_complete ,running_time))+' hour, ' ))+ ...
- iOS 音频视频图像合成那点事
代码地址如下:http://www.demodashi.com/demo/13420.html 人而无信不知其可 前言 很久很久没有写点什么了,只因为最近事情太多了,这几天终于闲下来了,趁此机会,记录 ...
- Mysql报错......\xE6\x80\xBB\xE7\x9B\x91' for column...
Mysql添加表中字符报错:Incorrect string value: '\xE6\x80\xBB\xE7\x9B\x91' for column 'postName' at row 1 原因:字 ...
- Python-代码对象
可调用的对象是python执行环境中最重要的部分,python语句,赋值,表达式,模块等,这些 对象只是构成可执行代码块的拼图的很少的一部分,而这些代码块被称为代码对象. 每个可调用的对象的核心都 ...
- spring声明式事务以及配置
使用spring提供的事务处理机制的好处是程序员可以不用关心事务的切面了,只要配置就好了,可以少写代码. spring声明式事务处理 spring 声明:针对的是程序员,程序员告诉spring容器,哪 ...