java压缩pdf

    <!-- PDF操作,itext7全家桶 -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.1.15</version>
        <type>pom</type>
    </dependency>

package org.example;

import com.itextpdf.kernel.pdf.*;

import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;

import javax.imageio.ImageIO;

import java.awt.;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.
;

public class Main {

public static void main(String[] args) throws IOException {
    PdfName key = new PdfName("ITXT_SpecialId");
    PdfName value = new PdfName("123456789");
    // 读取pdf文件
    PdfReader reader = null;
    try {
        reader = new PdfReader(new FileInputStream(new File("E:\\mqdemo\\paixu\\src\\main\\resources\\static\\2024作品集 .pdf")));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    PdfDocument pdfDocument = new PdfDocument(reader,new PdfWriter(swapStream));

    long n = reader.getLastXref();
    PdfObject object;
    PdfStream stream;
    //查找图像并操作图像流
    for (int i = 0; i < n; i++) {

        object = pdfDocument.getPdfObject(i);
        if (object == null || !object.isStream())
            continue;
        stream = (PdfStream) object;
        PdfObject pdfsubtype = stream.get(PdfName.Subtype);
        if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.Image.toString())) {
            PdfImageXObject image = new PdfImageXObject(stream);
            int i1 = image.getPdfObject().getAsNumber(PdfName.BitsPerComponent).intValue();
            //小于8的都跳过,BitsPerComponent=4是提示报错The color depth BitsPerComponent的值 is not supported
            //this.bpc != 8 ImagePdfBytesInfo类下decodeTiffAndPngBytes
            //当位图不等于8时,直接跳过
            if (i1 != 8) {
                continue;
            }
            BufferedImage bi = image.getBufferedImage();
            if (bi == null) continue;
            int width = (int) (bi.getWidth() * 0.7f);
            int height = (int) (bi.getHeight() * 0.7f);
            BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            AffineTransform at = AffineTransform.getScaleInstance(0.7f, 0.7f);
            Graphics2D g = img.createGraphics();
            g.drawRenderedImage(bi, at);
            ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
            //标记此处,后面会修改
            ImageIO.write(img, "JPG", imgBytes);
            stream.clear();
            stream.setData(imgBytes.toByteArray(), false);
            stream.put(PdfName.Type, PdfName.XObject);
            stream.put(PdfName.Subtype, PdfName.Image);
            stream.put(key, value);
            stream.put(PdfName.Filter, PdfName.DCTDecode);
            stream.put(PdfName.Width, new PdfNumber(width));
            stream.put(PdfName.Height, new PdfNumber(height));
            stream.put(PdfName.BitsPerComponent, new PdfNumber(8));
            stream.put(PdfName.ColorSpace, PdfName.DeviceRGB);
        }
    }
    //将数据写入到输出流中
    pdfDocument.close();
    reader.close();
    System.out.println("pdf压缩完成, 文件大小="+ swapStream.size());

    FileOutputStream fileOut = new FileOutputStream("outputFile.pdf");
     fileOut.write(swapStream.toByteArray());


}

}

相关推荐
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸4 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象4 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了5 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024065 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
小二·5 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic5 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it5 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
懒洋洋大魔王5 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq