8

我无法检测到 pdf 文件中的空白页。我已经在互联网上搜索了它,但找不到一个好的解决方案。

使用 Itextsharp 我尝试了页面大小,Xobjects。但他们没有给出确切的结果。

我试过了

if(xobjects==null || textcontent==null || size <20 bytes )
  then "blank"
else
 not blank

但它返回错误答案的最长时间。我用过Itextsharp

代码如下...我正在使用Itextsharp

对于 xobjects

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 .
4

3 回答 3

2

A very simple way to discover empty pages is this: use a Ghostscript commandline that calls the bbox device.

Ghostscript's bbox calculates the coordinates of that minimum rectangle 'bounding box' which encloses all points of the page where a pixel would be rendered:

gs \
  -o /dev/null \
  -sDEVICE=bbox \
   input.pdf

On Windows:

gswin32c.exe ^
  -o nul ^
  -sDEVICE=bbox ^
   input.pdf

Result:

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

As you can see, page 2 of my input document was empty.

于 2012-06-30T14:09:57.387 回答
1

我怀疑你已经在你的字符串上尝试过 .Trim() ,所以我不会单独建议它。

空白中 20+ 字符长度字符串的实际内容是什么?我怀疑这只是换行符(就像人们按enter10 次以上只是为了获取新页面而不是插入分页符时会发生什么),在这种情况下:

String extractedText = 
    string.Replace(string.Replace(
        PdfTextExtractor.GetTextFromPage(reader, pageNum, new LocationTextExtractionStrategy())
    , Environment.NewLine, ""), "\n", "").Trim();

让我们知道这之后的输出内容是什么。

另一种可能性是它是带有不间断空格和其他实际上不是空格的字符的空白文本,您需要手动查找和替换这些字符。此时我建议您实际上只使用正则表达式匹配[0-9,az,AZ] 并使用它来确定您的页面是否为空白。

于 2012-06-10T13:24:22.813 回答
-1

有一个用于 C# 和 VB.NET 的包装库,来自mupdf c++ library. 您可以使用它将页面转换为bmp(以不同格式,,,tifjpgpng检查位图的大小。

您应该检查哪个是您将视为空白的页面的最小字符的最小尺寸。

于 2013-04-10T17:01:46.150 回答