使用正则表达式的PDF页数 [英] PDF page count using regex

查看:132
本文介绍了使用正则表达式的PDF页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用正则表达式来计算pdf的页数.下面是我使用的代码.

I used regex to calculate page count for pdf. Below is the code that i used.

Regex regex = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = regex.Matches(sr.ReadToEnd());
return matches.Count;

它在1.6以下的版本中可以正常使用,但不能在1.6版以下的pdf文件中使用.如果pdf版本为1.6,则返回0页.

It works fine with the version below 1.6 but not working with pdf files with version 1.6 . It returns 0 page if pdf version is 1.6 .

推荐答案

在您的情况下,您很可能必须处理1.6文档,这些文档利用了压缩对象流随后引入的功能.由于在此类文档中,您搜索的信息已压缩,因此您的正则表达式找不到它.

In your case you most likely have to do with 1.6 documents which make use of the then introduced feature of compressed object streams. As in such documents the information you search for is compressed, your regular expression does not find it.

有些工具可让您在搜索文件之前先解压缩文件中的此类流.不过,在寻找它们之前,请注意,无论如何,代码的结果还是不能被信任的,因为

There are tools which allow you to decompress such streams in a file before searching it. Before you look for them, though, be aware that the result of your code cannot be trusted anyway as

  • 匹配项可能比页面更多,因为文件中可能有旧的,未使用的页面对象,甚至其他错误的肯定结果,
  • 匹配项少于页面,因为PDF允许使用其他方式来写那些类型的条目.

这篇关于使用正则表达式的PDF页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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