温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java pdf和jpg互转案例

发布时间:2020-10-06 22:17:43 来源:脚本之家 阅读:172 作者:xqnode 栏目:开发技术

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 contentStream = 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);
  }
}

以上这篇Java pdf和jpg互转案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI