c# - Document.Protect is not working -


hi have created 1 asp.net application, in converting asp:panel word document using below code snippet.

 stringwriter sw = new stringwriter();             htmltextwriter htw = new htmltextwriter(sw);              tblmain.rendercontrol(htw);              if (strtype.equals("save"))             {                 string filename = datetime.now.tostring().replace("/", "").replace("-", "").replace(":", "").replace(" ", "") + ".doc";                 string strpath = server.mappath("attachments") + "\\" + filename;                 streamwriter swriter = new streamwriter(strpath);                 swriter.write(sw.tostring());                 swriter.close(); 

now file saving specified folder. after fetching same file appending password protection. , used below code same.

microsoft.office.interop.word.applicationclass wordapp = new microsoft.office.interop.word.applicationclass();                 microsoft.office.interop.word.document doc = null;                 object missing = system.reflection.missing.value;                 object readonly = false;                 object visible = true;                 object password = "123";                 object filetoopen = strpath;                  try                 {                     doc = wordapp.documents.open(ref filetoopen, ref missing, ref readonly, ref missing, ref missing,                                                     ref missing, ref missing, ref password, ref missing, ref missing, ref missing,                                                     ref visible, ref visible, ref missing, ref missing, ref missing);                     doc.activate();                      doc.protect(wdprotectiontype.wdallowonlycomments,ref missing, ref password, ref missing, ref missing);                     doc.saveas(ref filetoopen, ref missing, ref missing, ref password, ref missing, ref password, ref missing, ref missing,                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);                  }                 catch (exception ex)                 {                     // exception catching                 }                                 {                     doc.close(ref missing, ref missing, ref missing);                     wordapp.quit(ref missing, ref missing, ref missing);                 } 

but save file along 1 folder named filename_files.

if remove line doc.protect(wdprotectiontype.wdallowonlycomments,ref missing, ref password, ref missing, ref missing);

it save doc file only, no password appended.

any idea? please me resolve issue. suggestions invited.

thanks in advance..

try this:

check whether has password or not

 if(doc.haspassword)      doc.protect(wdprotectiontype.wdallowonlyreading,ref missing, ref password, ref missing, ref missing);  else          doc.protect(wdprotectiontype.wdnoprotection,ref missing, ref password, ref missing, ref missing); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -