骑缝章是用于往来业务合同,以确保合同真实、有效的印章加盖方法,是一种防范风险的重要方式。在Java程序中,可以通过使用工具来辅助加盖这种骑缝章。
工具:Free Spire.PDF for Java (免费版)
工具获取及jar文件导入:
方式1:通过官网下载jar包,并解压,手动导入lib文件夹下的Spire.Pdf.jar文件。
方式2:通过创建Maven程序,在pom.xml中配置maven仓库路径并指定Free Spire.PDF for Java 的依赖,配置完成后,在IDEA中,点击“Import Changes”导入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的导入方法,参考这篇文章)
jar导入结果如下图所示:
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 mainString[] args) throws IOException { //加载测试文档 PdfDocument pdf = new PdfDocument); pdf.loadFromFile"test.pdf"); //获取分割后的印章图片 BufferedImage[] images = GetImagepdf.getPages).getCount)); float x = 0; float y = 0; //实例化PdfUnitConvertor类 PdfUnitConvertor convert = new PdfUnitConvertor); PdfPageBase pageBase; //将图片绘制到PDF页面上的指定位置 for int i = 0; i < pdf.getPages).getCount); i++) { BufferedImage image= images[ i ]; pageBase = pdf.getPages).geti); x = float)pageBase.getSize).getWidth) - convert.convertUnitsimage.getWidth), PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel) + 40; y = float) pageBase.getSize).getHeight)/ 2; pageBase.getCanvas).drawImagePdfImage.fromImageimage), new Point2D.Floatx, y)); } //保存PDF文档 pdf.saveToFile"Result.pdf"); } //定义GetImage方法,根据PDF页数分割印章图片 static BufferedImage[] GetImageint num) throws IOException { String originalImg = "seal.png"; BufferedImage image = ImageIO.readnew FileoriginalImg)); 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 BufferedImagechunkWidth, chunkHeight, image.getType)); Graphics2D gr = imgs[ count++ ].createGraphics); gr.drawImageimage, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, Color.WHITE,null); gr.dispose); } } return imgs; } }
骑缝章添加效果:
(完)