简体   繁体   English

使用iText Java获取空页数

[英]Get the empty page count using iText Java

我有800页的PDF文件,我想统计一下这800页中有多少页为空白

You will need to pass it through java.io.RandomAccessFile. 您将需要通过java.io.RandomAccessFile传递它。 It will look similar to this: 它看起来类似于:

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

------ EDIT ------ ------ 编辑 ------

This is not my code, but I found this on how to detect and delete blank pages. 这不是我的代码,但是我发现了如何检测和删除空白页。

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

I'm not working with Java but in VB.Net, the following code works, I think you just need to get the equivalent methods and convert the code : 我不使用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

=> _pdf is a iText.Pdfa.PdfADocument object This method will search all pages with empty content (Lenght Bytes = 0). => _pdfiText.Pdfa.PdfADocument对象。此方法将搜索内容为空(长度字节= 0)的所有页面。 I stored these pages (index) in a list and I will browse this list to remove each page (with it index). 我将这些页面(索引)存储在一个列表中,并且我将浏览此列表以删除每个页面(带有索引)。

I hope this help. 希望对您有所帮助。

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

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