国产PDF格式处理控件Spire.PDF功能演示:在Java中查找和删除PDF空页面

Spire.PDF for Java 是一款专门对 PDF 文档进行操作的 Java 类库。该类库的主要功能在于帮助开发人员在 Java 应用程序(J2SE和J2EE)中生成 PDF 文档和操作现有 PDF 文档,并且运行环境无需安装 Adobe Acrobat。

Spire.PDF for Java最新试用版

从 PDF 中删除绝对空白页面

您可以使用 PdfPageBase.isBlank() 方法从 PDF 文档中找到空白页面,然后删除 PDF 中的所有空白页面。

  • 创建PdfDocument类的对象并使用PdfDocument.loadFromFile()方法加载示例 PDF 文档。
  • 循环浏览页面并使用PdfPageBase.isBlank()方法检测页面是否为空白。
  • 使用PdfDocument.getPages().removeAt()方法删除空白页。
  • 使用PdfDocument.saveToFile()方法将文档保存到另一个文件。
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格式处理控件Spire.PDF功能演示:在Java中查找和删除PDF空页面

从 PDF 中删除包含白色图像的页面

以下代码片段将演示如何删除带有白色图像的 PDF 页面。首先,您需要申请 30 天的试用许可证,以删除转换图像中的评估消息。否则,此方法将无法正常工作。

  • 注册许可证密钥以删除警告消息
  • 创建PdfDocument类的对象并使用PdfDocument.loadFromFile()方法加载示例 PDF 文档。
  • 使用PdfDocument.saveAsImage()方法将 PDF 页面转换为图像。
  • 调用自定义方法isImageBlank(BufferedImage image)检测图片是否为空白。
  • 使用PdfDocument.getPages().removeAt()方法删除包含空白图像的页面。
  • 使用PdfDocument.saveToFile()方法将文档保存到另一个文件。
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;        }}

国产PDF格式处理控件Spire.PDF功能演示:在Java中查找和删除PDF空页面整合所有格式API处理套包Spire.office for Java正在 火热销售中!联系客服立马1分钟了解全部咨询!

国产PDF格式处理控件Spire.PDF功能演示:在Java中查找和删除PDF空页面

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年10月16日
下一篇 2021年10月16日

相关推荐

发表回复

登录后才能评论