首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C# Microsoft.Office.Interop.Word保护段落

C# Microsoft.Office.Interop.Word保护段落
EN

Stack Overflow用户
提问于 2019-05-24 21:53:32
回答 1查看 646关注 0票数 0

我在C#中使用Microsoft.Office.Interop.Word。我知道你可以为特定的段落设置修改限制。

示例

  • Paragraph1 you can‘t modify
  • Paragraph2 you can you’t modify

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

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-24 22:23:17

代码语言:javascript
复制
    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();}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56294094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档