簡體   English   中英

C#Microsoft.Office.Interop.Word保護段落

[英]C# Microsoft.Office.Interop.Word protect the paragraph

我在C#中使用Microsoft.Office.Interop.Word。 我知道您可以為特定段落設置限制修改。

  • 第1段你無法修改

  • 可以編輯第2段

  • 不能修改第3段

我在代碼中遇到的問題是它是保護第一段,但之后用戶可以修改其他段落,請檢查代碼謝謝

public string ExtractText()
    {


        var app = new Microsoft.Office.Interop.Word.Application();


        Document document = app.Documents.Open(@"C:\Test\656.docx");


        String read = string.Empty;
        List<string> data = new List<string>();
        for (int i = 0; i < document.Paragraphs.Count; i++)
        {
            string temp = document.Paragraphs[i + 1].Range.Text.Trim();
            if (temp != string.Empty)
                data.Add(temp);
        }
        data.Add("Next LINE");
        data.Add("Second method opens the existing Microsoft Office Word document specified by a fully qualified path and file name. This method returns a Document that represents the opened document");
        data.Add("Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney C");

        object missing = System.Reflection.Missing.Value;

       app.ActiveDocument.Content.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEveryone);


        foreach (var item in data)
        {

         var para = document.Content.Paragraphs.Add(ref missing);
          para.Range.Text = item.Trim();
          para.Range.InsertParagraphAfter();


            if (para.Range.Text.Contains("Second"))
            {

                 para = document.Content.Paragraphs.Add(ref missing);
                para.Range.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEditors).Delete();
            }

            if (para.Range.Text.Contains("Contrary"))
            {

                para = document.Content.Paragraphs.Add(ref missing);
                //   var para1 = document.Content.Paragraphs[1].Range.Paragraphs.Add(ref missing);
                para.Range.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEveryone).Delete();
            }
            else
            {

                para = document.Content.Paragraphs.Add(ref missing);

                para.Range.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEveryone);
            }


        }

        app.Visible = true;

        object noReset = true;
        object password = System.String.Empty;
        object useIRM = false;
        object enforceStyleLock = true;

        object PasswordEncryptionFileProperties = false;
        app.ActiveDocument.EnforceStyle = true;

        document.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, ref noReset, "000", ref useIRM, ref enforceStyleLock);

        document.Save();
        document.Close();
    public string ExtractText()
    {

        var app = new Microsoft.Office.Interop.Word.Application();


        Document document = app.Documents.Open(@"C:\Test\90.docx");


    //    app.ActiveDocument.Protect.p

        String read = string.Empty;
        List<string> data = new List<string>();
        for (int i = 0; i < document.Paragraphs.Count; i++)
        {
            string temp = document.Paragraphs[i + 1].Range.Text.Trim();
            if (temp != string.Empty)
                data.Add(temp);
        }
        data.Add("Next LINE");
        data.Add("Next LINE");
        data.Add("Second method opens the existing Microsoft Office Word document specified by a fully qualified path and file name. This method returns a Document that represents the opened document");
        data.Add("Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney C");

        object missing = System.Reflection.Missing.Value;

   app.ActiveDocument.Content.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEveryone);


        foreach (var item in data)
        {

         var para = document.Content.Paragraphs.Add(ref missing);
          para.Range.Text = item.Trim();
          para.Range.InsertParagraphAfter();






     app.Visible = true;

        object noReset = true;
        object password = System.String.Empty;
        object useIRM = false;
        object enforceStyleLock = true;

        object PasswordEncryptionFileProperties = false;
        app.ActiveDocument.EnforceStyle = true;

        document.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, ref noReset, "000", ref useIRM, ref enforceStyleLock);
        document.Paragraphs[1].Range.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEditors);
        document.Paragraphs[2 + 1].Range.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEveryone);
        document.Paragraphs[1 + 1].Range.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEditors);




        document.Save();
        document.Close();}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM