Create ReadOnly word document in Windows Application

Last Reply 5 months ago By dharmendr

Posted 5 months ago

Hi! I used below code its worked. I want when I run copydoc with programm then its mode must be readonly. Because anybody can't change it.

LastName:LN

Name:N

Birthday:Bth

Address:Ad

I want after open copydoc from c# anybody can't change text word file. For that I want protect text like pdf format.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace MSWord
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            object fileName = @"C:\documment.doc";
            Word.Application word = new Word.Application();
            Word.Document doc = new Word.Document();
            object missing = System.Type.Missing;
            try
            {
                doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                doc.Activate();
                foreach (Microsoft.Office.Interop.Word.Range docRange in doc.Words)
                {
                    if (docRange.Text.Trim() == "LN")
                    {
                        docRange.Text = docRange.Text.Replace("LN", " Pulodov");
                    }
                    else if (docRange.Text.Trim() == "N")
                    {
                        docRange.Text = docRange.Text.Replace("N", " Rustam");
                    }
                    else if (docRange.Text.Trim() == "Bth")
                    {
                        docRange.Text = docRange.Text.Replace("Bth", " 22.12.1987");
                    }
                    else if (docRange.Text.Trim() == "Ad")
                    {
                        docRange.Text = docRange.Text.Replace("Ad", " city Dushanbe, district Sino");
                    }    
                }
                string path = @"C:\\";

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                File.Delete(path + "copydoc.doc");
                doc.SaveAs(@"C:\copydoc.doc", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                doc.Close(ref missing, ref missing, ref missing);
                ((Word._Application)word).Quit();
            }

            Process p = new Process();
            p.StartInfo.FileName = @"C:\copydoc.doc";
            p.Start();
        }
    }
}

output result by readonly mode:

LastName: Pulodov

Name: Rustam

Birthday: 22.12.1987

Address: city Dushanbe, district Sino

Posted 5 months ago Modified on 5 months ago

This is not possible. Then you need to convert the word doc to pdf. So that no body change that.

To convert word document to pdf refer below link.

Convert Microsoft Office Document (Word) to PDF using C# and VB.Net in ASP.Net