我在我的项目中被分配了这个任务 . 我从服务获取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();
}
}
}
1 回答
我找到了一个解决方案,但renderer.renderImageWithDPI(pageNumber,300)方法需要 page number as method argument ,它可以 convert only one page of PDF at a time . 但我需要以字节数组的形式 full PDf into JPG .