pdf分页分片预览,pdf截取,pdf转图片
生活随笔
收集整理的這篇文章主要介紹了
pdf分页分片预览,pdf截取,pdf转图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?分頁預覽主要解決兩個問題
(1)前端下載整個pdf比較慢。
(2) 前端可以下載整個文件流,不安全。
若要分頁預覽,也有兩種方案:
(1)把pdf 轉(zhuǎn)成一張一張的圖片。
(2)把pdf 再次截取from-to頁,然后生成一個新的pdf文件。
pdf 分頁截取截取生成新的pdf,轉(zhuǎn)圖片 代碼
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.16</version> </dependency> <dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.16</version> </dependency> <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency></dependencies> import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.UUID;import javax.imageio.ImageIO;import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer;import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader;public class PdfToImageUtil {/*** dpi越大轉(zhuǎn)換后越清晰,相對轉(zhuǎn)換速度越慢*/private static final Integer DPI = 100;/*** 轉(zhuǎn)換后的圖片類型*/private static final String IMG_TYPE = "png";public static void main1(String[] args) throws FileNotFoundException, IOException {pdfToImage(new FileInputStream(new File("C:\\Users\\xxx\\Desktop\\temp\\zhuanhuan\\360Teams移動端使用簡介.pdf")));}public static void main(String[] args) throws FileNotFoundException, IOException {partitionPdfFile("C:\\Users\\xxx\\Desktop\\temp\\zhuanhuan\\360Teams移動端使用簡介.pdf", "C:\\Users\\xxx\\Desktop\\temp\\zhuanhuan\\360Teams移動端使用簡介2.pdf", 1, 1); }/*** PDF轉(zhuǎn)圖片 InputStream 和 OutputStream最好都不要落地,直接存儲到s3** @param fileContent PDF文件的二進制流* @return 圖片文件的二進制流*/public static List<String> pdfToImage(InputStream inputStream) throws IOException {List<String> imgKeys = new ArrayList<>();try (PDDocument document = PDDocument.load(inputStream)) {PDFRenderer renderer = new PDFRenderer(document);for (int i = 0; i < document.getNumberOfPages(); ++i) {BufferedImage bufferedImage = renderer.renderImageWithDPI(i, DPI);String imageKey = i + "_" + UUID.randomUUID().toString().replace("-", "");// 自己確定文件key的格式// 需要幾頁,就存幾頁即可。try (OutputStream outputStream = new FileOutputStream("C:\\Users\\xxx\\Desktop\\temp\\zhuanhuan\\" + imageKey + "." + IMG_TYPE)) {imgKeys.add(imageKey);ImageIO.write(bufferedImage, IMG_TYPE, outputStream);} catch (Exception e) {e.printStackTrace();}}}return imgKeys;}/*** PDF轉(zhuǎn)圖片** @param fileContent PDF文件的二進制流* @return 圖片文件的二進制流*/public static List<byte[]> pdfToImage(byte[] fileContent) throws IOException {List<byte[]> result = new ArrayList<>();try (PDDocument document = PDDocument.load(fileContent)) {PDFRenderer renderer = new PDFRenderer(document);for (int i = 0; i < document.getNumberOfPages(); ++i) {BufferedImage bufferedImage = renderer.renderImageWithDPI(i, DPI);ByteArrayOutputStream out = new ByteArrayOutputStream();ImageIO.write(bufferedImage, IMG_TYPE, out);result.add(out.toByteArray());}}return result;}/*** 截取pdf某幾頁,生成一個新的pdf* @param pdfFile* @param newFile* @param from* @param end*/public static void partitionPdfFile(String pdfFile, String newFile, int from, int end) {Document document = null;PdfCopy copy = null;try {PdfReader reader = new PdfReader(pdfFile);int n = reader.getNumberOfPages();if (end == 0) {end = n;}document = new Document(reader.getPageSize(1));copy = new PdfCopy(document, new FileOutputStream(newFile));document.open();for (int j = from; j <= end; j++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, j);copy.addPage(page);}document.close();} catch (Exception e) {e.printStackTrace();}} }總結(jié)
以上是生活随笔為你收集整理的pdf分页分片预览,pdf截取,pdf转图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 获取枚举的描述属性
- 下一篇: 自制宿舍门禁,微信小程序蓝牙控制,比指纹