I want code to copy word text content into texbox using c# code in web application

right now i have code for windows application.

using Microsoft.Office.Interop.Word;

private void readFileContent(string path)
    {
        Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
        object file = path;
        object nullobj = System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
            ref file, ref nullobj, ref nullobj,
            ref nullobj, ref nullobj, ref nullobj,
            ref nullobj, ref nullobj, ref nullobj,
            ref nullobj, ref nullobj, ref nullobj);
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        IDataObject data = Clipboard.GetDataObject();
       textbox1.Text = data.GetData(DataFormats.Text).ToString();
        doc.Close(ref nullobj, ref nullobj, ref nullobj);
        wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);

    }

This code is working in windows application....Clipbord is not there in web application....
so how to do in web application

Dani AI

Generated

Good direction from and a useful reality check from . For a web app, skip automating Word and anything clipboard-related. Office automation relies on desktop COM and is brittle in IIS. The scalable pattern is: let the user upload the document, extract its text on the server, then put that text into your textbox.

If you can require .docx, use the Open XML SDK (no Office needed). Example reads a posted file stream and preserves basic paragraph breaks:

using System;
using System.IO;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

// code-behind (e.g., Button click)
if (FileUpload1.HasFile)
{
    using (var stream = FileUpload1.PostedFile.InputStream)
    using (var doc = WordprocessingDocument.Open(stream, false))
    {
        var body = doc.MainDocumentPart.Document.Body;
        var text = string.Join(Environment.NewLine,
            body.Elements<Paragraph>().Select(p => p.InnerText));
        TextBox1.TextMode = TextBoxMode.MultiLine;
        TextBox1.Text = text;
    }
}

Notes:

  • For legacy .doc files, either convert to .docx client-side before upload or use a server-safe library that can read .doc. Avoid spinning up Word on the server.
  • Client-side paste is not reliable to automate. Modern browsers only allow paste via explicit user action; you cannot arbitrarily read the clipboard and shove it into a textbox. If users already have the text, instruct them to paste manually; otherwise, accept a file upload.
  • Always validate file type/size, handle very large docs defensively, and wrap parsing in try/catch so a bad file does not take down the request.

Recommended Answers

All 3 Replies

Don't use Clipboard object. Read content of doc using,

string txt=doc.Content.Text;

Do you want to this operation in client side or server side?

Since it is a web application, you can't use the above C# code at client side. It can be done using JavaScript only.

You can try to use object.execCommand javascript method to paste the text from clipboard to the current object such as TextBox.

For example,

function PasteTextFromClipboard()
        {
            document.getElementById('textArea1').focus();
            var strText = document.getElementById('textArea1').createTextRange();
            strText.execCommand("Paste");
        }

In case if you want to do this operation in server side, check the following article. The C# code uploads a word document file and stores it into a string and from that it is placed that string into a textbox.

Don't use Clipboard object. Read content of doc using,

string txt=doc.Content.Text;

Thanks it helped me...U r right

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.