pdf转图片以及内容读取

maven导入包

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>com.sleepycat</groupId>
            <artifactId>je</artifactId>
            <version>5.0.73</version>
        </dependency>
        <dependency>
            <groupId>com.github.jai-imageio</groupId>
            <artifactId>jai-imageio-jpeg2000</artifactId>
            <version>1.3.0</version>
        </dependency>

PdfUtil.java

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;

@Slf4j
public class PdfUtil {

  /**
   * 通过PDFbox获取文章总页数
   *
   * @param filePath:文件路径
   * @return
   * @throws IOException
   */
  public static int getNumberOfPages(String filePath) throws IOException, InterruptedException {
    File file = new File(filePath);
    PDDocument pdDocument = PDDocument.load(new File(filePath));
    int pages = pdDocument.getNumberOfPages();
    pdDocument.close();
    return pages;
  }

  /**
   * 通过PDFbox获取文章内容
   *
   * @param filePath
   * @return
   */
  public static String getContent(String filePath) throws IOException {
    PDFParser pdfParser =
        new PDFParser(new org.apache.pdfbox.io.RandomAccessFile(new File(filePath), "rw"));
    pdfParser.parse();
    PDDocument pdDocument = pdfParser.getPDDocument();
    String text = new PDFTextStripper().getText(pdDocument);
    pdDocument.close();

    return text;
  }

  /**
   * 通过PDFbox生成文件的缩略图
   *
   * @param filePath:文件路径
   * @param outPath:输出图片路径
   * @throws IOException
   */
  public static void getPdfImage(String filePath, String outPath) throws IOException {
    // 利用PdfBox生成图像
    List<String> list = new ArrayList<>();
    PDDocument pdDocument = PDDocument.load(new File(filePath));
    PDFRenderer renderer = new PDFRenderer(pdDocument);

    int pages = pdDocument.getNumberOfPages();
    for (int i = 0; i < pages; i++) {
      // 构造图片
      File file = new File(outPath);
      if (!file.exists()) {
        file.mkdirs();
      }
      String fileName = outPath + "/" + (i + 1) + ".png";
      BufferedImage image = renderer.renderImageWithDPI(i, 96); // Windows native DPI
      ImageIO.write(image, "PNG", new File(fileName));
      list.add(fileName);
    }
    // Warning: You did not close a PDF Document
    pdDocument.close();
  }
}

测试代码

import java.io.IOException;
/**
 * */
public class PdfTest {
  public static void main(String[] args) throws IOException, InterruptedException {
    int numberOfPages =
        PdfUtil.getNumberOfPages("D:\\workspace\\service-api\\xyd-client\\data1.pdf");
    System.out.println(numberOfPages);
    String content = PdfUtil.getContent("D:\\workspace\\service-api\\xyd-client\\data1.pdf");
    System.out.println(content);
    PdfUtil.getPdfImage(
        "D:\\workspace\\service-api\\xyd-client\\data1.pdf",
        "D:\\workspace\\service-api\\xyd-client\\data1.pdf.png");
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用开源库Apache PDFBox来实现将PDF换为图片的功能。PDFBox是一个功能强大的Java库,可以用于处理PDF文件。 以下是使用PDFBox将PDF换为图片的基本步骤: 1. 首先,你需要在你的Java项目中引入PDFBox库。你可以通过在Maven项目的pom.xml文件中添加以下依赖来实现: ```xml <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.26</version> </dependency> ``` 2. 创建一个Java类,并导入所需的类: ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class PDFToImageConverter { public static void main(String[] args) { String pdfFilePath = "path/to/your/pdf/file.pdf"; String outputImagePath = "path/to/save/output/image.png"; try { PDDocument document = PDDocument.load(new File(pdfFilePath)); PDFRenderer pdfRenderer = new PDFRenderer(document); // 遍历每一页并将其换为图像 for (int pageIndex = 0; pageIndex < document.getNumberOfPages(); pageIndex++) { BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 300); // 设置图像分辨率为300dpi ImageIO.write(image, "PNG", new File(outputImagePath)); } document.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码中,你需要将`pdfFilePath`替换为你要换的PDF文件的路径,将`outputImagePath`替换为你要保存输出图像的路径。 3. 运行Java程序,它将读取PDF文件并将每一页换为图像图像将保存在指定的输出路径中。 这就是使用JavaPDFBox库将PDF换为图片的基本过程。你可以根据自己的需求进行进一步的定制和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值