/// <summary> /// Create a read only document that can be edited by entering a valid password. /// </summary> public static void AddPasswordProtection() { Console.WriteLine("\tAddPasswordProtection()"); // Create a new document. using (DocX document = DocX.Create(ProtectionSample.ProtectionSampleOutputDirectory + @"AddPasswordProtection.docx")) { // Add a title document.InsertParagraph("Document protection using password").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center; // Insert a Paragraph into this document. var p = document.InsertParagraph(); // Append some text and add formatting. p.Append("This document is protected and can only be edited by stopping its protection with a valid password(\"xceed\").") .Font(new Font("Arial")) .FontSize(25) .Color(Color.Blue) .Bold(); // Set the document as read only and add a password to unlock it. document.AddPasswordProtection(EditRestrictions.readOnly, "xceed"); // Save this document to disk. document.Save(); Console.WriteLine("\tCreated: AddPasswordProtection.docx\n"); } }
public void DocProtect(DocX document, Boolean editProtect, string password) { if (editProtect) { //document.RemoveProtection(); document.AddPasswordProtection(EditRestrictions.readOnly, password); } }