[英]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
=> _pdf是iText.Pdfa.PdfADocument對象。此方法將搜索內容為空(長度字節= 0)的所有頁面。 我將這些頁面(索引)存儲在一個列表中,並且我將瀏覽此列表以刪除每個頁面(帶有索引)。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.