{"id":231823,"date":"2021-12-21T08:14:10","date_gmt":"2021-12-21T00:14:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=231823"},"modified":"2021-12-13T09:14:42","modified_gmt":"2021-12-13T01:14:42","slug":"java-manipulation-of-pdf-files","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-manipulation-of-pdf-files.html","title":{"rendered":"Java\u64cd\u4f5cPDF\u6587\u4ef6\u4e4bIText"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\niText\u662f\u8457\u540d\u7684\u5f00\u653e\u9879\u76ee\uff0c\u662f\u7528\u4e8e\u751f\u6210PDF\u6587\u6863\u7684\u4e00\u4e2ajava\u7c7b\u5e93\u3002\u901a\u8fc7iText\u4e0d\u4ec5\u53ef\u4ee5\u751f\u6210PDF\u6216rtf\u7684\u6587\u6863\uff0c\u800c\u4e14\u53ef\u4ee5\u5c06XML\u3001Html\u6587\u4ef6\u8f6c\u5316\u4e3aPDF\u6587\u4ef6\u3002 \u672c\u6587\u4ece iText \u6700\u57fa\u672c\u7684\u7528\u6cd5\uff0c\u5206\u522b\u4ece\u8868\u683c,\u56fe\u7247\uff0c\u4e2d\u6587\u6765\u4ecb\u7ecd iText\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u5230\u4f60\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
maven \u4f9d\u8d56<\/strong><\/div>\n

\u4f7f\u7528 iText \u7684\u65f6\u5019\u9700\u8981\u7528 maven \u4f9d\u8d56\u5982\u4e0b\uff1a<\/p>\n

\r\n<dependency> \r\n    <groupId>com.itextpdf<\/groupId> \r\n    <artifactId>itext7-core<\/artifactId> \r\n    <version>7.2.0<\/version> \r\n    <type>pom<\/type> \r\n<\/dependency>\r\n<\/pre>\n
\u7b80\u5355\u5b9e\u4f8b<\/strong><\/div>\n

\u5efa\u7acb PDF \u9700\u8981 4 \u4e2a\u6b65\u9aa4\uff1a<\/p>\n

\u521b\u5efa \u4e00\u4e2a PdfWriter\uff0c\u7528\u4e8e\u5b9a\u4e49 pdf \u7684\u8def\u5f84\u5730\u5740
\n\u521b\u5efa\u4e00\u4e2a pdf \u6587\u6863\uff0cDocument\uff0c\u4e0e PdfWriter \u5173\u8054
\n\u5411\u6587\u6863\u4e2d\u6dfb\u52a0\u5185\u5bb9<\/p>\n

\u5173\u95ed\u6587\u6863<\/strong><\/div>\n

\u4e0b\u9762\u4ece hello world \u5f00\u59cb\u3002<\/p>\n

\r\nimport com.itextpdf.kernel.pdf.PdfDocument; \r\nimport com.itextpdf.kernel.pdf.PdfWriter; \r\nimport com.itextpdf.layout.Document; \r\nimport com.itextpdf.layout.element.Paragraph; \r\n \r\npublic class PDFDemo { \r\n \r\n    public static void main(String[] args) { \r\n        try{ \r\n            PdfWriter pdfWriter = new PdfWriter(\".\/demo.pdf\"); \r\n            PdfDocument pdfDocument = new PdfDocument(pdfWriter); \r\n            Document document = new Document(pdfDocument); \r\n            document.add(new Paragraph(\"Hello World\")); \r\n            document.close(); \r\n        }catch(Exception e){ \r\n            e.printStackTrace(); \r\n        } \r\n    } \r\n}<\/pre>\n

\u793a\u4f8b\u7ed3\u679c\uff1a
\n\"\"<\/p>\n

\u4e2d\u6587<\/strong><\/div>\n

iText \u4e0d\u652f\u6301\u9ed8\u8ba4\u662f\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u4f53\u7684\uff0c\u6240\u4ee5\u9700\u8981\u5b9a\u4e49 PdfFont \u4e2d\u6587\u5b57\u4f53\u6837\u5f0f\u3002\u4f7f\u7528 PdfFontFactory \u7684 createFont \u521b\u5efa\u4e00\u4e2a\u4e2d\u6587\u5b57\u4f53\uff0c\u5e76\u5c06\u4e2d\u6587\u5b57\u4f53\u8bbe\u7f6e\u5230 document \u4e2d\u3002<\/p>\n

\r\npublic static void main(String[] args) { \r\n    try{ \r\n        PdfWriter pdfWriter = new PdfWriter(\".\/demo1.pdf\"); \r\n        PdfDocument pdfDocument = new PdfDocument(pdfWriter); \r\n        \/\/\u5b57\u4f53\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528 windows \u7cfb\u7edf\u4e2d\u7684 \r\n        PdfFont font = PdfFontFactory.createFont(\"E:\\\\pdfProject\\\\src\\\\main\\\\java\\\\simhei.ttf\"); \r\n        Document document = new Document(pdfDocument).setFont(font); \r\n        document.add(new Paragraph(\"Hello World!\u4f60\u597d,itext\")); \r\n        document.close(); \r\n    }catch(Exception e){ \r\n        e.printStackTrace(); \r\n    } \r\n} <\/pre>\n

\u793a\u4f8b\u7ed3\u679c\uff1a
\n\"\"<\/p>\n

\u8868\u683c<\/strong><\/div>\n

\u5728\u9879\u76ee\u4e2d\u7ecf\u5e38\u9700\u8981 PDF \u6587\u4ef6\u4e2d\u6253\u5370\u8868\u683c\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528 Table \u5bf9\u8c61\uff0c\u521b\u5efa\u4e00\u4e2a\u4e2a\u5355\u5143\u683c\u5e76\u5c06\u8868\u683c\u5185\u5bb9\u5199\u5165\u5355\u5143\u683c\u4e2d\u3002<\/p>\n

\r\npublic static void main(String[] args) { \r\n    try{ \r\n        PdfWriter pdfWriter = new PdfWriter(\".\/demo2.pdf\"); \r\n        PdfDocument pdfDocument = new PdfDocument(pdfWriter); \r\n        PdfFont font = PdfFontFactory.createFont(\"E:\\\\pdfProject\\\\src\\\\main\\\\java\\\\simhei.ttf\"); \r\n        Document document = new Document(pdfDocument).setFont(font); \r\n        Table table = new Table(4); \r\n        table.setWidth(500); \r\n        table.addHeaderCell(\"header 1\").addHeaderCell(\"header 2\").addHeaderCell(\"header 3\").addHeaderCell(\"header 4\"); \r\n        for(int i = 0; i < 16; i++) { \r\n            table.addCell(\"cell \" + i); \r\n        } \r\n        document.add(table); \r\n \r\n        document.close(); \r\n    }catch(Exception e){ \r\n        e.printStackTrace(); \r\n    } \r\n} <\/pre>\n

\u793a\u4f8b\u7ed3\u679c\uff1a
\n\"\"<\/p>\n

\u56fe\u7247<\/strong><\/div>\n

\u56fe\u7247\u7684\u8bbe\u7f6e\u4e5f\u5f88\u7b80\u5355\uff0c\u521b\u5efa Image \u5bf9\u8c61\u586b\u5145\u5230 document \u4e2d\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n

\r\npublic static void main(String[] args) { \r\n    try{ \r\n        PdfWriter pdfWriter = new PdfWriter(\".\/demo3.pdf\"); \r\n        PdfDocument pdfDocument = new PdfDocument(pdfWriter); \r\n        PdfFont font = PdfFontFactory.createFont(\"E:\\\\pdfProject\\\\src\\\\main\\\\java\\\\simhei.ttf\"); \r\n        Document document = new Document(pdfDocument).setFont(font); \r\n        Image img = new Image(ImageDataFactory.create(\"E:\\\\pdfProject\\\\src\\\\main\\\\java\\\\img.png\")); \r\n        document.add(img.setAutoScale(true)); \r\n \r\n        document.close(); \r\n    }catch(Exception e){ \r\n        e.printStackTrace(); \r\n    } \r\n}<\/pre>\n

\u793a\u4f8b\u7ed3\u679c\uff1a
\n\"\"<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u672c\u6587\u4ece iText \u6700\u57fa\u672c\u7684\u7528\u6cd5\uff0c\u5206\u522b\u4ecb\u7ecd\u4ece\u8868\u683c,\u56fe\u7247\uff0c\u4e2d\u6587\u6765\u4ecb\u7ecd iText\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u5230\u4f60\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f7f\u7528 iText \u7684\u65f6\u5019\u9700\u8981\u7528 maven \u4f9d\u8d56\u5982\u4e0b\uff1a <dependency> <grou […]<\/p>\n","protected":false},"author":1329,"featured_media":231830,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-231823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=231823"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231823\/revisions"}],"predecessor-version":[{"id":231833,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231823\/revisions\/231833"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231830"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=231823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=231823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=231823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}