多平台统一管理软件接口,如何实现多平台统一管理软件接口
347
2022-11-19
Java pdf和jpg互转案例
pdfbox: jpg转pdf:
/**
* 使用pdfbox将jpg转成pdf
* @param jpgStream jpg输入流
* @param pdfPath pdf文件存储路径
* @throws IOException IOException
*/
public static void jpgToPdf(InputStream jpgStream, String pdfPath) throws IOException {
PDDocument pdDocument = new PDDocument();
BufferedImage image = ImageIO.read(jpgStream);
PDPage pdPage = new PDPage(new PDRectangle(image.getWidth(), image.getHeight()));
pdDocument.addPage(pdPage);
PDImageXObject pdImageXObject = LosslessFactory.createFromImage(pdDocument, image);
PDPageContentStream contentSfLaTyObtream = new PDPageContentStream(pdDocument, pdPage);
contentStream.drawImage(pdImageXObject, 0, 0, image.getWidth(), image.getHeight());
contentStream.close();
pdDocument.save(pdfPath);
pdDocument.close();
}
pdfbox: pdf转jpg:
static void pdfbox() throws IOException {
long start = System.currentTimeMillis();
//pdf路径
URL url = new URL("file:///D:/1.pdf");
InputStream stream = URLUtil.getStream(url);
// 加载解析PDF文件
PDDocument doc = PDDocument.load(stream);
PDFRenderer pdfRenderer = new PDFRenderer(doc);
PDPageTree pages = doc.getPages();
int pageCount = pages.getCount();
for (int i = 0; i < pageCount; i++) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bim, "jpg", os);
byte[] datas = os.toByteArray();
// InputStream is = new ByteArrayInputStream(datas);
//jpg文件转出路径
FileUtil.writeBytes(datas, new File("d:/jpg/" + i + ".jpg"));
}
long end = System.currentTimeMillis();
long time = (end - start) / 1000;
System.out.println(StrUtil.format("pdf转jpg耗时: {}s", time));
}
icepdf: pdf转jpg
Document document = new Document();
document.setUrl(new URL(pdfUrl));
int pageNum = document.getNumberOfPages();
for (int i = 0; i < pageNum; i++) {
// 目前仅支持1对1的pdf->jpg
if (i != 0) {
continue;
}
// 3、pdf -> jpg
BufferedImage bim = (BufferedImage) document.getPageImage(i,
GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX,
rotation, scale);
os = new ByteArrayOutputStream();
ImageIO.write(bim, "jpg", os);
// 4、jpg -> fdfs
byte[] datas = os.toByteArray();
InputStream is = new ByteArrayInputStream(datas);
补充知识:java实现对png图片文件电子签名操作
我就废话不多说了,大家还是直接看代码吧~
/**
* 根据图片像素位置添加用户电子签名
* @param imagePath 要操作的图片路径
* @param signImagePath 电子签名图片路径
* @param outImagePath 合成后输出图片路径
* @param width 像素位宽度
* @param height 像素位高度
*/
public static void syntheticPicture(String imagePath, String signImagePath,Integer width,Integer height, String outImagePath ) {
try {
BufferedImage big = ImageIO.read(new File(imagePath));
BufferedImage small = ImageIO.read(new File(signImagePath));
Graphics2D g = big.createGraphics();
//根据图片像素位置粘贴带电子签名
g.drawImage(small, width, height, small.getWidth(), small.getHeight(), null);
g.dispose();
ImageIO.write(big, outImagePath .split("\\.")[1], new File(outImagePath ));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~