你能在 C# (.NET 4.0) 中从剪贴板获取 docx xml

发布于 2024-10-10 17:59:01 字数 220 浏览 5 评论 0原文

使用以下命令可以轻松地从剪贴板中获取 Html、rtf、UnicodeText 和图像等内容:

Clipboard.GetText(TextDataFormat.Html);或 Clipboard.GetImage();或者你有什么。

有没有办法从剪贴板中获取 docx xml(查看保存的 docx 文件时得到的那种)? Word 文档中的复制/剪切是否会将 docx xml 放入剪贴板?

It's easy to get things like Html, rtf, UnicodeText, and images off of the clipboard using this:

Clipboard.GetText(TextDataFormat.Html); or Clipboard.GetImage(); or what have you.

Is there a way to get docx xml off of the clipboard (the kind that you get if you look at a saved docx file)? Does a copy/cut in a word document even put docx xml on the clipboard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-10-17 17:59:01

您必须采取解决方法,获取 Word docx 文件的文件名,然后读取它以获取其后面的 XML,然后将其粘贴到剪贴板。

当你在那里时,你可以

  1. 使用 http://docx.codeplex.com/ 操作 docx
  2. 将 HTML 放在剪贴板上 http://cathalscorner.blogspot.ca/2009/10 /converting-docx-into-doc-pdf-html.html
  3. 获取文档 XML 部分 - https://msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx
  4. 如何获取 Word 文档的纯文本使用 Open XML (CSOpenXmlGetPlainText) - https://code.msdn.microsoft.com/office/ CSOpenXmlGetPlainText-554918c3

    public static string ToStringWithDeclaration(this XDocument doc)
    {
        if (doc == null)
        {
            throw new ArgumentNullException("doc");
        }
        StringBuilder builder = new StringBuilder();
        using (TextWriter writer = new StringWriter(builder))
        {
            doc.Save(writer);
        }
        return builder.ToString();
    }

    public static void SetFileDropList(StringCollection filePaths) {

        IDataObject data = Clipboard.GetDataObject();
        if (!data.GetDataPresent(DataFormats.FileDrop))
        return;

        string[] filePaths = (string[])data.GetData(DataFormats.FileDrop);
        foreach (string fileLoc in filePaths) {

            if (Path.GetExtension(fileLoc)==".docx"){

                  if (File.Exists(c)) {

                        Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                        Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
                        XDocument xDoc = XDocument.Load(fileLoc);

                       Clipboard.SetText(xDoc.ToStringWithDeclaration(), TextDataFormat.Html);

                 }
            }
        }

You have to do a workaround by grabbing the file name of the Word docx file and then reading it to get the XML behind it, then pasting it to clipboard.

While your there you can

  1. manipulate docx using http://docx.codeplex.com/
  2. put HTML on clipboard http://cathalscorner.blogspot.ca/2009/10/converting-docx-into-doc-pdf-html.html
  3. Get doc XML part - https://msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx
  4. How to get Plain Text of a Word Document using Open XML (CSOpenXmlGetPlainText) - https://code.msdn.microsoft.com/office/CSOpenXmlGetPlainText-554918c3

.

    public static string ToStringWithDeclaration(this XDocument doc)
    {
        if (doc == null)
        {
            throw new ArgumentNullException("doc");
        }
        StringBuilder builder = new StringBuilder();
        using (TextWriter writer = new StringWriter(builder))
        {
            doc.Save(writer);
        }
        return builder.ToString();
    }

    public static void SetFileDropList(StringCollection filePaths) {

        IDataObject data = Clipboard.GetDataObject();
        if (!data.GetDataPresent(DataFormats.FileDrop))
        return;

        string[] filePaths = (string[])data.GetData(DataFormats.FileDrop);
        foreach (string fileLoc in filePaths) {

            if (Path.GetExtension(fileLoc)==".docx"){

                  if (File.Exists(c)) {

                        Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                        Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
                        XDocument xDoc = XDocument.Load(fileLoc);

                       Clipboard.SetText(xDoc.ToStringWithDeclaration(), TextDataFormat.Html);

                 }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文