如何使用Java處理PDF圖章

這篇文章將為大家詳細(xì)講解有關(guān)如何使用Java處理PDF圖章,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)是專業(yè)的乳山網(wǎng)站建設(shè)公司,乳山接單;提供成都網(wǎng)站設(shè)計、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行乳山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!

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文件導(dǎo)入:

步驟1:步驟1:在Java程序中新建一個文件夾可命名為Lib。并將產(chǎn)品包中的2個jar文件復(fù)制到新建的文件夾下。

如何使用Java處理PDF圖章

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

如何使用Java處理PDF圖章

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對象,加載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);

 }

}

圖片圖章添加效果:

如何使用Java處理PDF圖章

2.添加動態(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對象

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

 

  //繪制路徑到模板,并進(jìn)行填充  

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

 }

}

Java是什么

Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。

關(guān)于如何使用Java處理PDF圖章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享名稱:如何使用Java處理PDF圖章
當(dāng)前網(wǎng)址:http://bm7419.com/article42/jcephc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、外貿(mào)建站、App開發(fā)電子商務(wù)、定制開發(fā)、微信小程序

廣告

聲明:本網(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)

網(wǎng)站優(yōu)化排名