【知識點】Java機密

Java添加PDF圖章、動態圖章



主要實現如下功能:html

    1. 添加圖片圖章。即經過加載現有的圖章(以圖片形式),添加到PDF指定頁面位置
    2. 添加動態圖章。即加載PDF文檔,並在動態的添加印章內容,包括印章字樣、日期、時間、經辦人、組織名稱等。
 

具體實現過程


首先,準備要加圖章(1.png)以及所加的文件(main.pdf、test.pdf),其次須要一些外部.jar包(Spire.Pdf.jar).

下載地址:Free Spire.PDF for Java v2.4.4(免費版)

添加圖章



實現代碼以下:
package org.zxd.com;

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) {

//建立PdfDocument對象,加載PDF測試文檔
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");

//獲取文檔第1頁
PdfPageBase page = doc.getPages().get(0);

//加載印章圖片
PdfImage image = PdfImage.fromFile("1.png");
//獲取印章圖片的寬度和高度
int width = image.getWidth();
int height = image.getHeight();

//建立PdfTemplate對象
PdfTemplate template = new PdfTemplate(width, height);
//將圖片繪製到模板
template.getGraphics().drawImage(image, 0, 0, width, height);

//建立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);

//建立PdfAppearance對象
PdfAppearance pdfAppearance = new PdfAppearance(stamp);
//將模板應用爲PdfAppearance的通常狀態
pdfAppearance.setNormal(template);
//將PdfAppearance 應用爲圖章的樣式
stamp.setAppearance(pdfAppearance);

//添加圖章到PDF
page.getAnnotationsWidget().add(stamp);

//保存文檔
doc.saveToFile("main.pdf",FileFormat.PDF);
}
}

運行截圖:


 添加動態圖章


實現代碼以下:
package org.zxd.com;

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) {

//建立PdfDocument對象
PdfDocument document = new PdfDocument();

//加載PDF文檔
document.loadFromFile("test.pdf");

//獲取第1頁
PdfPageBase page = document.getPages().get(0);

//建立PdfTamplate對象
PdfTemplate template = new PdfTemplate(185, 50);

//建立兩種字體
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);

//建立畫刷
PdfSolidBrush solidBrush = new PdfSolidBrush(new PdfRGBColor(Color.red));
Rectangle2D rect1 = new Rectangle2D.Float();
rect1.setFrame(new Point2D.Float(0,0),template.getSize());

//建立圓角矩形路徑
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.getRed(), path);

//在模板上繪製文字及動態日期
String str1 = "Audited\n";
String str2 = "Blog Center " + dateToString(new java.util.Date(),"yyyy-MM-dd HH:mm:ss");
template.getGraphics().drawString(str1, font1, solidBrush, new Point2D.Float(5, 5));
template.getGraphics().drawString(str2, font2, solidBrush, new Point2D.Float(5, 28));

//建立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);

//建立PdfAppearance對象,應用模板爲通常狀態
PdfAppearance appearance = new PdfAppearance(stamp);
appearance.setNormal(template);

//應用樣式到圖章
stamp.setAppearance(appearance);

//添加圖章到Annotation集合
page.getAnnotationsWidget().add(stamp);

//保存文檔
document.saveToFile("javatest.pdf");
document.close();
}

//將日期轉化成字符串
public static String dateToString(java.util.Date poDate,String pcFormat) {
SimpleDateFormat loFormat = new SimpleDateFormat(pcFormat);
return loFormat.format(poDate);
}
}

運行結果:




轉載請標明出處!!!

相關文章
相關標籤/搜索