发布网友 发布时间:2022-04-21 15:26
共3个回答
热心网友 时间:2022-04-24 08:19
package com.aiait.base.util;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aiait.base.service.impl.base.SearchServiceImpl;
import org.apache.pdfbox.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class PDFUtil {
// logger
private static final Logger lOGGER = LoggerFactory.getLogger(PDFUtil.class);
public static void main(String[] args) {
//pdfTojpg("C://Test//eClaimPDF//1//Others.pdf","C://Test//eClaimPDF//WrittenConfirmation.jpg");
Date timeDiffE = null;
Date timeDiffL = null;
timeDiffE = new Date();
base64PdfToJpg(pdfBase64);
timeDiffL = new Date();
lOGGER.info("base64PdfToJpg use time: " + getDiffTime(timeDiffL, timeDiffE) + "s");
}
private static String base64PdfToJpg(String base64Pdf) {
String jpg_base64 = null;
int pdfdpi = 400;
BASE64Decoder decoder = new BASE64Decoder();
byte[] pdf_bytes = null;
try {
pdf_bytes = decoder.decodeBuffer(base64Pdf);
} catch (IOException e1) {
e1.printStackTrace();
}
try (final PDDocument document = PDDocument.load(pdf_bytes)) {
int size = document.getNumberOfPages();
/*图像合并使用参数*/
// 定义宽度
int width = 0;
// 保存一张图片中的RGB数据
int[] singleImgRGB;
// 定义高度,后面用于叠加
int shiftHeight = 0;
//保存每张图片的像素值
BufferedImage imageResult = null;
// 利用PdfBox生成图像
PDDocument pdDocument = document;
PDFRenderer renderer = new PDFRenderer(pdDocument);
/*根据总页数, 按照50页生成一张长图片的逻辑, 进行拆分*/
// 每50页转成1张图片
int pageLength = size; //有多少转多少
// 总计循环的次数
int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;
for (int m = 0; m < totalCount; m++) {
for (int i = 0; i < pageLength; i++) {
int pageIndex = i + (m * pageLength);
if (pageIndex == pdDocument.getNumberOfPages()) {
System.out.println("m = " + m);
break;
}
// 96为图片的dpi,dpi越大,则图片越清晰,图片越大,转换耗费的时间也越多
BufferedImage image = renderer.renderImageWithDPI(pageIndex, 106, ImageType.RGB);
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
if (i == 0) {
//计算高度和偏移量
//使用第一张图片宽度;
width = imageWidth;
// 保存每页图片的像素值
// 加个判断:如果m次循环后所剩的图片总数小于pageLength,则图片高度按剩余的张数绘制,否则会出现长图片下面全是黑色的情况
if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {
imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);
} else {
imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);
}
} else {
// 将高度不断累加
shiftHeight += imageHeight;
}
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
}
// System.out.println("m = " + m);
File outFile = new File("C://Test//eClaimPDF//WrittenConfirmation.png");
System.out.println(outFile.getName());
// 写图片
ImageIO.write(imageResult, "png", outFile);
// 这个很重要,下面会有说明
shiftHeight = 0;
}
pdDocument.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
ImageIO.write(imageResult, "png", baos);//写入流中
byte[] jpg_Bytes = baos.toByteArray();//转换成字节
BASE64Encoder encoder = new BASE64Encoder();
jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//转换成base64串
jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
// System.out.println("值为:"+"data:image/jpg;base64,"+jpg_base64);
} catch (Exception e) {
e.printStackTrace();
}
return "data:image/jpg;base64,"+jpg_base64;
}
// private static String base64PdfToJpg(String base64Pdf) {
// String jpg_base64 = null;
// int pdfdpi = 400;
//
// BASE64Decoder decoder = new BASE64Decoder();
// byte[] pdf_bytes = null;
//try {
//pdf_bytes = decoder.decodeBuffer(base64Pdf);
//} catch (IOException e1) {
//e1.printStackTrace();
//}
//
// try (final PDDocument document = PDDocument.load(pdf_bytes)) {
// int size = document.getNumberOfPages();
// for (int i = 0; i < size; i++) {
// BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, pdfdpi, ImageType.RGB);
// BufferedImage image = new PDFRenderer(document).
//
// ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
// ImageIO.write(image, "jpg", baos);//写入流中
// byte[] jpg_Bytes = baos.toByteArray();//转换成字节
// BASE64Encoder encoder = new BASE64Encoder();
// jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//转换成base64串
// jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
//
// System.out.println("值为:"+"data:image/jpg;base64,"+jpg_base64);
//
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// return "data:image/jpg;base64,"+jpg_base64;
// }
private static void pdfTojpg(String pdfFilePath, String jpgFilePath) {
File pdfFile = new File(pdfFilePath);
int idx = jpgFilePath.lastIndexOf('.');
String jpgprefix = StringUtils.substring(jpgFilePath, 0, idx);
int pdfdpi = 400;
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = null;
try {
bytes = decoder.decodeBuffer(pdfBase64);
} catch (IOException e1) {
e1.printStackTrace();
}
// try (final PDDocument document = PDDocument.load(pdfFile, "")) {
try (final PDDocument document = PDDocument.load(bytes)) {
int size = document.getNumberOfPages();
for (int i = 0; i < size; i++) {
BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, pdfdpi, ImageType.RGB);
/*
* ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
* ImageIO.write(image, "jpg", baos);//写入流中 byte[] imgBytes =
* baos.toByteArray();//转换成字节 BASE64Encoder encoder = new BASE64Encoder();
* String png_base64 = encoder.encodeBuffer(imgBytes).trim();//转换成base64串
* png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
*
* System.out.println("值为:"+"data:image/jpg;base64,"+png_base64);
*/
File jpgFile = new File(jpgprefix + "_" + i + ".jpg");
ImageIO.write(image, "jpg", jpgFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Double getDiffTime(Date lateTime, Date earlyTime) {
DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
return Double.valueOf(df.format((double)(lateTime.getTime() - earlyTime.getTime()) / 1000));
}
public static String pdfBase64 = "***" //from web 网页链接, upload a PDF to get the base64 string (Base64 - Online Base64 decoder and encoder)
}
热心网友 时间:2022-04-24 09:37
如果桌面没有迅雷快捷方式,可以到开始菜单里的【所有程序】里查找热心网友 时间:2022-04-24 11:12
base64不就是一串字符吗、这还有格式?