如何在pdf文件中查找空白页
本文关键字:查找 空白 文件 pdf | 更新日期: 2023-09-27 18:36:50
>我无法检测到pdf文件中的空白页。我已经在网上搜索了它,但找不到一个好的解决方案。
使用Itextsharp,我尝试了页面大小Xobjects。但他们没有 给出确切的结果。
我试过了
if(xobjects==null || textcontent==null || size <20 bytes )
then "blank"
else
not blank
但最长时间它会返回错误的答案。我用过 Itextsharp
代码如下...我正在使用Itextsharp Librabry
对于 x对象
PdfDictionary xobjects = resourceDic.GetAsDict(PdfName.XOBJECT);
//here resourceDic is PdfDictionary type
//I know that if Xobjects is null then page is blank. But sometimes blank page gives xobjects which is not null.
对于内容流
RandomAccessFileOrArray f = reader.SafeFile;
//here reader = new PdfReader(filename);
byte[] contentBytes = reader.GetPageContent(pageNum, f);
//I have measured the size of contentbytes but sometimes it gives more than 20 bytes for blank page
对于文本内容
String extractedText = PdfTextExtractor.GetTextFromPage(reader, pageNum, new LocationTextExtractionStrategy());
// sometimes blank page give a text more than 20 char length .
发现空白页的一种非常简单的方法是:使用调用bbox
设备的 Ghostscript 命令行。
Ghostscript 的 bbox 计算最小矩形"边界框"的坐标,该边界框包含页面中将渲染像素的所有点:
gs '
-o /dev/null '
-sDEVICE=bbox '
input.pdf
在视窗上:
gswin32c.exe ^
-o nul ^
-sDEVICE=bbox ^
input.pdf
结果:
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 6.
Page 1
%%BoundingBox: 27 281 548 804
%%HiResBoundingBox: 27.000000 281.000000 547.332031 804.000000
Page 2
%%BoundingBox: 0 0 0 0
%%HiResBoundingBox: 0.000000 0.000000 0.000000 0.000000
Page 3
%%BoundingBox: 27 302 568 814
%%HiResBoundingBox: 27.949219 302.000000 567.332031 814.000000
Page 4
%%BoundingBox: 27 302 568 814
%%HiResBoundingBox: 27.949219 302.000000 567.332031 814.000000
Page 5
%%BoundingBox: 27 302 568 814
%%HiResBoundingBox: 27.949219 302.000000 567.332031 814.000000
Page 6
%%BoundingBox: 27 302 568 814
%%HiResBoundingBox: 27.949219 302.000000 567.332031 814.000000
如您所见,我的输入文档的第 2 页是空的。
我怀疑你已经尝试过.Trim() 在你的字符串上,所以我不会建议它自己
。空白中 20+ 字符长度字符串的实际内容是什么?我怀疑这只是换行符(就像人们按 Enter 10+ 次只是为了获得新页面而不是插入分页符时会发生什么),在这种情况下:
String extractedText =
string.Replace(string.Replace(
PdfTextExtractor.GetTextFromPage(reader, pageNum, new LocationTextExtractionStrategy())
, Environment.NewLine, ""), "'n", "").Trim();
在此之后让我们知道输出内容是什么。
另一种可能性是它是带有不间断空格和其他实际上不是空格的字符的空白文本,您需要手动查找并替换这些字符......此时,我建议您实际上只对 [0-9,a-z,A-Z] 使用正则表达式匹配并使用它来确定您的页面是否为空白。
有一个用于 C# 的包装库,并且来自mupdf c++ library
VB.NET。您可以使用它将页面转换为bmp
(以不同的格式tif
,jpg
,png
)并检查位图的大小。
大小,哪个是您将视为空白的页面的最小字符。