1、实现功能:对pdf文件抽取出指定页进行处理
2、问题:spire.pdf免费版不能处理超过10页的文件
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>5.1.0</version>
</dependency>
【截图在公司内部软件上,不能外传,大概就是在文件第11页会显示:
spire.pdf
Tree version is limited to 10 pages of PDF。
This limitation is enforced during loading and creating files.
When converting PDF to image, the first 10 pages of PDF files will be converted to Image format successfully.
Upgrade to Commecial edition of Spire.pdf(http://www.e-icebule.com/Introduce/pdf-for-java.html) 】
上图是商业版的收费价格。
3、解决:
换maven依赖包:itextpdf
实现方法如下:
/**
*pdfBytes文件
*list位置坐标 格式:页数,横坐标,纵坐标,缩放大小 eg:1,100,100,30
*imageBytes待插入图片
*在文件的指定页中插入图片
*/
public static byte[] mergeImageByte(byte[] pdfBytes,List<String> list,byte[] imageBytes) throws Exception{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//读取源文件
PdfReader pdfReader = new PdfReader(pdfBytes);
PdfStamper stamper = new PdfStamper(pdfReader,byteArrayOutputStream);
//pdf页数
int count = pdfReader.getNumberOfPages();
//插入的图片
Image contractImg = Image.getInstance(imageBytes);
for(int i =0;i<list.size();i++){
String str = (String)list.get(i); //如果盖多页则分开处理
String[] split = str.split(","); // 取出一组坐标中的元素
int page = Integer.parseInt(split[0]);
int x = Integer.parseInt(split[1]);
int y = Integer.parseInt(split[2]);
int point = Integer.parseInt(split[3]);
//将图片放在pdf文件的第page页
PdfContentByte pages = stamper.getOverContent(page);
pages.saveState();
PdfGState pdfGState = new PdfGState();
//设置图片的透明度
pdfGState.setFillOpacity(1.2F);
pages.setGState(pdfGState);
//图片缩放
int width = (int)Math.round(contracting.getWidth()*point*0.01);
int height = (int)MAth.round(contracting.getHeight()*point*0.01);
//设置图片在PDF该页中的位置
Rectangle pagesize = pdfReader.getPageSize(idx);
int pageHeight = (int)pagesize.getHeight();
int pageWidth = (int)pageSize.getWidth();
contractImg.setAbsolutePosition(pagewidth-width-x,pageHeight-height-y);
//设置图片的大小
contractImg.scaleAbsolute(width, height);
//将图片添加到pdf文件中
pages.addImage(contractImg);
pages.restoreState();
stamper.setFormFlattening(true);
}
stamper.close();
pdfReader.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}