翻譯|使用教程|編輯:張瑩心|2021-11-19 13:29:46.743|閱讀 836 次
概述:Spire.PDF for Java提供了一種方法PdfPageBase.isBlank()來檢測(cè) PDF 頁面是否絕對(duì)為空。但有時(shí),PDF 頁面可能包含看起來空白的掃描白色圖像。本文將從以下兩部分演示如何去除空的PDF頁面。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Spire.PDF for Java 是一款專門對(duì) PDF 文檔進(jìn)行操作的 Java 類庫。該類庫的主要功能在于幫助開發(fā)人員在 Java 應(yīng)用程序(J2SE和J2EE)中生成 PDF 文檔和操作現(xiàn)有 PDF 文檔,并且運(yùn)行環(huán)境無需安裝 Adobe Acrobat。
Spire.PDF for Java提供了一種方法PdfPageBase.isBlank()來檢測(cè) PDF 頁面是否絕對(duì)為空。但有時(shí),PDF 頁面可能包含看起來空白的掃描白色圖像。本文將從以下兩部分演示如何去除空的PDF頁面。
點(diǎn)擊下載Spire.PDF for Java最新試用版
import com.spire.pdf.*; public class removeBlankPages { public static void main(String[] args) throws Exception { //Create an object of PdfDocument class. PdfDocument pdf = new PdfDocument(); //Load the file from disk pdf.loadFromFile("Sample.pdf"); //Traverse all the pages for (int i = pdf.getPages().getCount() - 1; i >= 0; i--) //detect if a page is blank if (pdf.getPages().get(i).isBlank()) { //Remove blank page pdf.getPages().removeAt(i); } //Save the pdf document pdf.saveToFile("Removeblankpages.pdf"); } }
從 PDF 中刪除包含白色圖像的頁面
以下代碼片段將演示如何刪除帶有白色圖像的 PDF 頁面。首先,您需要申請(qǐng) 30 天的試用許可證,以刪除轉(zhuǎn)換圖像中的評(píng)估消息。否則,此方法將無法正常工作。
import com.spire.pdf.*; import java.awt.*; import java.awt.image.*; import static com.spire.pdf.graphics.PdfImageType.Bitmap; public class removeBlankPages2 { public static void main(String[] args) throws Exception { //Register the license key com.spire.license.LicenseProvider.setLicenseKey("your license key"); //Create an object of PdfDocument class. PdfDocument pdf = new PdfDocument(); //Load the file from disk pdf.loadFromFile("Sample2.pdf"); //Traverse all the pages for (int i = pdf.getPages().getCount() - 1; i >= 0; i--) { //Convert the PDF to images BufferedImage image = pdf.saveAsImage(i, Bitmap); //Determine whether a picture is blank or not if (isImageBlank(image)) { //Delete the corresponding PDF page if the picture is blank pdf.getPages().removeAt(i); } //Save the Pdf document pdf.saveToFile("Removeblankpages.pdf"); } } public static boolean isImageBlank (BufferedImage image) { for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int pixel = image.getRGB(i, j); Color c = new Color(pixel); if (c.getRed() < 240 || c.getGreen() < 240 || c.getBlue() < 240) { return false; } } } return true; } }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn