Replacing Text on Document open using asp.net c# RRS feed

  • Question

  • User-1901014284 posted


    I am using an on click event to open Word documents within my asp.net C# application but I would also like to replace text within the Word Document when opened using a SessionData value which stores the company name from within my C# code. for example I would like the text within the header to be replaced with the company name as multiple companies will be accessing the same documents. The Word documents are stored within my database as a Varbinary(MAX) value. I am unsure how to do this or if it is even possible.

    Any assistance would be greatly appreciated.

    Many thanks


    Wednesday, July 24, 2019 2:10 PM

All replies

  • User288213138 posted

    Hi jonnygareth30,

    replace text within the Word Document when opened using a SessionData value which stores the company name from within my C# code

    According to your description, I couldn’t understand your requirement clearly.

    Do you mean you want to Replacing Text in word Document? If this is your requirement, I suggest you try to use Spire.Doc to Replace Text on Document.

    The code:

    using Spire.Doc;
    protected void Button1_Click(object sender, EventArgs e)
                Document doc = new Document();
                doc.Replace("1", "2", true, true);
                doc.SaveToFile(Server.MapPath("~/Doc/1.docx"), FileFormat.Docx);

    The resylt:

    Best regards,


    Thursday, July 25, 2019 6:29 AM
  • Thursday, July 25, 2019 12:26 PM
  • User-1901014284 posted


    Thank you very much for your responses and please accept my apologies for the delay in my response. I have been away from the project for a while.

    Is it possible to change text on multiple documents? (e.g. not specify it to 1 document name as below)


    For example when pulling the folder where my documents are stored it pulls multiple documents, please see below my code which populates a Gridview:

               if (!IsPostBack)
                    string[] filePaths = Directory.GetFiles(Server.MapPath("~/Documents/Templates/"));
                    List<ListItem> files = new List<ListItem>();
                    foreach (string filePath in filePaths)
                        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
                    GV.DataSource = files;

    Please see my code to download which runs when the "Download" button is clicked:

     protected void DownloadCrownfordForm(object sender, EventArgs e)
                string filePath = (sender as LinkButton).CommandArgument;
                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));

    Within the Word documents I have text as "Ccc" which is what i would like to replace with text populated within a textbox. Would this be possible?

    Many thanks in advance for any help, it is greatly appreciated.


    Monday, February 24, 2020 3:44 PM