目錄
Apache PDFBox是一個開源Java庫,支持PDF檔案的開發和轉換,
引入依賴
業務代碼
讀取網路中的PDF檔案與檔案型別轉換
如果本篇博客對您有一定的幫助,大家記得留言+點贊+收藏哦,
Apache PDFBox是一個開源Java庫,支持PDF檔案的開發和轉換,
有以下中有功能 -
Extract Text - 使用PDFBox,您可以從PDF檔案中提取Unicode文本,
Split & Merge - 使用PDFBox,您可以將單個PDF檔案分成多個檔案,并將它們合并為一個檔案,
Fill Forms - 使用PDFBox,您可以在檔案中填寫表單資料,
Print - 使用PDFBox,您可以使用標準Java列印API列印PDF檔案,
Save as Image - 使用PDFBox,您可以將PDF保存為影像檔案,如PNG或JPEG,
Create PDFs - 使用PDFBox,您可以通過創建Java程式創建新的PDF檔案,還可以包含影像和字體,
Signing - 使用PDFBox,您可以將數字簽名添加到PDF檔案,
有一個教程對PDFBox的介紹很詳細,這里不再多說,
PDFBox - 快速指南_學習PDFbox|WIKI教程此方法接受檔案物件作為引數,因為這是一個靜態方法,您可以使用類名呼叫它,如下所示,. 此方法接受檔案物件作為引數,因為這是一個靜態方法,您可以使用類名呼叫它,如下所示,. 此方法接受檔案物件作為引數,因為這是一個靜態方法,您可以使用類名呼叫它,如下所示,. 此方法接受檔案物件作為引數,因為這是一個靜態方法,您可以使用類名呼叫它,如下所示,. 此方法接受檔案物件作為引數,因為這是一個靜態方法,您可以使用類名呼叫它,如下所示,https://iowiki.com/pdfbox/pdfbox_quick_guide.html
引入依賴
<!--start:PDF獲取第一頁的圖片-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.20</version>
</dependency>
<!--end:PDF獲取第一頁的圖片-->
業務代碼
/**
* 截取PDF中的某一頁作為縮略圖,并上傳(保存)
* @param pdfFileName
* @return
*/
public String PDFFramer(String pdfFileName){
//將網路中的PDF檔案轉換成file
File file = URLToFile(pdfFileName);
//new File() 只能訪問本地檔案
//將本地檔案轉換成file
//File file = new File("C:\\Users\\Administrator\\Downloads\\(重要必看).pdf");
String pdfUrl="";
try
{
// 打開來源 pdf
log.info("開始截取PDF:");
//PDDocument類的load()方法用于加載現有PDF檔案
PDDocument pdfDocument = PDDocument.load(file);
//PDFRenderer的類將PDF檔案呈現為AWT BufferedImage
PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);
// 提取的頁碼
int pageNumber = 0;
// 以300 dpi 讀取存入 BufferedImage 物件
int dpi = 300;
//Renderer類的renderImage()方法在特定頁面中渲染影像
BufferedImage buffImage = pdfRenderer.renderImageWithDPI(pageNumber, dpi, ImageType.RGB);
// 檔案型別轉換
MultipartFile multipartFile = fileCase(buffImage);
log.info("PDF開始上傳:");
pdfUrl = fileLoad(multipartFile);
log.info("PDF上傳成功:{}",pdfUrl);
// 關閉檔案
pdfDocument.close();
//洗掉臨時檔案
String s = threadLocal.get();
log.info("臨時檔案的目錄:"+s);
File f=new File(s);
boolean delete = f.delete();
log.info("檔案是否洗掉"+delete);
}
catch (InvalidPasswordException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return pdfUrl;
}
讀取網路中的PDF檔案與檔案型別轉換
/**
* 讀取網路中的PDF檔案
* @param url
* @return
*/
public File URLToFile(String url){
log.info("讀取FastDFS上的PDF");
//保存臨時檔案--jar包的相對位置
File file1 = new File("Temporary.pdf");
try {
URL url1 = new URL(url);
FileUtils.copyURLToFile(url1,file1);
} catch (IOException e) {
e.printStackTrace();
}
File absoluteFile = file1.getAbsoluteFile();
threadLocal.set(absoluteFile.toString());
log.info("ppt已經存盤到本地"+absoluteFile.toString());
return file1;
}
/**
* 檔案轉換將BufferedImage轉換成MultipartFile:為了檔案上傳
* @param image
* @return
*/
public static MultipartFile fileCase(BufferedImage image){
//得到BufferedImage物件
// BufferedImage bufferedImage = JoinTwoImage.testEncode(200, 200, url);
MultipartFile multipartFile= null;
try {
//創建一個ByteArrayOutputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
//把BufferedImage寫入ByteArrayOutputStream
ImageIO.write(image, "jpg", os);
//ByteArrayOutputStream轉成InputStream
InputStream input = new ByteArrayInputStream(os.toByteArray());
//InputStream轉成MultipartFile
multipartFile =new MockMultipartFile("file", "file.jpg", "text/plain", input);
} catch (IOException e) {
e.printStackTrace();
}
return multipartFile;
}
如果本篇博客對您有一定的幫助,大家記得留言+點贊+收藏哦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/301269.html
標籤:java
