C#Microsoft.Office.Interop.Word保护段落 [英] C# Microsoft.Office.Interop.Word protect the paragraph

查看:190
本文介绍了C#Microsoft.Office.Interop.Word保护段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Microsoft.Office.Interop.Word与C#一起使用.我知道您可以为特定段落设置受限的修改.

I'm using Microsoft.Office.Interop.Word with C#. I know that you can set modification restricted for a specific paragraph.

示例

  • 无法修改的段落1

您可以可以编辑

您无法修改

Paragraph3 you can’t modify

我在代码中遇到的问题是它保护了第一段,但是在用户可以修改另一段之后,请检查代码谢谢

The issue I am having in the code is it is protecting the first paragraph, but after that users are able to modify the other paragraph, please check the codes Thanks

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();}

这篇关于C#Microsoft.Office.Interop.Word保护段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆