简体   繁体   中英

Get the empty page count using iText Java

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

You will need to pass it through 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

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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