{"id":1211,"date":"2010-07-12T19:22:50","date_gmt":"2010-07-12T13:52:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1211"},"modified":"2016-12-19T15:06:06","modified_gmt":"2016-12-19T09:36:06","slug":"merge-two-pdf-files","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/merge-two-pdf-files\/","title":{"rendered":"Merge two PDF files"},"content":{"rendered":"<p>In my current project the user has the option to add pdf file into the system. Recently we got the requirement to add a cover page to each pdf document that user downloads. We were already using the iText API for generating pdf so the task to do that was easy. After going through the documentation of iText I found a way to merge two pdf files.<\/p>\n<pre lang=\"groovy\"> \r\ntry{\r\n            PdfReader pdfReader1 = new PdfReader(\"firstFile.pdf\");\r\n            PdfReader pdfReader2 = new PdfReader(\"secondFile.pdf\");\r\n            PdfCopyFields finalCopy = new PdfCopyFields(new FileOutputStream(\"finalCopy.pdf\"));\r\n            finalCopy.open();\r\n            [pdfReader1,pdfReader2].each {PdfReader pdfReader ->\r\n                   finalCopy.addDocument(pdfReader);\r\n            }\r\n            finalCopy.close();\r\n        } catch (DocumentException e) {\r\n            e.printStackTrace();\r\n        } catch (FileNotFoundException e) {\r\n            e.printStackTrace();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n<\/pre>\n<p><\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my current project the user has the option to add pdf file into the system. Recently we got the requirement to add a cover page to each pdf document that user downloads. We were already using the iText API for generating pdf so the task to do that was easy. After going through the [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[306,304,305],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1211"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}