您好,登錄后才能下訂單哦!
圖章(印章)是一種在合同、票據(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)容將分2部分介紹:
使用工具:Free Spire.PDF for Java(免費(fèi)版)
關(guān)于jar文件導(dǎo)入:
方法1:通過(guò)官網(wǎng)下載。下載后,解壓文件,將lib文件夾下的Spire.Pdf.jar文件導(dǎo)入java程序。
方法2:可通過(guò)maven倉(cāng)庫(kù)安裝導(dǎo)入至maven項(xiàng)目。
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)圖章添加效果:
(本文完)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。