繁体   English   中英

使用iText Java获取空页数

[英]Get the empty page count using iText Java

我有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;
  }

------ 编辑 ------

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

这是链接: http : //www.rgagnon.com/javadetails/java-detect-and-remove-blank-page-in-pdf.html

我不使用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

=> _pdfiText.Pdfa.PdfADocument对象。此方法将搜索内容为空(长度字节= 0)的所有页面。 我将这些页面(索引)存储在一个列表中,并且我将浏览此列表以删除每个页面(带有索引)。

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM