这里合并用到了一个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 = fal; 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.clo(); } if (copy != null) { copy.clo(); } if (document != null) { document.clo(); } } return retvalue; } public static void main(string[] args) { string[] files = { "d:\\ca\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 = fal;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.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b = mergepdffiles(files, savepath);system.out.println(b);}}00001\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 = fal;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.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b = mergepdffiles(files, savepath);system.out.println(b);}}001\\abistatistic.pdf", "d:\\ca\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 = fal;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.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b = mergepdffiles(files, savepath);system.out.println(b);}}00001\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 = fal;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.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b = mergepdffiles(files, savepath);system.out.println(b);}}001\\abitable.pdf", "d:\\ca\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 = fal;document docu建行金卡有什么用ment = 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;} sin60度等于多少啊catch (exception e) {e.printstacktrace();} finally {if (reader != null) {reader.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b = mergepdffiles(files, savepath);system.out.println(b);}}00001\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 = fal;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.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b初刻拍案惊奇读后感 = mergepdffiles(files, savepath);system.out.println(b);}}001\\cvrr.pdf" }; string savepath = "d:\\ca\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 = fal;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.get四书五经是什么importedpage(reader, j);copy.addpage(page);}}retvalue = true;} catch (exception e) {e.printstacktrace();} finally {if 杞人忧天文言文(reader != null) {reader.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b = mergepdffiles(files, savepath);system.out.println(b);}}00001\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 = fal;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.clo();}if (copy != null) {copy.clo();}if (document != null) {document.clo();}}return retvalue;}public static void main(string[] args) {string[] files = { "d:\\ca\\0000001\\00001\\abistatistic.pdf", "d:\\ca\\0000001\\00001\\abitable.pdf","d:\\ca\\0000001\\00001\\cvrr.pdf" };string savepath = "d:\\ca\\0000001\\00001\\temp.pdf";boolean b = mergepdffiles(files, savepath);system.out.println(b);}}001\\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.clo();}}
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(); pdfpageba 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"); } }
到此这篇关于java实现合并多个pdf的示例代码的文章就介绍到这了,更多相关java合并多个pdf内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 14:06:40,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/00aa54375ce51808b070ca770cc78efb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java实现合并多个PDF的示例代码.doc
本文 PDF 下载地址:Java实现合并多个PDF的示例代码.pdf
留言与评论(共有 0 条评论) |