Java之生成Pdf并對Pdf內容操作

雖說網上有很多可以在線導出Pdf或者word或者轉成png等格式的工具,但是我覺得還是得了解知道是怎么實現的。一來,在線免費轉換工具,是有容量限制的,達到一定的容量時,是不能成功導出的;二來,業務需求,特別是OA方面的項目,報表不單單只是在線通過瀏覽器登錄對應的站點瀏覽還需有時導出Pdf格式(pdf格式為通用格式,無論是瀏覽器還是其他工具都能打開,因此特別是做項目實施的,除了用word編寫文檔之外,通常還導出一下pdf,這樣一來保證給老板看時,不會因為某種原因打不開文件看不到對應的實質內容。

?

直接開門見山:

一、導入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對象Document document = new Document();// 2.建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中。// 創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E://Pdf//test.pdf"));writer.flush();writer.close();// 3.打開文檔
        document.open();// 4.添加一個內容段落document.add(new Paragraph("Hello World!"));// 5.關閉文檔
        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 {// 創建文件Document document = new Document();// 建立一個書寫器PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("E://Pdf//test2.pdf"));// 打開文件
        document.open();// 添加內容document.add(new Paragraph("Some content here"));// 設置屬性// 標題document.addTitle("this is a title");// 作者document.addAuthor("Mr You");// 主題document.addSubject("this is subject");// 關鍵字document.addKeywords("Keywords");// 創建時間
        document.addCreationDate();// 應用程序document.addCreator("hd.com");// 關閉文檔
        document.close();// 關閉書寫器
        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 {// 創建文件Document document = new Document();// 建立一個書寫器PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("E://Pdf//test3.pdf"));// 打開文件
        document.open();// 添加內容document.add(new Paragraph("HD content here"));// 圖片1Image image1 = Image.getInstance("E://Demo//workspace//java_pdf//src//main//webapp//images//test.png");// 設置圖片位置的x軸和y周
        image1.setAbsolutePosition(100f, 550f);// 設置圖片的寬度和高度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);// 關閉文檔
        document.close();// 關閉書寫器
        writer.close();}
}

?

以上值列舉三個測試代碼,更多可以參考我的github:https://github.com/youcong1996/study_simple_demo.git

記住該示例代碼在我的側分支,分支名為java-pdf。

三、運行測試代碼的最后結果(結果最后是生成pdf,一般控制臺不報錯,就表示OK)

全面示例運行后生成的文件如下圖所示:

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389661.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389661.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389661.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

邊際概率條件概率_數據科學家解釋的邊際聯合和條件概率

邊際概率條件概率Probability plays a very important role in Data Science, as Data Scientist regularly attempt to draw statistical inferences that could be used to predict data or analyse data better.P robability起著數據科學非常重要的作用&#xff0c;為數據科…

1822. 數組元素積的符號

1822. 數組元素積的符號 已知函數 signFunc(x) 將會根據 x 的正負返回特定值&#xff1a; 如果 x 是正數&#xff0c;返回 1 。 如果 x 是負數&#xff0c;返回 -1 。 如果 x 是等于 0 &#xff0c;返回 0 。 給你一個整數數組 nums 。令 product 為數組 nums 中所有元素值的…

java并發編程實戰:第十四章----構建自定義的同步工具

一、狀態依賴性管理 對于單線程程序&#xff0c;某個條件為假&#xff0c;那么這個條件將永遠無法成真在并發程序中&#xff0c;基于狀態的條件可能會由于其他線程的操作而改變1 可阻塞的狀態依賴操作的結構2 3 acquire lock on object state4 while (precondition does not ho…

關于之前的函數式編程

之前寫的函數式編程是我從 JavaScript ES6 函數式編程入門經典這本書里面整理的&#xff0c;然后只在第一篇里專門提到了&#xff0c;后面的話沒有專門提到&#xff0c;而且引用了書中大量的文字&#xff0c;所以我把掘金這里的文章都刪除了&#xff0c;然后在 CSDN 上面每一篇…

袋裝決策樹_袋裝樹是每個數據科學家需要的機器學習算法

袋裝決策樹袋裝樹木介紹 (Introduction to Bagged Trees) Without diving into the specifics just yet, it’s important that you have some foundation understanding of decision trees.尚未深入研究細節&#xff0c;對決策樹有一定基礎了解就很重要。 From the evaluatio…

[JS 分析] 天_眼_查 字體文件

0. 參考 js分析 貓_眼_電_影 字體文件 font-face 1. 分析 1.1 定位目標元素 1.2 查看網頁源代碼 1.3 requests 請求提取得到大量錯誤信息 對比貓_眼_電_影抓取到unicode編碼&#xff0c;天_眼_查混合使用正常字體和自定義字體&#xff0c;難點在于如何從 紅 轉化為 美。 一開始…

深入學習Redis(4):哨兵

前言在 深入學習Redis&#xff08;3&#xff09;&#xff1a;主從復制 中曾提到&#xff0c;Redis主從復制的作用有數據熱備、負載均衡、故障恢復等&#xff1b;但主從復制存在的一個問題是故障恢復無法自動化。本文將要介紹的哨兵&#xff0c;它基于Redis主從復制&#xff0c;…

1805. 字符串中不同整數的數目

1805. 字符串中不同整數的數目 給你一個字符串 word &#xff0c;該字符串由數字和小寫英文字母組成。 請你用空格替換每個不是數字的字符。例如&#xff0c;“a123bc34d8ef34” 將會變成 " 123 34 8 34" 。注意&#xff0c;剩下的這些整數為&#xff08;相鄰彼此至…

經天測繪測量工具包_公共土地測量系統

經天測繪測量工具包部分-鄉鎮第一師 (Sections — First Divisions of Townships) The PLSS Townships are typically divided into 36 Sections (nominally one mile on a side), but in the national standard this feature is called the first division because Townships …

洛谷 P4012 深海機器人問題【費用流】

題目鏈接&#xff1a;https://www.luogu.org/problemnew/show/P4012 洛谷 P4012 深海機器人問題 輸入輸出樣例 輸入樣例#1&#xff1a; 1 1 2 2 1 2 3 4 5 6 7 2 8 10 9 3 2 0 0 2 2 2 輸出樣例#1&#xff1a; 42 說明 題解&#xff1a;建圖方法如下&#xff1a; 對于矩陣中的每…

day5 模擬用戶登錄

_user "yangtuo" _passwd "123456"# passd_authentication False #flag 標志位for i in range(3): #for 語句后面可以跟else&#xff0c;但是不能跟elifusername input("Username:")password input("Password:")if username _use…

opencv實現對象跟蹤_如何使用opencv跟蹤對象的距離和角度

opencv實現對象跟蹤介紹 (Introduction) Tracking the distance and angle of an object has many practical uses, especially in robotics. This tutorial explains how to get an accurate distance and angle measurement, even when the target is at a strong angle from…

spring cloud 入門系列七:基于Git存儲的分布式配置中心--Spring Cloud Config

我們前面接觸到的spring cloud組件都是基于Netflix的組件進行實現的&#xff0c;這次我們來看下spring cloud 團隊自己創建的一個全新項目&#xff1a;Spring Cloud Config.它用來為分布式系統中的基礎設施和微服務提供集中化的外部配置支持&#xff0c;分為服務端和客戶端兩個…

458. 可憐的小豬

458. 可憐的小豬 有 buckets 桶液體&#xff0c;其中 正好 有一桶含有毒藥&#xff0c;其余裝的都是水。它們從外觀看起來都一樣。為了弄清楚哪只水桶含有毒藥&#xff0c;你可以喂一些豬喝&#xff0c;通過觀察豬是否會死進行判斷。不幸的是&#xff0c;你只有 minutesToTest…

熊貓數據集_大熊貓數據框的5個基本操作

熊貓數據集Tips and Tricks for Data Science數據科學技巧與竅門 Pandas is a powerful and easy-to-use software library written in the Python programming language, and is used for data manipulation and analysis.Pandas是使用Python編程語言編寫的功能強大且易于使用…

圖嵌入綜述 (arxiv 1709.07604) 譯文五、六、七

應用 圖嵌入有益于各種圖分析應用&#xff0c;因為向量表示可以在時間和空間上高效處理。 在本節中&#xff0c;我們將圖嵌入的應用分類為節點相關&#xff0c;邊相關和圖相關。 節點相關應用 節點分類 節點分類是基于從標記節點習得的規則&#xff0c;為圖中的每個節點分配類標…

聊聊自動化測試框架

無論是在自動化測試實踐&#xff0c;還是日常交流中&#xff0c;經常聽到一個詞&#xff1a;框架。之前學習自動化測試的過程中&#xff0c;一直對“框架”這個詞知其然不知其所以然。 最近看了很多自動化相關的資料&#xff0c;加上自己的一些實踐&#xff0c;算是對“框架”有…

1971. Find if Path Exists in Graph

1971. Find if Path Exists in Graph 有一個具有 n個頂點的 雙向 圖&#xff0c;其中每個頂點標記從 0 到 n - 1&#xff08;包含 0 和 n - 1&#xff09;。圖中的邊用一個二維整數數組 edges 表示&#xff0c;其中 edges[i] [ui, vi] 表示頂點 ui 和頂點 vi 之間的雙向邊。 …

移動磁盤文件或目錄損壞且無法讀取資料如何找回

文件或目錄損壞且無法讀取說明這個盤的文件系統結構損壞了。在平時如果數據不重要&#xff0c;那么可以直接格式化就能用了。但是有的時候里面的數據很重要&#xff0c;那么就必須先恢復出數據再格式化。具體恢復方法可以看正文了解&#xff08;不格式化的恢復方法&#xff09;…

python 平滑時間序列_時間序列平滑以實現更好的聚類

python 平滑時間序列In time series analysis, the presence of dirty and messy data can alter our reasonings and conclusions. This is true, especially in this domain, because the temporal dependency plays a crucial role when dealing with temporal sequences.在…