开发者

Can you get docx xml off the Clipboard in C# (.NET 4.0)

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?


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);

                 }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜