Java 中pdf部分内容加边线_Java 在PDF中添加骑缝章示例解析
騎縫章是用于往來業務合同,以確保合同真實、有效的印章加蓋方法,是一種防范風險的重要方式。在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包:
com.e-iceblue
http://repo.e-iceblue.cn/repository/maven-public/
e-iceblue
spire.pdf.free
2.6.3
(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 main(String[] args) throws IOException {
//加載測試文檔
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("test.pdf");
//獲取分割后的印章圖片
BufferedImage[] images = GetImage(pdf.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().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方法,根據PDF頁數分割印章圖片
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;
}
}
騎縫章添加效果:
到此這篇關于Java 在PDF中添加騎縫章示例解析的文章就介紹到這了,更多相關Java 在PDF中添加騎縫章內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
總結
以上是生活随笔為你收集整理的Java 中pdf部分内容加边线_Java 在PDF中添加骑缝章示例解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 参数类型的多态_【Pyth
- 下一篇: php if 定义变量,无法在PHP中的