實(shí)例講解Java處理PDF圖章的方法
圖章(印章)是一種在合同、票據(jù)、公文等文件中表明法律效應(yīng)、部門機(jī)關(guān)權(quán)威的重要指示物,常見(jiàn)于各種格式的文件、文檔中。對(duì)于紙質(zhì)文檔可以手動(dòng)蓋章,但對(duì)于電子文檔,則需要通過(guò)特定的方法來(lái)實(shí)現(xiàn)。本篇文檔分享通過(guò)Java代碼在PDF文檔中添加圖章的方法。內(nèi)容將分兩部分介紹:
1. 添加圖片圖章。即通過(guò)加載現(xiàn)有的圖章(以圖片形式),添加到PDF指定頁(yè)面位置
2. 添加動(dòng)態(tài)圖章。即加載PDF文檔,并在動(dòng)態(tài)的添加印章內(nèi)容,包括印章字樣、日期、時(shí)間、經(jīng)辦人、組織名稱等。
使用工具:Free Spire.PDF for Java v2.0.0
關(guān)于jar文件導(dǎo)入:
步驟1:步驟1:在Java程序中新建一個(gè)文件夾可命名為L(zhǎng)ib。并將產(chǎn)品包中的2個(gè)jar文件復(fù)制到新建的文件夾下。

步驟2:復(fù)制文件后,添加到引用類庫(kù):選中這兩個(gè)jar文件,點(diǎn)擊鼠標(biāo)右鍵,選擇“Build Path” – “Add to Build Path”。完成引用。

Java示例(供參考)
1. 添加圖片圖章
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.geom.Rectangle2D;
public class ImageStamp {
public static void main(String[] args) {
//創(chuàng)建PdfDocument對(duì)象,加載PDF測(cè)試文檔
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
//獲取文檔第3頁(yè)
PdfPageBase page = doc.getPages().get(2);
//加載印章圖片
PdfImage image = PdfImage.fromFile("stamp.png");
//獲取印章圖片的寬度和高度
int width = image.getWidth();
int height = image.getHeight();
//創(chuàng)建PdfTemplate對(duì)象
PdfTemplate template = new PdfTemplate(width, height);
//將圖片繪制到模板
template.getGraphics().drawImage(image, 0, 0, width, height);
//創(chuàng)建PdfRubebrStampAnnotation對(duì)象,指定大小和位置
Rectangle2D rect = new Rectangle2D.Float((float) (page.getActualSize().getWidth() - width - 10), (float) (page.getActualSize().getHeight() - height - 60), width, height);
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);
//創(chuàng)建PdfAppearance對(duì)象
PdfAppearance pdfAppearance = new PdfAppearance(stamp);
//將模板應(yīng)用為PdfAppearance的一般狀態(tài)
pdfAppearance.setNormal(template);
//將PdfAppearance 應(yīng)用為圖章的樣式
stamp.setAppearance(pdfAppearance);
//添加圖章到PDF
page.getAnnotationsWidget().add(stamp);
//保存文檔
doc.saveToFile("ImageStamp.pdf",FileFormat.PDF);
}
}
圖片圖章添加效果:

2.添加動(dòng)態(tài)圖章
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
public class DynamicStamp {
public static void main(String[] args) {
//創(chuàng)建PdfDocument對(duì)象
PdfDocument document = new PdfDocument();
//加載PDF文檔
document.loadFromFile("test.pdf");
//獲取第3頁(yè)
PdfPageBase page = document.getPages().get(2);
//創(chuàng)建PdfTamplate對(duì)象
PdfTemplate template = new PdfTemplate(185, 50);
//創(chuàng)建兩種字體
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN ,15), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN ,10), true);
//創(chuàng)建畫(huà)刷
PdfSolidBrush solidBrush = new PdfSolidBrush(new PdfRGBColor(Color.blue));
Rectangle2D rect1 = new Rectangle2D.Float();
rect1.setFrame(new Point2D.Float(0,0),template.getSize());
//創(chuàng)建圓角矩形路徑
int CornerRadius = 20;
PdfPath path = new PdfPath();
path.addArc(template.getBounds().getX(), template.getBounds().getY(), CornerRadius, CornerRadius, 180, 90);
path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius,template.getBounds().getY(), CornerRadius, CornerRadius, 270, 90);
path.addArc(template.getBounds().getX() + template.getWidth() - CornerRadius, template.getBounds().getY()+ template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 0, 90);
path.addArc(template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, CornerRadius, CornerRadius, 90, 90);
path.addLine( template.getBounds().getX(), template.getBounds().getY() + template.getHeight() - CornerRadius, template.getBounds().getX(), template.getBounds().getY() + CornerRadius / 2);
//繪制路徑到模板,并進(jìn)行填充
template.getGraphics().drawPath(PdfPens.getBlue(), path);
//在模板上繪制文字及動(dòng)態(tài)日期
String s1 = "已審核\n";
String s2 = "社區(qū)管理中心 " + dateToString(new java.util.Date(),"yyyy-MM-dd HH:mm:ss");
template.getGraphics().drawString(s1, font1, solidBrush, new Point2D.Float(5, 5));
template.getGraphics().drawString(s2, font2, solidBrush, new Point2D.Float(5, 28));
//創(chuàng)建PdfRubberStampAnnotation對(duì)象,并指定其位置和大小
Rectangle2D rect2= new Rectangle2D.Float();
rect2.setFrame(new Point2D.Float((float)(page.getActualSize().getWidth()-250),(float)(page.getActualSize().getHeight()-150)), template.getSize());
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect2);
//創(chuàng)建PdfAppearance對(duì)象,應(yīng)用模板為一般狀態(tài)
PdfAppearance appearance = new PdfAppearance(stamp);
appearance.setNormal(template);
//應(yīng)用樣式到圖章
stamp.setAppearance(appearance);
//添加圖章到Annotation集合
page.getAnnotationsWidget().add(stamp);
//保存文檔
document.saveToFile("DynamicStamp.pdf");
document.close();
}
//將日期轉(zhuǎn)化成字符串
public static String dateToString(java.util.Date poDate,String pcFormat) {
SimpleDateFormat loFormat = new SimpleDateFormat(pcFormat);
return loFormat.format(poDate);
}
}
動(dòng)態(tài)圖章添加效果:

相關(guān)文章
Java按時(shí)間梯度實(shí)現(xiàn)異步回調(diào)接口的方法
這篇文章主要介紹了Java按時(shí)間梯度實(shí)現(xiàn)異步回調(diào)接口,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Java實(shí)現(xiàn)注冊(cè)登錄跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)注冊(cè)登錄跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Java數(shù)據(jù)結(jié)構(gòu)之棧的詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之棧簡(jiǎn)單操作的相關(guān)資料,需要的朋友可以參考下,希望能夠給你帶來(lái)幫助2021-09-09
利用Java如何獲取Mybatis動(dòng)態(tài)生成的sql接口實(shí)現(xiàn)
MyBatis 的強(qiáng)大特性之一便是它的動(dòng)態(tài)SQL,下面這篇文章主要給大家介紹了關(guān)于利用Java如何獲取Mybatis動(dòng)態(tài)生成的sql接口實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
SpringBoot中SmartLifecycle的使用解析
這篇文章主要介紹了SpringBoot中SmartLifecycle的使用解析,SmartLifecycle是一個(gè)擴(kuò)展了Lifecycle接口,可以跟蹤spring容器ApplicationContext刷新或者關(guān)閉的接口,實(shí)現(xiàn)該接口的實(shí)現(xiàn)類有特定的執(zhí)行順序,需要的朋友可以參考下2023-11-11
SpringCloud Alibaba使用Seata處理分布式事務(wù)的技巧
在傳統(tǒng)的單體項(xiàng)目中,我們使用@Transactional注解就能實(shí)現(xiàn)基本的ACID事務(wù)了,隨著微服務(wù)架構(gòu)的引入,需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行分庫(kù)分表,每個(gè)服務(wù)擁有自己的數(shù)據(jù)庫(kù),這樣傳統(tǒng)的事務(wù)就不起作用了,那么我們?nèi)绾伪WC多個(gè)服務(wù)中數(shù)據(jù)的一致性呢?跟隨小編一起通過(guò)本文了解下吧2021-06-06
詳解Java中finally和return的執(zhí)行順序
try-catch-finally是一種針對(duì)程序運(yùn)行時(shí)出錯(cuò)的響應(yīng)手段,對(duì)于一些可以預(yù)料到的出錯(cuò)類型,在發(fā)生時(shí)對(duì)其進(jìn)行報(bào)告和補(bǔ)救,這篇文章主要介紹了Java中finally和return的執(zhí)行順序,需要的朋友可以參考下2024-01-01
mybatis plus saveOrUpdate實(shí)現(xiàn)有重復(fù)數(shù)據(jù)就更新,否則新增方式
這篇文章主要介紹了mybatis plus saveOrUpdate實(shí)現(xiàn)有重復(fù)數(shù)據(jù)就更新,否則新增方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
你可能真沒(méi)用過(guò)這些 IDEA 插件(建議收藏)
IDEA 全稱 IntelliJ IDEA,是java編程語(yǔ)言開(kāi)發(fā)的集成環(huán)境。IntelliJ在業(yè)界被公認(rèn)為最好的java開(kāi)發(fā)工具。這篇文章主要介紹 IDEA 必用插件的安裝及用法,需要的朋友可以參考下2020-08-08

