{"id":2282,"date":"2024-01-24T21:55:06","date_gmt":"2024-01-24T13:55:06","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2282"},"modified":"2024-01-24T21:55:32","modified_gmt":"2024-01-24T13:55:32","slug":"java-itext-cell-table-image","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2024\/01\/24\/java-itext-cell-table-image\/","title":{"rendered":"Java\u7684itext\u5e38\u89c1\u65b9\u6cd5\u603b\u7ed3&#8211;2\uff08\u5355\u5143\u683c\uff0c\u8868\u683c\uff0c\u56fe\u7247\uff09"},"content":{"rendered":"<h2>\u5355\u5143\u683c<\/h2>\n<ol>\n<li>\u5355\u5143\u683c\u7684\u521b\u5efa<\/li>\n<\/ol>\n<pre><code class=\"language-java\">PdfPCell pdfPCell = new PdfPCell();<\/code><\/pre>\n<p>\u5728\u521b\u5efa\u5355\u5143\u683c\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4e00\u822c\u90fd\u4f1a\u7ed9\u5355\u5143\u683c\u7684\u6784\u9020\u51fd\u6570\u4f20\u9012\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u591a\u6570\u662f\u4e00\u4e2a\u542b\u6709\u6587\u672c\u7684\u6bb5\u843d\uff0c\u540c\u65f6\u4f1a\u5728\u8fd9\u4e2a\u6bb5\u843d\u4e0a\u8bbe\u7f6e\u4e86\u884c\u95f4\u8ddd\uff08\u5728\u5b57\u6570\u592a\u591a\uff0c\u5355\u5143\u683c\u7684\u5bbd\u5ea6\u8f83\u5c0f\u60c5\u51b5\u4e0b\u4f1a\u8fdb\u884c\u6362\u884c\uff09\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">Paragraph elements = new Paragraph(text);\n\nelements.setLeading(fixedLeading, multipliedLeading); \/\/fixedLeading\u56fa\u5b9a\u95f4\u8ddd\u503c\uff0cmultipliedLeading\u591a\u500d\u95f4\u8ddd\u503c\n\nPdfPCell pdfPCell = new PdfPCell(elements); \/\/\u8fd9\u4e2a\u65f6\u5019\u5c31\u4e0d\u80fd\u76f4\u63a5\u628a\u6bb5\u843d\u653e\u6784\u9020\u65b9\u6cd5\u4e2d\u4e86\uff0c\u56e0\u4e3anew PdfPCell(elements)\u5e95\u5c42\u4f7f\u7528\u7684\u662f`this.column.setLeading(0.0F, 1.0F);`\uff0c\u4e0d\u4f1a\u5ef6\u7528elements\u7684setLeading\uff0c\u56e0\u6b64\u5728\u5355\u5143\u683c\u4e2d\u6dfb\u52a0\u6bb5\u843d\u5efa\u8bae\u4f7f\u7528\uff1a\n\nPdfPCell pdfPCell = new PdfPCell();\npdfPCell.addElement(elements\u00a0);<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5355\u5143\u683c\u7684\u5e38\u89c1\u65b9\u6cd5<\/li>\n<\/ol>\n<pre><code class=\"language-java\">PdfPCell pdfPCell = new PdfPCell();\n\n\u5bf9\u9f50\u65b9\u5f0f\uff1a\n\n\/\/\u6c34\u5e73\u5782\u76f4\u5bf9\u9f50\npdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);\n\/\/\u6c34\u5e73\u5c45\u4e2d\u5bf9\u9f50\npdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);\n\/\/\u6c34\u5e73\u5c45\u4e2d\u5de6\u5bf9\u9f50\npdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);\n\/\/\u6c34\u5e73\u5c45\u4e2d\u53f3\u5bf9\u9f50\npdfPCell.setHorizontalAlignment(Element.ALIGN_RIGHT);\n\n\u8fb9\u6846\uff08\u5355\u5143\u683c\u9ed8\u8ba4\u662f\u6709\u8fb9\u6846\u7684\uff09\uff1a\n\/\/\u65e0\u8fb9\u6846\npdfPCell.setBorder(NO_BORDER);\n\n\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u9ad8\u5ea6\uff1a\n\/\/\u8bbe\u7f6e\u8ba1\u7b97\u9ad8\u5ea6\npdfPCell.setCalculatedHeight();\u00a0\n\/\/\u8bbe\u7f6e\u56fa\u5b9a\u9ad8\u5ea6\npdfPCell.setFixedHeight();\u00a0\n\/\/\u8bbe\u7f6e\u6700\u5c0f\u9ad8\u5ea6\u00a0\npdfPCell.setMinimumHeight();\u00a0\n\n\u5408\u5e76\u5217\u5355\u5143\u683c\uff1a\n\/\/\u8868\u793a\u8be5\u5355\u5143\u683c\u8ddf\u53f3\u8fb9\u5217\u7684\u4e24\u4e2a\u5355\u5143\u683c\u5408\u5e76\uff08\u4e00\u5171\u4e09\u4e2a\u5355\u5143\u683c\uff09\npdfPCell.setColspan(3);\n\n\u5408\u5e76\u884c\u5355\u5143\u683c\uff1a\n\/\/\u8868\u793a\u8be5\u5355\u5143\u683c\u8ddf\u4e0b\u9762\u7684\u4e00\u4e2a\u5355\u5143\u683c\u5408\u5e76\uff08\u4e00\u4e2a\u4e24\u4e2a\u5355\u5143\u683c\uff09\npdfPCell.setRowspan(2);\n\n\u8bbe\u7f6e\u5143\u7d20\u5728\u5355\u5143\u683c\u5185\u7684\u95f4\u8ddd\uff1a\n\/\/\u5de6\u95f4\u8ddd\npdfPCell.setPaddingLeft();\n\/\/\u53f3\u95f4\u8ddd\npdfPCell.setPaddingRight();\n\/\/\u4e0a\u8fb9\u8ddd\npdfPCell.setPaddingTop();\n\/\/\u4e0b\u8fb9\u8ddd\npdfPCell.setPaddingBottom();\n\n\u8bbe\u7f6e\u6587\u5b57\u5728\u5355\u5143\u683c\u4e2d\u6362\u884c\u65f6\u7684\u884c\u95f4\u8ddd\uff1a\npdfPCell.setLeading(fixedLeading,multipliedLeading);\n\u6700\u7ec8\u884c\u95f4\u8ddd \uff1atotalLeading= fixedLeading+fontSize(\u5b57\u4f53\u5927\u5c0f)* multipliedLeading;\n\n\u5355\u5143\u683c\u7684\u80cc\u666f\u989c\u8272\uff1a\npdfPCell.setBackgroundColor(new BaseColor(111,112,50));\u00a0 \/\/ \u6570\u5b57\u4ee3\u8868\u7684\u662fRGB\u4e09\u4e2a\u989c\u8272\u7684\u6570\u503c\n\n\u5355\u5143\u683c\u6dfb\u52a0\u5143\u7d20\uff1a\npdfPCell.addElement();<\/code><\/pre>\n<h2>\u8868\u683c<\/h2>\n<ol>\n<li>\u8868\u683c\u7684\u521b\u5efa<\/li>\n<\/ol>\n<pre><code class=\"language-java\">PdfPTabletable = new PdfPTable(3);\n\u5982\u679c\u521b\u5efa\u4e86N\u5217\uff0c\u5982\u679c`table.add`\u6dfb\u52a0\u7684\u5355\u5143\u683c\u6570\u91cf\u4e0d\u662fN\u7684\u500d\u6570\uff0c\u90a3\u4e48\u5c31\u4f1a\u62a5\u5f02\u5e38<\/code><\/pre>\n<ol start=\"2\">\n<li>\u8868\u683c\u7684\u5e38\u89c1\u65b9\u6cd5<\/li>\n<\/ol>\n<pre><code class=\"language-java\">\u5bbd\u5ea6\u767e\u5206\u6bd4\uff1a\n\/\/\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u753b\u51fa\u8fdb\u5ea6\u6761\ntable.setWidthPercentage(100);\n\n\/\/\u524d\u95f4\u8ddd\ntable.setSpacingBefore(10f);\n\/\/\u540e\u95f4\u8ddd\ntable.setSpacingAfter(10f);\n\n\/\/\u8bbe\u7f6e\u6bcf\u5217\u7684\u5bbd\u5ea6,\u56e0\u4e3a\u662f\u5b9a\u4e49\u4e86\u4e09\u5217\u7684\u8868\u683c\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u8bbe\u7f6e3\u4e2a\u6570\u636e\uff0c\u4ee3\u8868\u4e86\u6bcf\u5217\u7684\u5bbd\u5ea6\nfloat[] columnWidths = { 1f, 2f, 3f };\ntable.setWidths(columnWidths);\n\n\/\/\u5bf9\u9f50\u65b9\u5f0f\ntable .setHorizontalAlignment(Element.ALIGN_LEFT); \/\/\u5de6\u5bf9\u9f50\n\n\/\/\u6dfb\u52a0\u5355\u5143\u683c\ntable.add(new PdfPCell());<\/code><\/pre>\n<h2>\u56fe\u7247<\/h2>\n<ol>\n<li>\u83b7\u53d6\u8be5\u9875\u56db\u4e2a\u70b9\u7684\u5750\u6807\uff08\u6700\u5de6\u4e0a\u89d2\uff0c\u6700\u5de6\u4e0b\u89d2\uff0c\u6700\u53f3\u4e0a\u89d2\uff0c\u6700\u53f3\u4e0b\u89d2\uff09<\/li>\n<\/ol>\n<pre><code class=\"language-java\">document.getPageSize().getTop(); \/\/y\u8f74\u6700\u4e0a\u8fb9\u7684y\u5750\u6807\ndocument.getPageSize().getBottom(); \/\/y\u8f74\u6700\u4e0b\u8fb9\u7684y\u5750\u6807\ndocument.getPageSize().getLeft(); \/\/x\u8f74\u6700\u5de6\u8fb9\u7684x\u5750\u6807\ndocument.getPageSize().getRight(); \/\/x\u8f74\u6700\u53f3\u8fb9\u7684x\u5750\u6807\ndocument.getPageSize().getWidth(); \/\/\u83b7\u53d6\u9875\u9762\u5bbd\u5ea6\ndocument.getPageSize().getHeight(); \/\/\u83b7\u53d6\u9875\u9762\u9ad8\u5ea6<\/code><\/pre>\n<ol start=\"2\">\n<li>\u56fe\u7247\u7684\u83b7\u53d6\u4ee5\u53ca\u6dfb\u52a0\u5230PDF\u4e0a<\/li>\n<\/ol>\n<pre><code class=\"language-java\">\/\/\u83b7\u53d6\u7167\u7247\nImageimage = Image.getInstance(&quot;\u672c\u5730\u56fe\u7247\u5730\u5740&quot;);\n\n\/\/\u4e5f\u53ef\u4ee5\u83b7\u53d6\u7f51\u4e0a\u7684\u56fe\u7247\uff0c\u4f8b\u5982\uff1a\nURLurl = \u00a0newURL(&quot;http:\/\/static.cnblogs.com\/images\/adminlogo.gif&quot;)\nImageimage = Image.getInstance(url);\n\n\/\/\u8bbe\u7f6e\u56fe\u7247\u4f4d\u7f6e\u7684x\u8f74\u548cy\u8f74\nimage.setAbsolutePosition(0, 0); \/\/ \u76f8\u5bf9\u4e8e\u4e0a\u9762\u83b7\u53d6\u5230\u7684\u5750\u6807\uff0c\u4f4d\u4e8e\u5de6\u4e0b\u89d2\n\n\/\/\u8bbe\u7f6e\u56fe\u7247\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\nimage.scaleAbsolute(100, 100);\n\n\/\/\u6dfb\u52a0\u56fe\u7247\ndocument.add(image);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u5143\u683c \u5355\u5143\u683c\u7684\u521b\u5efa PdfPCell pdfPCell = new PdfPCell(); \u5728\u521b\u5efa\u5355\u5143\u683c\u7684\u65f6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[574],"class_list":["post-2282","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-itext"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2282"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2282\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}