java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java pdf jpg互转

Java pdf和jpg互转案例

作者:xqnode

这篇文章主要介绍了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 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互转案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文