我已经在我的项目中分配了此任务。我从服务中获取PDF的字节数组,并且必须将其转换为JPG图像的字节数组并返回JPG的字节数组。有人可以帮我吗?
我尝试了以下将PDF字节数组转换为JPG但不返回JPG字节数组的解决方案。
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.pdfbox.util.PDFImageWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
public class DocumentService{
public byte[] convertPDFtoImage(byte[] bytes) {
InputStream targetStream = new ByteArrayInputStream(bytes);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDDocument document = null;
try {
document = PDDocument.load(targetStream);
PDFImageWriter writer = new PDFImageWriter();
writer.writeImage(document, "jpg", null, 1, 2, "C:\\Shailesh\\aaa");
} catch (Exception e) {
log.error(e.getMessage(), e);
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
我找到了一个解决方案,但是renderer.renderImageWithDPI(pageNumber,300)方法采用页码作为方法参数,并且它只能一次转换一页PDF 。但是我需要将字节格式的完整PDf转换为JPG 。
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
public class DocumentService {
public byte[] convertPDFtoImage(byte[] bytesPDF) {
InputStream targetStream = new ByteArrayInputStream(bytesPDF);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDDocument document = null;
try {
document = PDDocument.load(targetStream);
PDFRenderer renderer = new PDFRenderer(document);
int pageNumber = 1;
BufferedImage bi = renderer.renderImageWithDPI(pageNumber, 300);
ImageIO.write(bi, "jpg", baos);
baos.flush();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
if (document != null) {
try {
document.close();
baos.close();
log.info("End convert PDF to Images process");
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return baos.toByteArray();
}
}