Copy Document To Clipboard

Is there any method where I can get the contents of a word or rtf document and copy to the clipboard using Aspose.Words?

Hi

Thanks for your inquiry. You can try using code like the following to copy document’s content to clipboard:

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Aspose.Words;

namespace ConsoleApplication1
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Open document, which text should be copied.
            Document doc = new Document(@"C:\Temp\in.doc");
            // Save document in RTF format into stream.
            using (MemoryStream rtfStream = new MemoryStream())
            {
                doc.Save(rtfStream, SaveFormat.Rtf);
                // Get RTF string from the stream.
                string rtfString = Encoding.UTF8.GetString(rtfStream.GetBuffer());
                // Set clipboard text.
                Clipboard.SetText(rtfString, TextDataFormat.Rtf);
            }
        }
    }
}

Hope this helps.
Best regards.

Thanks! This was exactly what I needed. Worked perfectly!

Is something like this (copying word doc to clipboard) available in Aspose words for Java,. If yes, please provide sample code.
thanks
Neeraj

You can use following code to get RTF representation of Word document:

Document doc = new Document("D:\\Temp\\input.docx");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.save(baos, SaveFormat.RTF);

String rtf = new String( baos.toByteArray(),  java.nio.charset.StandardCharsets.UTF_8 );

The following code copies the text into clipboard. But, you may use standard Java APIs to copy in RTF format.

StringSelection selection = new StringSelection(rtf);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);