小編給大家分享一下Java如何處理PDF圖章,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

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

步驟2:復制文件后,添加到引用類庫:選中這兩個jar文件,點擊鼠標右鍵,選擇“Build Path” – “Add to Build Path”。完成引用。

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對象,加載PDF測試文檔
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
//獲取文檔第3頁
PdfPageBase page = doc.getPages().get(2);
//加載印章圖片
PdfImage image = PdfImage.fromFile("stamp.png");
//獲取印章圖片的寬度和高度
int width = image.getWidth();
int height = image.getHeight();
//創(chuàng)建PdfTemplate對象
PdfTemplate template = new PdfTemplate(width, height);
//將圖片繪制到模板
template.getGraphics().drawImage(image, 0, 0, width, height);
//創(chuàng)建PdfRubebrStampAnnotation對象,指定大小和位置
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對象
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);
}
}圖片圖章添加效果:

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對象
PdfDocument document = new PdfDocument();
//加載PDF文檔
document.loadFromFile("test.pdf");
//獲取第3頁
PdfPageBase page = document.getPages().get(2);
//創(chuàng)建PdfTamplate對象
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)建畫刷
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);
//繪制路徑到模板,并進行填充
template.getGraphics().drawPath(PdfPens.getBlue(), path);
//在模板上繪制文字及動態(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對象,并指定其位置和大小
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對象,應(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);
}
}動態(tài)圖章添加效果:

以上是Java如何處理PDF圖章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文標題:Java如何處理PDF圖章-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://chinadenli.net/article32/shspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、營銷型網(wǎng)站建設(shè)、企業(yè)建站、全網(wǎng)營銷推廣、定制開發(fā)、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)