获取Word的页数

/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2013/9/25
 * Time: 22:05
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using Novacode;

namespace DocX01
{
     class Program
    {
        
         private  static Microsoft.Office.Interop.Word.Application myWordApp =  new Microsoft.Office.Interop.Word. ApplicationClass();
         static  object oMissing = System.Reflection.Missing.Value;
        
         public  static  void  Main( string[] args)
        {
            Console. WriteLine( "Hello World!");
            
             int i= GetWordPageCount( @"d:\Spring.docx");
            Console. WriteLine(i);
            
            Console. ReadKey( true);
        }
        
          public  static  int  GetWordPageCount( string path)
        {
             try
            {
                myWordApp.Visible =  false;
                 object filePath = path;  //这里是Word文件的路径
                 //打开文件
                Microsoft.Office.Interop.Word.Document myWordDoc = myWordApp.Documents. Open(
                     ref filePath,  ref oMissing,  ref oMissing,  ref oMissing,
                     ref oMissing,  ref oMissing,  ref oMissing,  ref oMissing,
                     ref oMissing,  ref oMissing,  ref oMissing,  ref oMissing,
                     ref oMissing,  ref oMissing,  ref oMissing,  ref oMissing);
                 //下面是取得打开文件的页数
                 int pages = myWordDoc. ComputeStatistics(WdStatistic.wdStatisticPages,  ref  oMissing);
                myWordDoc. Close( ref oMissing,  ref oMissing,  ref oMissing);
                 return pages;
            }
             catch(Exception ex) 
            {
                 return  0;
            }
        }
       
    }
        
        
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uniapp中获取word文档的页数需要使用js库Pizzip.js和JSZip-utils.js来读取word文档,然后解析xml文件来计算页数。 以下是获取word文档页数的代码示例: ```javascript // 导入Pizzip.js和JSZip-utils.js import PizZip from 'pizzip'; import { JSZipUtils } from 'jszip-utils'; // 获取word文档页数 function getWordPageCount(url) { return new Promise((resolve, reject) => { JSZipUtils.getBinaryContent(url, function(err, content) { if (err) { reject(err); return; } const zip = new PizZip(content); const docXml = zip.file('word/document.xml').asText(); // 获取word文档的xml文件 const pageNodes = docXml.match(/<w:pgSz[\s\S]*?\/>/g); // 匹配所有的页面设置节点 const pageSize = pageNodes[0].match(/w:w="(\d+)" w:h="(\d+)"/); // 获取页面宽度和高度 const pageWidth = parseInt(pageSize[1]); const pageHeight = parseInt(pageSize[2]); const contentWidth = pageWidth - 1440; // 减去页边距 const contentHeight = pageHeight - 1440; const pages = docXml.match(/<w:p\s/g); // 匹配所有的段落节点 const pageCount = Math.ceil(pages.length * contentHeight / pageHeight); resolve(pageCount); }); }); } ``` 使用示例: ```javascript getWordPageCount('http://example.com/example.docx').then(pageCount => { console.log('页数:', pageCount); }).catch(error => { console.error('获取页数失败:', error); }); ``` 需要注意的是,由于需要读取word文档的xml文件,因此需要将word文档放在服务器上,然后通过url来获取。如果需要处理本地的word文档,则需要先将其上传到服务器上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值