zoukankan      html  css  js  c++  java
  • C#获取Word文档页数,并跳转到指定的页面获取页面信息

    using MSWord = Microsoft.Office.Interop.Word;

    private MSWord.Application wordApp;  //Word应用程序变量
    private MSWord.Document wordDoc;     //Word文档变量
    private Object Nothing = Missing.Value;
      //初始化
    wordApp = new MSWord.ApplicationClass();
    wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    // 打开Word
    object FileName = strPath;
    object readOnly = false;
    object isVisible = true;
    wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing);

    // 计算Word文档页数
    MSWord.WdStatistic stat = MSWord.WdStatistic.wdStatisticPages;
    int num = wordDoc.ComputeStatistics(stat, ref  Nothing);

    // 跳转到指定书签
    object BookMarkName = "BookMark1";
    object what = MSWord.WdGoToItem.wdGoToBookmark;
    wordDoc.ActiveWindow.Selection.GoTo(ref what, ref Nothing, ref Nothing, ref BookMarkName);
    MessageBox.Show( wordDoc.ActiveWindow.Selection.Paragraphs[1].Range.Text.ToString());


    //  跳转到指定页

    object What = MSWord.WdGoToItem.wdGoToPage;
    object Which = MSWord.WdGoToDirection.wdGoToNext;
    object Name = "1"; // 页数
    wordDoc.ActiveWindow.Selection.GoTo(ref What, ref Which, ref Nothing, ref Name); // 第二个参数可以用Nothing
    wordDoc.ActiveWindow.Selection.Paragraphs[1].Range.Text.ToString();

    //  以下是跳转到某个页面之后的操作

    // 通过PageSetup可以获取某个页面的信息

    // 获取页面的分栏
    wordDoc.ActiveWindow.Selection.PageSetup.TextColumn;

  • 相关阅读:
    CentOS Grub、BASH 故障、解决方法
    Fail2ban 阻止暴力破解
    Extundelete 数据恢复
    HTTP 状态信息
    LNMP 参数调优 ( 无注释 )
    Mysql 性能优化 ( my.cnf )
    MantisBT 缺陷管理系统
    Zabbix Agent 源码编译安装
    Zabbix 监控 Mysql 状态
    Zabbix 监控 Nginx 状态
  • 原文地址:https://www.cnblogs.com/CPFlying/p/1723724.html
Copyright © 2011-2022 走看看