JAVA PDF操作之实现截取N页和多个PDF合并

 更新时间:2025年01月16日 10:27:14   作者:VipSoft  
这篇文章主要为大家详细介绍了java关于PDF的一些操作,例如截取N页并生成新文件,转图片以及多个PDF合并,文中的示例代码讲解详细,感兴趣的可以了解下

JAVA PDF 截取N页,生成新文件,转图片,多个PDF 合并

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>

完整代码 

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class PdfUtil {

    /**
     * 截取pdfFile的第from页至第end页,组成一个新的文件名
     *
     * @param pdfFile 要切割的pdf文件
     * @param newFile 切割后形成的新的pdf文件
     * @param from    从第N页开始
     * @param end     到第N页结束
     */
    public static void partitionPdf(String pdfFile, String newFile, int from, int end) {
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdfFile);
            int pageCount = reader.getNumberOfPages();
            if (from < 1) {
                from = 1;
            }
            if (from > pageCount) {
                from = pageCount;
            }
            if (end == 0 || end > pageCount) {
                end = pageCount;
            }
            document = new Document(reader.getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newFile));
            document.open();
            for (int j = from; j <= end; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }

    /**
     * pdf转图片
     *
     * @param pdfFile   PDF 文件
     * @param imageFile 输出的图片文件
     * @param from      开始页 从1开始
     * @param end       结束页 最大为PDF总页数
     * @throws Exception
     */
    public static void pdfToImage(String pdfFile, String imageFile, int from, int end) throws Exception {
        PDDocument doc = null;
        ByteArrayOutputStream os = null;
        InputStream stream = null;
        OutputStream out = null;
        try {
            //pdf路径
            stream = new FileInputStream(pdfFile);
            // 加载解析PDF文件
            doc = PDDocument.load(stream);
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            PDPageTree pages = doc.getPages();
            int pageCount = pages.getCount();
            if (from < 1) {
                from = 1;
            }
            if (from > pageCount) {
                from = pageCount;
            }
            if (end == 0 || end > pageCount) {
                end = pageCount;
            }
            for (int i = from; i <= end; i++) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(i - 1, 200); //PDFBOX 是从0开始的,from初始值为1,所以这边要减 i-1
                os = new ByteArrayOutputStream();
                ImageIO.write(bim, "jpg", os);
                byte[] dataList = os.toByteArray(); 
                //只取一页,等于传进来的名称,多页时,加上 页号
                String imageFilePath = from == end ? saveImgFile : saveImgFile.replace(".jpg", "_" + i + ".jpg");
                File file = new File(imageFilePath);
                if (!file.getParentFile().exists()) {
                    // 不存在则创建父目录及子文件
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                out = new FileOutputStream(file);
                out.write(dataList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (doc != null) {
                doc.close();
            }
            if (os != null) {
                os.close();
            }
            if (stream != null) {
                stream.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    //多个PDF合并成一个
    public static void mergePDFFiles(List<String> pdfFiles, String outputPdf) throws IOException {
        // 创建一个新的 PDF 阅读器对象和一个新的 PDF 写入对象
        PdfReader reader = null;
        PdfCopy copy = null;
        Document document = new Document();
        try {
            // 创建 PDF 阅读器对象和写入对象
            reader = new PdfReader(pdfFiles.get(0));
            copy = new PdfCopy(document, new FileOutputStream(outputPdf));
            // 打开文档准备写入内容
            document.open();

            // 将第一个 PDF 的所有页面复制到输出 PDF 中
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                PdfImportedPage page = copy.getImportedPage(reader, i);
                copy.addPage(page);
            }

            // 将其它PDF的所有页,输出到 PDF 中
            for (int i = 1; i < pdfFiles.size(); i++) {
                reader = new PdfReader(pdfFiles.get(i));
                for (int j = 1; j <= reader.getNumberOfPages(); j++) {
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }
}

测试类 

@Test
void pdf() throws Exception {
    String pdfFile = "D:\\Desktop\\20220117.pdf";
    String jpgFile = "D:\\Desktop\\20220117.jpg";
    PdfUtil.pdfToImage(pdfFile, jpgFile, 1, 1); 
}

@Test
 void testMerge() throws IOException {
    List<String> pdfFiles = new ArrayList<>();
    pdfFiles.add("D:\\Projects\\20231225180735.pdf");
    pdfFiles.add("D:\\Projects\\20231225182535.pdf");
    pdfFiles.add("D:\\Projects\\20231225184135.pdf");
    PdfUtil.mergePDFFiles(pdfFiles, "D:\\Projects\\New.pdf");
}

到此这篇关于JAVA PDF操作之实现截取N页和多个PDF合并的文章就介绍到这了,更多相关JAVA PDF内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java中关于getProperties方法的使用

    java中关于getProperties方法的使用

    这篇文章主要介绍了java中关于getProperties方法的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java接口回调和方法回调的简单实现步骤

    Java接口回调和方法回调的简单实现步骤

    这篇文章主要介绍了Java接口回调和方法回调的相关资料,接口回调是一种设计模式,实现三方解耦,调用者提供接口实现,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-03-03
  • SpringMVC @RequestMapping注解详解

    SpringMVC @RequestMapping注解详解

    本文主要介绍了SpringMVC @RequestMapping注解详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • jQuery 动画效果代码分享

    jQuery 动画效果代码分享

    本文给大家分享一段关于jquery实现的动画效果,代码简单易懂,非常不错,感兴趣的朋友参考下
    2016-11-11
  • 关于注解式的分布式Elasticsearch的封装案例

    关于注解式的分布式Elasticsearch的封装案例

    这篇文章主要介绍了关于注解式的分布式Elasticsearch的封装案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • SpringBoot-JWT生成Token和拦截器的使用(访问受限资源)

    SpringBoot-JWT生成Token和拦截器的使用(访问受限资源)

    本文主要介绍了SpringBoot-JWT生成Token和拦截器的使用(访问受限资源),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Mybatis中一对多(collection)和一对一(association)的组合查询使用

    Mybatis中一对多(collection)和一对一(association)的组合查询使用

    这篇文章主要介绍了Mybatis中一对多(collection)和一对一(association)的组合查询使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • JAVA使用爬虫抓取网站网页内容的方法

    JAVA使用爬虫抓取网站网页内容的方法

    这篇文章主要介绍了JAVA使用爬虫抓取网站网页内容的方法,实例分析了java爬虫的两种实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 如何获取java类中的属性注释

    如何获取java类中的属性注释

    在开发中,有时需要获取Java类的属性注释,尤其是当JPA生成的表缺少注释时,可以通过jdk自带的tools.jar工具包来实现,方法类似于生成javadoc文档,需要在pom.xml文件中导入tools.jar的依赖,该jar文件一般位于JAVA_HOME/lib目录下
    2024-09-09
  • Java如何获取Cookie和Session

    Java如何获取Cookie和Session

    Cookie 和 Session之间主要是通过 SessionId 关联起来的, SessionId是 Cookie 和 Session 之间的桥梁,这篇文章主要介绍了Java获取Cookie和Session的方法,需要的朋友可以参考下
    2024-01-01

最新评论