c语言sscanf函数的用法是什么
244
2023-01-01
Java实现pdf转图片案例
工程加入依赖:
pdf文件转图片:
public static List
if (pdfFile == null || !pdfFile.exists()) {
throw new RuntimeException("pdf文件不能为空");
}
String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
String targetPath = pdfFile.getParent() + File.separator + name;
List
try {
PDDocument doc = PDDocument.load(pdfFile);
// 页数
int pageCount = doc.getNumberOfPages();
PDFRenderer pdfRenderer = new PDFRenderer(doc);
for (int i = 0; i < pageCount; i++) {
File targetFile = new File(targetPath + File.separator + name + "-" + (i + 1) + ".jpg");
if (!targetFile.getParentFile().exists()) {
FileUtil.mkdir(targetFile.getParentFile());
}
pdfRenderer.renderImage(i);
BufferedImage image = pdfRenderer.renderImageWithDPI(i, 105, ImageType.RGB);
ImageIOUtil.writeImage(image, targetFile.getPath(), 105);
imgList.add(targetFile.getPath());
}
} catch (IOException e) {
log.error("文件转换异常", e);
throw new RuntimeException("文件转换异常,err=" + e.getMessage());
}
pdf转成一张图片:
/**
* pdf转成一张图片
*
* @param pdfFile pdf图片文件
* @return 图片地址
*/
public static String pdf2OneImg(File pdfFile) {
List
int len = imgs.size();
File[] src = new File[len];
BufferedImage[] images = new BufferedImage[len];
JrRPd int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
try {
src[i] = new File(imgs.get(i));
if (!src[i].exists()) {
throw new RuntimeException("文件【" + imgs.get(i) + "】不存在");
}
images[i] = ImageIO.read(src[i]);
} catch (Exception e) {
log.error("", e);
throw new RuntimeException(e);
}
int width = images[i].getWidth();
int height = images[i].getHeight();
// 从图片中读取RGB 像素
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}
int dst_height = 0;
int dst_width = images[0].getWidth();
// 合成图片像素
for (int i = 0; i < images.length; i++) {
dst_width = http://dst_width > images[i].getWidth() ? dst_width : images[i].getWidth();
dst_height += images[i].getHeight();
}
if (dst_height < 1) {
throw new RuntimeException("文件合成失败,合成后的图片文件高度=" + dst_height);
}
String name = pdfFile.getName().substring(0, pdfFile.getName().lastIndexOf("."));
String targetPath = pdfFile.getParent() + File.separator + name;
// 输出路径
File outFile = new File(targetPath + File.separator + name + "-bigone.jpg");
// 生成新图片
try {
dst_width = images[0].getWidth();
BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width);
height_i += images[i].getHeight();
}
ImageIO.write(ImageNew, "jpg", outFile);
} catch (Exception e) {
log.error("图片合并异常=", e);
throw new RuntimeException(e);
}
return outFile.getPath();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~