如何在pdf文件中找到空白页面

Md *_*ker 8 .net c# pdf itextsharp pdf-parsing

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

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

我试过了

if(xobjects==null || textcontent==null || size <20 bytes )
  then "blank"
else
 not blank
Run Code Online (Sandbox Code Playgroud)

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

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

对于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.
Run Code Online (Sandbox Code Playgroud)

对于contentstream

 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
Run Code Online (Sandbox Code Playgroud)

对于textcontent

String extractedText = PdfTextExtractor.GetTextFromPage(reader, pageNum, new LocationTextExtractionStrategy());
  // sometimes blank page give a text more than 20 char length .
Run Code Online (Sandbox Code Playgroud)

Sep*_*eph 1

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

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

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

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

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