亲宝软件园·资讯

展开

Java pdf jpg互转 Java pdf和jpg互转案例

xqnode 人气:0
想了解Java pdf和jpg互转案例的相关内容吗,xqnode在本文为您仔细讲解Java pdf jpg互转的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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互转案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论