Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用iText Java获取空页计数_Java_Itext - Fatal编程技术网

使用iText Java获取空页计数

使用iText Java获取空页计数,java,itext,Java,Itext,我有800页的PDF文件,我想计算有多少页是空白的,在这800页中,您需要通过java.io.RandomAccessFile传递它。它看起来与此类似: int efficientPDFPageCount(File file) { RandomAccessFile raf = new RandomAccessFile(file, "r"); RandomAccessFileOrArray pdfFile = new RandomAccessFileOrArray(

我有800页的PDF文件,我想计算有多少页是空白的,在这800页中,您需要通过java.io.RandomAccessFile传递它。它看起来与此类似:

int efficientPDFPageCount(File file) {
     RandomAccessFile raf = new RandomAccessFile(file, "r");
     RandomAccessFileOrArray pdfFile = new RandomAccessFileOrArray(
          new RandomAccessSourceFactory().createSource(raf));
     PdfReader reader = new PdfReader(pdfFile, new byte[0]);
     int pages = reader.getNumberOfPages();
     reader.close();
     return pages;
  }
------编辑------

这不是我的代码,但我发现这是关于如何检测和删除空白页


下面是链接:

我不使用Java,但在VB.Net中,以下代码可以工作,我认为您只需要获得等效的方法并转换代码:

    Private Sub DeleteEmptyPage()
        Dim lstIdxEmptyPage As New List(Of Integer)

        For iPage As Integer = 1 To _pdf.GetNumberOfPages
            Dim page As PdfPage = _pdf.GetPage(iPage)
            Dim pageContent As Byte() = page.GetContentBytes()

            If pageContent.Length > 0 Then
                Continue For
            Else
                lstIdxEmptyPage.Add(iPage)
            End If
        Next

        For Each idxPage As Integer In lstIdxEmptyPage
            Me._pdf.RemovePage(idxPage)
        Next
    End Sub
=>\u pdf是一个iText.Pdfa.PdfADocument对象 此方法将搜索包含空内容(长度字节=0)的所有页面。我将这些页面(索引)存储在一个列表中,我将浏览此列表以删除每个页面(带有索引)


我希望这能有所帮助。

我有一个非常简单的算法来实现这一点,但不幸的是,它不适合这个……你的问题是,空白页并不总是空白的。在PDF查看器中查看时,对人眼来说可能是空白的内容,对于读取PDF文件中语法的软件来说可能不是空白的。你必须指定你所说的“空白页”是什么意思,而这个“空白页的定义”在不同的文件中可能有所不同。我不会投反对票,因为这不是一个坏问题,只是一个很可能无法回答的问题。这并不意味着这是一个非法的问题。这会告诉你文档中有多少页,而不是其中有多少页是空白的。这可能比期望的更严格:op很可能希望找到空白的页。显示为空白的页面可能有一个非空的内容流。例如,pdf创建者通常通过选择字体或创建剪辑路径来准备每页,甚至页面最终也会留空。