123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Microsoft.Office.Interop.Word;
- using Microsoft.Web.WebView2.WinForms;
- using Microsoft.Web.WebView2.Wpf;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace HTEXWordTest
- {
- public partial class Form1 : Form
- {
-
- public Form1()
- {
-
- InitializeComponent();
- this.Load+=Form1_Load;
- }
- private async void Form1_Load(object sender, EventArgs e)
- {
-
- //在加载窗体时初始化 WebView2
- await webView21.EnsureCoreWebView2Async();
- // 注册自定义对象以供 JavaScript 使用
- // webView21.CoreWebView2.NavigateToString("<html><head><meta charset=\"utf-8\"><title>菜鸟教程(runoob.com)</title></head><body> <h1>我的第一个标题</h1> <p>我的第一个段落。</p></body></html>");
- // 加载网页
- // webView2.Source = new Uri("https://www.baidu.com");
- webView21.CoreWebView2.Navigate("https://cn.vuejs.org/");
- }
- private void webView21_Click(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- this.Invoke(new Action(() =>
- {
- this.webView21.CoreWebView2.ExecuteScriptAsync("document.execCommand('selectAll', false, null);");
- }));
- }
- private async void button2_Click(object sender, EventArgs e)
- {
- //var sting = Clipboard.GetText();
- //string htmlContent = await webView21.ExecuteScriptAsync("document.documentElement.outerHTML");
- //string html = @"<html><head><meta charset=""utf-8""><title>菜鸟教程(runoob.com)</title></head><body> <h1>我的第一个标题</h1> <p>我的第一个段落。</p></body></html>";
- ////var html = Rtf.ToHtml(htmlContent);
- //// 将 HTML 内容放入剪贴板
- ////Clipboard.SetText(sting, TextDataFormat.Rtf);
- //Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Paste();
- // 获取剪贴板中的数据
- IDataObject dataObject = Clipboard.GetDataObject();
- // 检查是否有图像数据
- if (dataObject != null && dataObject.GetDataPresent(DataFormats.Bitmap))
- {
- // 如果有图像数据,粘贴图像
- Bitmap bitmap = (Bitmap)dataObject.GetData(DataFormats.Bitmap);
- Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.InlineShapes.AddPicture("", false, true).Range.PasteSpecial(DataType: WdPasteDataType.wdPasteBitmap);
- }
- else
- {
- // 如果没有图像数据,可以尝试其他格式的粘贴
- Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Paste();
- }
- // Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Range.PasteSpecial(DataType: WdPasteDataType.wdPasteRTF);
- }
- private async void button3_Click(object sender, EventArgs e)
- {
- // 使用 JavaScript 选择整个网页内容
- await webView21.ExecuteScriptAsync("document.execCommand('selectall')");
- // 使用 JavaScript 复制选定的内容到剪贴板
- await webView21.ExecuteScriptAsync("document.execCommand('copy')");
- IDataObject dataObject = Clipboard.GetDataObject();
- // 检查是否有图像数据
- if (dataObject != null && dataObject.GetDataPresent(DataFormats.Bitmap))
- {
- // 如果有图像数据,粘贴图像
- Bitmap bitmap = (Bitmap)dataObject.GetData(DataFormats.Bitmap);
- Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.InlineShapes.AddPicture("", false, true).Range.PasteSpecial(DataType: WdPasteDataType.wdPasteBitmap);
- }
- else
- {
- // 如果没有图像数据,可以尝试其他格式的粘贴
- Globals.ThisAddIn.Application.ActiveDocument.Application.Selection.Paste();
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- }
- }
- }
|