Java之生成Pdf并对Pdf内容操作
雖說網(wǎng)上有很多可以在線導(dǎo)出Pdf或者word或者轉(zhuǎn)成png等格式的工具,但是我覺得還是得了解知道是怎么實(shí)現(xiàn)的。一來,在線免費(fèi)轉(zhuǎn)換工具,是有容量限制的,達(dá)到一定的容量時(shí),是不能成功導(dǎo)出的;二來,業(yè)務(wù)需求,特別是OA方面的項(xiàng)目,報(bào)表不單單只是在線通過瀏覽器登錄對(duì)應(yīng)的站點(diǎn)瀏覽還需有時(shí)導(dǎo)出Pdf格式(pdf格式為通用格式,無論是瀏覽器還是其他工具都能打開,因此特別是做項(xiàng)目實(shí)施的,除了用word編寫文檔之外,通常還導(dǎo)出一下pdf,這樣一來保證給老板看時(shí),不會(huì)因?yàn)槟撤N原因打不開文件看不到對(duì)應(yīng)的實(shí)質(zhì)內(nèi)容。
?
直接開門見山:
一、導(dǎo)入Maven依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>site.duanzy</groupId><artifactId>study_pdf</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>study_pdf</name><description /><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.10</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.54</version></dependency><dependency><groupId>javax</groupId><artifactId>javaee-api</artifactId><version>7.0</version><scope>provided</scope></dependency><dependency><groupId>org.glassfish.web</groupId><artifactId>javax.servlet.jsp.jstl</artifactId><version>1.2.2</version></dependency></dependencies><build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>2.2</version><configuration><version>3.1</version><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins></build> </project>?
二、編寫Java代碼
package site.duanzy.pdf.demo;import java.io.FileNotFoundException; import java.io.FileOutputStream;import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter;public class TestPDFDemo1 {public static void main(String[] args) throws FileNotFoundException, DocumentException {// 1.新建document對(duì)象Document document = new Document();// 2.建立一個(gè)書寫器(Writer)與document對(duì)象關(guān)聯(lián),通過書寫器(Writer)可以將文檔寫入到磁盤中。// 創(chuàng)建 PdfWriter 對(duì)象 第一個(gè)參數(shù)是對(duì)文檔對(duì)象的引用,第二個(gè)參數(shù)是文件的實(shí)際名稱,在該名稱中還會(huì)給出其輸出路徑。PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E://Pdf//test.pdf"));writer.flush();writer.close();// 3.打開文檔 document.open();// 4.添加一個(gè)內(nèi)容段落document.add(new Paragraph("Hello World!"));// 5.關(guān)閉文檔 document.close();}}?
package site.duanzy.pdf.demo;import java.io.FileNotFoundException; import java.io.FileOutputStream;import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter;public class TestPDFDemo2 {public static void main(String[] args) throws FileNotFoundException,DocumentException {// 創(chuàng)建文件Document document = new Document();// 建立一個(gè)書寫器PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("E://Pdf//test2.pdf"));// 打開文件 document.open();// 添加內(nèi)容document.add(new Paragraph("Some content here"));// 設(shè)置屬性// 標(biāo)題document.addTitle("this is a title");// 作者document.addAuthor("Mr You");// 主題document.addSubject("this is subject");// 關(guān)鍵字document.addKeywords("Keywords");// 創(chuàng)建時(shí)間 document.addCreationDate();// 應(yīng)用程序document.addCreator("hd.com");// 關(guān)閉文檔 document.close();// 關(guān)閉書寫器 writer.close();} }?
package site.duanzy.pdf.demo;import java.io.FileOutputStream; import java.io.IOException; import java.net.URL;import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter;public class TestPDFDemo3 {public static void main(String[] args) throws DocumentException,IOException {// 創(chuàng)建文件Document document = new Document();// 建立一個(gè)書寫器PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("E://Pdf//test3.pdf"));// 打開文件 document.open();// 添加內(nèi)容document.add(new Paragraph("HD content here"));// 圖片1Image image1 = Image.getInstance("E://Demo//workspace//java_pdf//src//main//webapp//images//test.png");// 設(shè)置圖片位置的x軸和y周 image1.setAbsolutePosition(100f, 550f);// 設(shè)置圖片的寬度和高度image1.scaleAbsolute(200, 200);// 將圖片1添加到pdf文件中 document.add(image1);// 圖片2Image image2 = Image.getInstance(new URL("https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D220/sign=bda2ad09277f9e2f74351a0a2f31e962/0b46f21fbe096b63ea0d41bf0c338744eaf8accc.jpg"));// 將圖片2添加到pdf文件中 document.add(image2);// 關(guān)閉文檔 document.close();// 關(guān)閉書寫器 writer.close();} }?
以上值列舉三個(gè)測試代碼,更多可以參考我的github:https://github.com/youcong1996/study_simple_demo.git
記住該示例代碼在我的側(cè)分支,分支名為java-pdf。
三、運(yùn)行測試代碼的最后結(jié)果(結(jié)果最后是生成pdf,一般控制臺(tái)不報(bào)錯(cuò),就表示OK)
全面示例運(yùn)行后生成的文件如下圖所示:
?
總結(jié)
以上是生活随笔為你收集整理的Java之生成Pdf并对Pdf内容操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两道简单的入门题
- 下一篇: 梦到参加白事是啥意思啊