Java中多个pdf文件合并为一个

使用maven引入jar包:


    com.lowagie
    itext
    2.1.7

 可以使用工具类和测试Utile测试一下~

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 合并成新的文件的路径
     */
    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);
                }
				reader.close();
            }
            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:\wanyi_az\Dict\a.pdf","D:\wanyi_az\Dict\b.pdf","D:\wanyi_az\Dict\c.pdf" };
        String savepath = "D:\wanyi_az\Dict\back.pdf";
        boolean b = mergePdfFiles(files, savepath);
        System.out.println(b);
    }
}

你可能感兴趣的:(JAVA,java,开发语言,maven)