{"id":5578,"date":"2023-12-06T10:00:00","date_gmt":"2023-12-06T02:00:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5578"},"modified":"2023-12-04T21:00:56","modified_gmt":"2023-12-04T13:00:56","slug":"%e5%9c%a8-java-%e4%b8%ad%e4%bd%bf%e7%94%a8-itext-%e6%93%8d%e4%bd%9cpdf%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5578.html","title":{"rendered":"\u5728 Java \u4e2d\u4f7f\u7528 iText \u64cd\u4f5cPDF\u6587\u4ef6"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u524d\u6bb5\u65f6\u95f4\u770b\u5230\u4e00\u7bc7\u6587\u7ae0\u60f3\u8d77\u6765\u7684\uff0c\u6700\u8fd1\u6709\u65f6\u95f4\u7b80\u5355\u505a\u4e86\u6d4b\u8bd5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u4ee3\u7801\u548c\u529f\u80fd\u7684\u5b66\u4e60\uff0c\u8fd9\u91cc\u7b80\u5355\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p>iText \u7684 PdfStamper \u505a\u8d77\u8fd9\u4e2a\u5de5\u4f5c\u6765\u771f\u7684\u662f\u5f88\u65b9\u4fbf\u548c\u5feb\u901f\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u7b80\u5355\u6574\u7406\u4e00\u4e0b\u5e38\u89c1\u7684PDF\u6587\u4ef6\u8bfb\u5199\u9700\u6c42\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u5176\u5b83\u7684\u529f\u80fd\u5b9e\u73b0\u540e\u9762\u6309\u5b9e\u9645\u9700\u6c42\u60c5\u51b5\u8fdb\u884c\u8865\u5145\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.itextpdf\/itextpdf --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.itextpdf&lt;\/groupId&gt;\n    &lt;artifactId&gt;itextpdf&lt;\/artifactId&gt;\n    &lt;version&gt;5.5.13.3&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example;\n\nimport com.itextpdf.text.BaseColor;\nimport com.itextpdf.text.DocumentException;\nimport com.itextpdf.text.Element;\nimport com.itextpdf.text.pdf.BaseFont;\nimport com.itextpdf.text.pdf.PdfContentByte;\nimport com.itextpdf.text.pdf.PdfReader;\nimport com.itextpdf.text.pdf.PdfStamper;\nimport com.itextpdf.text.pdf.parser.PdfTextExtractor;\n\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.util.Map;\n\n\/**\n * @author ixyzero\n * Created on 2023-12-01\n *\/\npublic class ItextWatermark {\n\n    static void printPdfContent(PdfReader pdfReader) throws IOException {\n        \/\/ \u9010\u9875\u6253\u5370 PDF \u6587\u4ef6\u5185\u5bb9\n        for (int i = 1; i &lt;= pdfReader.getNumberOfPages(); i++) {\n            String pageContent = PdfTextExtractor.getTextFromPage(pdfReader, i);\n            System.out.println(\"\\nContent on Page \" + i + \":\\n\" + pageContent);\n        }\n    }\n\n    public static void main(String&#91;] args) throws IOException, DocumentException {\n        \/\/ \u8bfb\u53d6\u539f\u59cb PDF \u6587\u4ef6\n        PdfReader reader = new PdfReader(\"output-pdfbox.pdf\");\n        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(\"output-itext.pdf\"));\n\n        \/\/ \u9010\u9875\u6253\u5370 PDF \u6587\u4ef6\u5185\u5bb9\n        printPdfContent(reader);\n\n        \/\/ \u67e5\u770b PDF \u6587\u4ef6\u7684meta\u4fe1\u606f\n        Map&lt;String, String&gt; info = reader.getInfo();\n        for (Map.Entry&lt;String, String&gt; entry : info.entrySet()) {\n            System.out.println(entry.getKey() + \":\" + entry.getValue());\n        }\n\n        \/\/ \u83b7\u53d6 PDF \u4e2d\u7684\u9875\u6570\n        int pageCount = reader.getNumberOfPages();\n\n        \/\/ \u6dfb\u52a0\u6c34\u5370\n        for (int i = 1; i &lt;= pageCount; i++) {\n            PdfContentByte contentByte = stamper.getUnderContent(i); \/\/ \u6216\u8005 getOverContent()\n            contentByte.beginText();\n            contentByte.setFontAndSize(BaseFont.createFont(), 1f); \/\/ \u5b57\u4f53\u548c\u5b57\u53f7\n            contentByte.setColorFill(BaseColor.WHITE); \/\/ \u5b57\u4f53\u989c\u8272\n            \/\/ showTextAligned(\u5143\u7d20\u5bf9\u9f50\u65b9\u5f0f\uff0c\u6587\u672c\u5185\u5bb9\uff0cx\u5750\u6807\uff0cy\u5750\u6807\uff0c\u65cb\u8f6c\u89d2\u5ea6)\n            contentByte.showTextAligned(Element.ALIGN_BOTTOM, \"Watermark-add-here\", 100, 1, 0);\n            contentByte.endText();\n        }\n\n        \/\/ \u4fdd\u5b58\u4fee\u6539\u540e\u7684 PDF \u6587\u4ef6\u5e76\u5173\u95ed\u6587\u4ef6\u6d41\n        stamper.close();\n        reader.close();\n    }\n\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>SpringBoot \u5b9e\u73b0 PDF \u6dfb\u52a0\u6c34\u5370\uff0c\u6211\u6709 5 \u79cd\u5b9e\u73b0\u65b9\u6848<br><a href=\"https:\/\/mp.weixin.qq.com\/s\/gphcp_L80OzOXxsPAijgig\">https:\/\/mp.weixin.qq.com\/s\/gphcp_L80OzOXxsPAijgig<\/a><\/p>\n\n\n\n<p>A Free Java-PDF library<br><a href=\"https:\/\/mvnrepository.com\/artifact\/com.itextpdf\/itextpdf\">https:\/\/mvnrepository.com\/artifact\/com.itextpdf\/itextpdf<\/a><\/p>\n\n\n\n<p>showTextAligned<br><a href=\"https:\/\/www.tabnine.com\/code\/java\/methods\/com.itextpdf.text.pdf.PdfContentByte\/showTextAligned\">https:\/\/www.tabnine.com\/code\/java\/methods\/com.itextpdf.text.pdf.PdfContentByte\/showTextAligned<\/a><\/p>\n\n\n\n<p>iText &#8211; Quick Guide<br><a href=\"https:\/\/www.tutorialspoint.com\/itext\/itext_quick_guide.htm\">https:\/\/www.tutorialspoint.com\/itext\/itext_quick_guide.htm<\/a><\/p>\n\n\n\n<p>iText &#8211; Setting Font<br><a href=\"https:\/\/www.tutorialspoint.com\/itext\/itext_setting_font.htm\">https:\/\/www.tutorialspoint.com\/itext\/itext_setting_font.htm<\/a><\/p>\n\n\n\n<p>Are there any Java PDF creation alternatives to iText? [closed]<br><a href=\"https:\/\/stackoverflow.com\/questions\/1377135\/are-there-any-java-pdf-creation-alternatives-to-itext\">https:\/\/stackoverflow.com\/questions\/1377135\/are-there-any-java-pdf-creation-alternatives-to-itext<\/a><\/p>\n\n\n\n<p>iText &#8211; add content to existing PDF file<br><a href=\"https:\/\/stackoverflow.com\/questions\/3335126\/itext-add-content-to-existing-pdf-file\">https:\/\/stackoverflow.com\/questions\/3335126\/itext-add-content-to-existing-pdf-file<\/a><\/p>\n\n\n\n<p>Tagged with itext,pdf,java<br><a href=\"https:\/\/stackoverflow.com\/questions\/tagged\/itext+pdf+java\">https:\/\/stackoverflow.com\/questions\/tagged\/itext+pdf+java<\/a><\/p>\n\n\n\n<p>Reading PDF File Using Java<br><a href=\"https:\/\/www.baeldung.com\/java-pdf-file-read\">https:\/\/www.baeldung.com\/java-pdf-file-read<\/a><\/p>\n\n\n\n<p>iText \u2013 Read and Write PDF in Java<br><a href=\"https:\/\/mkyong.com\/java\/itext-read-and-write-pdf-in-java\/\">https:\/\/mkyong.com\/java\/itext-read-and-write-pdf-in-java\/<\/a><\/p>\n\n\n\n<p>How To Read An Existing Pdf File In Java Using IText Jar?<br><a href=\"https:\/\/www.w3schools.blog\/itext-read-pdf-file-in-java\">https:\/\/www.w3schools.blog\/itext-read-pdf-file-in-java<\/a><\/p>\n\n\n\n<p>Difference between getUnderContent and getOverContent of itext<br><a href=\"https:\/\/stackoverflow.com\/questions\/66029258\/difference-between-getundercontent-and-getovercontent-of-itext\">https:\/\/stackoverflow.com\/questions\/66029258\/difference-between-getundercontent-and-getovercontent-of-itext<\/a><\/p>\n\n\n\n<p>Creating PDF Files in Java<br><a href=\"https:\/\/www.baeldung.com\/java-pdf-creation\">https:\/\/www.baeldung.com\/java-pdf-creation<\/a><\/p>\n\n\n\n<p>Tag: PDF<br><a href=\"https:\/\/www.baeldung.com\/tag\/pdf\">https:\/\/www.baeldung.com\/tag\/pdf<\/a><\/p>\n\n\n\n<p>Get Information About a PDF in Java<br><a href=\"https:\/\/www.baeldung.com\/java-pdf-info\">https:\/\/www.baeldung.com\/java-pdf-info<\/a><\/p>\n\n\n\n<p>Using Watermarks with iText in Java<br><a href=\"https:\/\/www.baeldung.com\/java-watermarks-with-itext\">https:\/\/www.baeldung.com\/java-watermarks-with-itext<\/a><\/p>\n\n\n\n<p>java\u751f\u6210PDF\u7684\u51e0\u79cd\u65b9\u6cd5<br><a href=\"https:\/\/www.jianshu.com\/p\/e0a54d5e63c3\">https:\/\/www.jianshu.com\/p\/e0a54d5e63c3<\/a><\/p>\n\n\n\n<p>Using Watermarks in iText 7<br><a href=\"https:\/\/dzone.com\/articles\/using-watermarks-in-itext-7\">https:\/\/dzone.com\/articles\/using-watermarks-in-itext-7<\/a><\/p>\n\n\n\n<p>Custom Metadata entry<br><a href=\"https:\/\/kb.itextpdf.com\/itext\/custom-metadata-entry\">https:\/\/kb.itextpdf.com\/itext\/custom-metadata-entry<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u524d\u6bb5\u65f6\u95f4\u770b\u5230\u4e00\u7bc7\u6587\u7ae0\u60f3\u8d77\u6765\u7684\uff0c\u6700\u8fd1\u6709\u65f6\u95f4\u7b80\u5355\u505a\u4e86\u6d4b\u8bd5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u4ee3\u7801\u548c\u529f\u80fd\u7684\u5b66\u4e60\uff0c\u8fd9\u91cc\u7b80 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[2092,2094,2087,61,1769,2095,2093,2068,2089],"class_list":["post-5578","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-getovercontent","tag-getundercontent","tag-itext","tag-java","tag-metadata","tag-pdfreader","tag-pdfstamper","tag-snippets","tag-2089"],"views":1363,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5578"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5578\/revisions"}],"predecessor-version":[{"id":5579,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5578\/revisions\/5579"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}