Java合并多个PDF

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java合并多个PDF

怪咖软妹@   2022-05-23 我要评论

这里合并用到了一个itext的包。使用maven直接导入依赖即可。

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>

这个是我写的一个utl工具类,里面还写了一个main方法,如果你有两个pdf,可以直接用main方法跑一下。

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

import java.io.FileOutputStream;

public class PdfUtil {

    /**
     * 合并pdf
     * @param files 需要合并的pdf路径
     * @param newfile 合并成新的文件的路径
     * @return
     */
    public static boolean mergePdfFiles(String[] files, String newfile) {
        boolean retValue = false;
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            document = new Document(new PdfReader(files[0]).getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newfile));
            document.open();
            for (int i = 0; i < files.length; i++) {
                reader = new PdfReader(files[i]);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            retValue = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (document != null) {
                document.close();
            }
        }
        return retValue;
    }

    public static void main(String[] args) {
        String[] files = { "D:\\Case\\0000001\\00001\\ABIStatistic.pdf", "D:\\Case\\0000001\\00001\\ABITable.pdf",
                "D:\\Case\\0000001\\00001\\CVRR.pdf" };
        String savepath = "D:\\Case\\0000001\\00001\\temp.pdf";
        boolean b = mergePdfFiles(files, savepath);
        System.out.println(b);
    }
}

补充

通过java还能实现pdf的拆分

1.按每页单独拆分

import com.spire.pdf.*;

public class SplitPDF1 {
	public static void main(String[] args)
	{
	//加载需要拆分的PDF文档
  PdfDocument doc = new PdfDocument();
  doc.loadFromFile("test.pdf");

  //调用方法split()将PDF文档按每一页拆分为单独的文档
  doc.split("output/splitDocument-{0}.pdf", 0);
  doc.close();
	}
}

2.按指定页数范围拆分

import com.spire.pdf.*; 
import com.spire.pdf.graphics.PdfMargins; 
 
import java.awt.geom.Point2D; 
 
public class SplitPDF2 { 
  public static void main(String[] args) 
  { 
 
    //加载需要拆分的PDF文档 
    PdfDocument doc = new PdfDocument(); 
    doc.loadFromFile("test.pdf"); 
 
    //新建第1个PDF文档1 
    PdfDocument newpdf1 = new PdfDocument(); 
    PdfPageBase page; 
 
    //将原PDF文档的第1、2页拆分,并保存到newpdf1 
    for(int i = 0;i<2;i++) 
    { 
      page = newpdf1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0)); 
      doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0)); 
    } 
    newpdf1.saveToFile("split/result1.pdf"); 
 
    //新建第2个PDF文档 
    PdfDocument newpdf2 = new PdfDocument(); 
 
    //将原PDF文档的第3、4页拆分,并保存到newpdf2 
    for(int i = 2;i<4;i++) 
    { 
      page = newpdf2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0)); 
      doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0)); 
    } 
    newpdf2.saveToFile("split/result2.pdf"); 
  } 
} 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们