Beispiel #1
0
        public void Protect()
        {
            //ExStart
            //ExFor:Document.Protect(ProtectionType)
            //ExFor:ProtectionType
            //ExFor:Section.ProtectedForForms
            //ExSummary:Protects a section so only editing in form fields is possible.
            // Create a blank document
            Aspose.Words.Document doc = new Aspose.Words.Document();

            // Insert two sections with some text
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Writeln("Section 1. Unprotected.");
            builder.InsertBreak(BreakType.SectionBreakContinuous);
            builder.Writeln("Section 2. Protected.");

            // Section protection only works when document protection is turned and only editing in form fields is allowed.
            doc.Protect(ProtectionType.AllowOnlyFormFields);

            // By default, all sections are protected, but we can selectively turn protection off.
            doc.Sections[0].ProtectedForForms = false;

            builder.Document.Save(MyDir + "Section.Protect Out.doc");
            //ExEnd
        }
Beispiel #2
0
        public void ProtectUnprotectDocument()
        {
            //ExStart
            //ExFor:Document.Protect(ProtectionType,String)
            //ExId:ProtectDocument
            //ExSummary:Shows how to protect a document.
            Aspose.Words.Document doc = new Aspose.Words.Document();
            doc.Protect(ProtectionType.AllowOnlyFormFields, "password");
            //ExEnd

            //ExStart
            //ExFor:Document.Unprotect
            //ExId:UnprotectDocument
            //ExSummary:Shows how to unprotect a document. Note that the password is not required.
            doc.Unprotect();
            //ExEnd

            //ExStart
            //ExFor:Document.Unprotect(String)
            //ExSummary:Shows how to unprotect a document using a password.
            doc.Unprotect("password");
            //ExEnd
        }
        private void BTN_Clean_Click(object sender, EventArgs e)
        {
            int    cleanedCount = 0;
            bool   passwordFlg  = false;
            bool   errorFlg     = false;
            string Message      = "";

            foreach (string ThisFile in InputFiles)
            {
                AsposeWords.FileFormatInfo info = AsposeWords.FileFormatUtil.DetectFileFormat(ThisFile);
                bool WordAttachment             = false;

                switch (info.LoadFormat)
                {
                case AsposeWords.LoadFormat.Doc:
                case AsposeWords.LoadFormat.Dot:
                case AsposeWords.LoadFormat.Docx:
                case AsposeWords.LoadFormat.Docm:
                case AsposeWords.LoadFormat.Dotx:
                case AsposeWords.LoadFormat.Dotm:
                case AsposeWords.LoadFormat.FlatOpc:
                case AsposeWords.LoadFormat.Rtf:
                case AsposeWords.LoadFormat.WordML:
                case AsposeWords.LoadFormat.Html:
                case AsposeWords.LoadFormat.Mhtml:
                case AsposeWords.LoadFormat.Odt:
                case AsposeWords.LoadFormat.Ott:
                case AsposeWords.LoadFormat.DocPreWord97:
                    WordAttachment = true;
                    break;

                default:
                    WordAttachment = false;
                    break;
                }

                // If word Attachment is found
                if (WordAttachment)
                {
                    try
                    {
                        AsposeWords.Document doc = new AsposeWords.Document(ThisFile);

                        // Remove if there is any protection on the document
                        AsposeWords.ProtectionType protection = doc.ProtectionType;
                        if (protection != AsposeWords.ProtectionType.NoProtection)
                        {
                            doc.Unprotect();
                        }

                        // Remove all built-in and Custom Properties
                        doc.CustomDocumentProperties.Clear();
                        doc.BuiltInDocumentProperties.Clear();

                        // Password will be removed if the document is password protected.
                        if (protection != AsposeWords.ProtectionType.NoProtection)
                        {
                            doc.Protect(protection);
                        }

                        // Save the file back to temp location
                        doc.Save(ThisFile);
                        cleanedCount++;
                    }
                    catch (Words.IncorrectPasswordException)
                    {
                        passwordFlg = true;
                        Message     = "Password protected files cannot be cleaned";
                    }
                    catch (Exception ex)
                    {
                        errorFlg = true;
                        Message  = "Error: " + ex.Message;
                    }
                }
                else
                {
                    Message = "Not a Word Document";
                }
            }
            if (passwordFlg)
            {
                LBL_Error.Text = Message;
            }
            if (errorFlg)
            {
                LBL_Error.Text = Message;
            }
            BTN_Clean.Enabled = false;
            LBL_Cleaned.Text  = cleanedCount.ToString();
        }
Beispiel #4
0
 public static void PrintWordEx(string targetFile, byte[] docBytes, DataSet dataSource, bool preview, PrintBySelf printByself, FormatColumnValue formatColumnValue, bool write)
 {
     BackgroundWorker worker = new BackgroundWorker();
     worker.DoWork += delegate (object sender, DoWorkEventArgs e) {
         WaitDialogHelper.Show();
         try
         {
             using (MemoryStream stream = new MemoryStream(docBytes))
             {
                 if (license == null)
                 {
                     license = new Aspose.Words.License();
                     license.SetLicense("Aspose.lic");
                 }
                 Aspose.Words.Document doc = new Aspose.Words.Document(stream);
                 InternalWrodReportExecute(doc, dataSource, printByself, formatColumnValue);
                 if (!write)
                 {
                     doc.Protect(ProtectionType.ReadOnly, "sbdwlfty");
                 }
                 if (preview)
                 {
                     doc.Save(targetFile);
                     Process.Start(targetFile);
                 }
                 else
                 {
                     new AsposeWordsPrintDocument(doc).Print();
                 }
             }
         }
         finally
         {
             WaitDialogHelper.Close();
         }
     };
     worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(PrintHelper.RunPrintWordWorkerCompleted);
     worker.RunWorkerAsync();
 }
Beispiel #5
0
 /// <summary>
 /// 不可编辑受保护,需输入密码
 /// </summary>
 /// <param name="pwd">密码</param>
 public void NoEdit(string pwd)
 {
     wordDoc.Protect(ProtectionType.ReadOnly, pwd);
 }
Beispiel #6
0
        public void Protect()
        {
            //ExStart
            //ExFor:Document.Protect(ProtectionType)
            //ExFor:ProtectionType
            //ExFor:Section.ProtectedForForms
            //ExSummary:Protects a section so only editing in form fields is possible.
            // Create a blank document
            Aspose.Words.Document doc = new Aspose.Words.Document();

            // Insert two sections with some text
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.Writeln("Section 1. Unprotected.");
            builder.InsertBreak(BreakType.SectionBreakContinuous);
            builder.Writeln("Section 2. Protected.");

            // Section protection only works when document protection is turned and only editing in form fields is allowed.
            doc.Protect(ProtectionType.AllowOnlyFormFields);

            // By default, all sections are protected, but we can selectively turn protection off.
            doc.Sections[0].ProtectedForForms = false;

            builder.Document.Save(ExDir + "Section.Protect Out.doc");
            //ExEnd
        }
        private void BTN_Clean_Click(object sender, EventArgs e)
        {
            int cleanedCount = 0;
            bool passwordFlg = false;
            bool errorFlg = false;
            string Message = "";
            foreach (string ThisFile in InputFiles)
            {
                AsposeWords.FileFormatInfo info = AsposeWords.FileFormatUtil.DetectFileFormat(ThisFile);
                bool WordAttachment = false;

                switch (info.LoadFormat)
                {
                    case AsposeWords.LoadFormat.Doc:
                    case AsposeWords.LoadFormat.Dot:
                    case AsposeWords.LoadFormat.Docx:
                    case AsposeWords.LoadFormat.Docm:
                    case AsposeWords.LoadFormat.Dotx:
                    case AsposeWords.LoadFormat.Dotm:
                    case AsposeWords.LoadFormat.FlatOpc:
                    case AsposeWords.LoadFormat.Rtf:
                    case AsposeWords.LoadFormat.WordML:
                    case AsposeWords.LoadFormat.Html:
                    case AsposeWords.LoadFormat.Mhtml:
                    case AsposeWords.LoadFormat.Odt:
                    case AsposeWords.LoadFormat.Ott:
                    case AsposeWords.LoadFormat.DocPreWord97:
                        WordAttachment = true;
                        break;
                    default:
                        WordAttachment = false;
                        break;
                }

                // If word Attachment is found
                if (WordAttachment)
                {
                    try
                    {
                        AsposeWords.Document doc = new AsposeWords.Document(ThisFile);

                        // Remove if there is any protection on the document
                        AsposeWords.ProtectionType protection = doc.ProtectionType;
                        if (protection != AsposeWords.ProtectionType.NoProtection)
                            doc.Unprotect();

                        // Remove all built-in and Custom Properties
                        doc.CustomDocumentProperties.Clear();
                        doc.BuiltInDocumentProperties.Clear();

                        // Password will be removed if the document is password protected.
                        if (protection != AsposeWords.ProtectionType.NoProtection)
                            doc.Protect(protection);

                        // Save the file back to temp location
                        doc.Save(ThisFile);
                        cleanedCount++;
                    }
                    catch (Words.IncorrectPasswordException)
                    {
                        passwordFlg = true;
                        Message = "Password protected files cannot be cleaned";
                    }
                    catch (Exception ex)
                    {
                        errorFlg = true;
                        Message = "Error: " + ex.Message;
                    }
                }
                else
                    Message = "Not a Word Document";
            }
            if (passwordFlg)
                LBL_Error.Text = Message;
            if (errorFlg)
                LBL_Error.Text = Message;
            BTN_Clean.Enabled = false;
            LBL_Cleaned.Text = cleanedCount.ToString();
        }