Java 在PDF中添加騎縫章示例解析
騎縫章是用于往來(lái)業(yè)務(wù)合同,以確保合同真實(shí)、有效的印章加蓋方法,是一種防范風(fēng)險(xiǎn)的重要方式。在Java程序中,可以通過(guò)使用工具來(lái)輔助加蓋這種騎縫章。
工具:Free Spire.PDF for Java (免費(fèi)版)
工具獲取及jar文件導(dǎo)入:
方式1:通過(guò)官網(wǎng)下載jar包,并解壓,手動(dòng)導(dǎo)入lib文件夾下的Spire.Pdf.jar文件。
方式2:通過(guò)創(chuàng)建Maven程序,在pom.xml中配置maven倉(cāng)庫(kù)路徑并指定Free Spire.PDF for Java 的依賴(lài),配置完成后,在IDEA中,點(diǎn)擊“Import Changes”導(dǎo)入JAR包:
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository></repositories><dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>2.6.3</version> </dependency></dependencies>
(Ecllipse的導(dǎo)入方法,參考這篇文章)
jar導(dǎo)入結(jié)果如下圖所示:
Java 示例
import com.spire.pdf.*;import com.spire.pdf.graphics.PdfGraphicsUnit;import com.spire.pdf.graphics.PdfImage;import com.spire.pdf.graphics.PdfUnitConvertor;import javax.imageio.ImageIO;import java.awt.*;import java.awt.geom.Point2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;public class AcrossPageSeal { public static void main(String[] args) throws IOException { //加載測(cè)試文檔 PdfDocument pdf = new PdfDocument(); pdf.loadFromFile('test.pdf'); //獲取分割后的印章圖片 BufferedImage[] images = GetImage(pdf.getPages().getCount()); float x = 0; float y = 0; //實(shí)例化PdfUnitConvertor類(lèi) PdfUnitConvertor convert = new PdfUnitConvertor(); PdfPageBase pageBase; //將圖片繪制到PDF頁(yè)面上的指定位置 for (int i = 0; i < pdf.getPages().getCount(); i++) { BufferedImage image= images[ i ]; pageBase = pdf.getPages().get(i); x = (float)pageBase.getSize().getWidth() - convert.convertUnits(image.getWidth(), PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel) + 40; y = (float) pageBase.getSize().getHeight()/ 2; pageBase.getCanvas().drawImage(PdfImage.fromImage(image), new Point2D.Float(x, y)); } //保存PDF文檔 pdf.saveToFile('Result.pdf'); } //定義GetImage方法,根據(jù)PDF頁(yè)數(shù)分割印章圖片 static BufferedImage[] GetImage(int num) throws IOException { String originalImg = 'seal.png'; BufferedImage image = ImageIO.read(new File(originalImg)); int rows = 1; int cols = num; int chunks = rows * cols; int chunkWidth = image.getWidth() / cols; int chunkHeight = image.getHeight() / rows; int count = 0; BufferedImage[] imgs = new BufferedImage[ chunks ]; for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { imgs[ count ] = new BufferedImage(chunkWidth, chunkHeight, image.getType()); Graphics2D gr = imgs[ count++ ].createGraphics(); gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, Color.WHITE,null); gr.dispose(); } } return imgs; }}
騎縫章添加效果:
到此這篇關(guān)于Java 在PDF中添加騎縫章示例解析的文章就介紹到這了,更多相關(guān)Java 在PDF中添加騎縫章內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. IntelliJ IDEA導(dǎo)入jar包的方法2. SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果3. 刪除docker里建立容器的操作方法4. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法5. 簡(jiǎn)單了解Python字典copy與賦值的區(qū)別6. Java源碼解析之ClassLoader7. Python如何測(cè)試stdout輸出8. Java導(dǎo)出Execl疑難點(diǎn)處理的實(shí)現(xiàn)9. 解決python DataFrame 打印結(jié)果不換行問(wèn)題10. .Net中的Http請(qǐng)求調(diào)用詳解(Post與Get)
