Java讀寫pdf文件的詳細實現(xiàn)方法
Java讀寫pdf文件
在OA業(yè)務(wù)開發(fā)中,經(jīng)常需要java后臺讀取或生成pdf文件,
itextpdf是一個常用的java操作pdf的開源庫
物料準備:
1.引入itextpdf依賴
2.定義Text2PdfUtil工具類
引入itextpdf相關(guān)的依賴
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency>
定義工具類Text2PdfUtil.java
package cn.ath.knowwikibackend.util; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.parser.PdfTextExtractor; import java.io.*; import java.nio.charset.Charset; /** * 需要引入的依賴 * * <dependency> * <groupId>com.itextpdf</groupId> * <artifactId>itext-asian</artifactId> * <version>5.2.0</version> * </dependency> * * <dependency> * <groupId>com.itextpdf</groupId> * <artifactId>itextpdf</artifactId> * <version>5.5.13</version> * </dependency> * */ public class Text2PdfUtil { /** * txt文本文件 轉(zhuǎn)pdf文件 * @param text F:/data/te616.txt * @param pdf F:/data/aet618.pdf * @throws DocumentException * @throws IOException */ public static void text2pdf(String text,String pdf) throws DocumentException, IOException { Document doc = new Document(); OutputStream os = new FileOutputStream(new File(pdf)); PdfWriter.getInstance(doc, os); doc.open(); //指定 使用內(nèi)置的中文字體 BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); Font font = new Font(baseFont,12,Font.NORMAL); //指定輸出編碼為UTF-8 InputStreamReader isr = new InputStreamReader( new FileInputStream(new File(text)), Charset.forName("UTF-8")); BufferedReader br = new BufferedReader(isr); String str = ""; while((str = br.readLine()) != null){ doc.add(new Paragraph(str,font)); } isr.close(); br.close(); doc.close(); } /** * 讀取pdf文件的內(nèi)容 * @param filename F:/data/aet618.pdf * @return String */ public static String readPDF(String filename){ StringBuilder result = new StringBuilder(); try { PdfReader reader = new PdfReader(filename); int countPage = reader.getNumberOfPages(); for(int i=1;i<=countPage;i++){ result.append(PdfTextExtractor.getTextFromPage(reader, i)); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return result.toString(); } }
測試
@Test void contextLoads() throws DocumentException, IOException { String txtPath = "E:/test617.txt"; String pdfPath = "E:/test617.pdf"; Text2PdfUtil.text2pdf(txtPath,pdfPath); System.out.println(Text2PdfUtil.readPDF(pdfPath)); }
總結(jié)
到此這篇關(guān)于Java讀寫pdf文件的詳細實現(xiàn)方法的文章就介紹到這了,更多相關(guān)Java讀寫pdf文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring 事件監(jiān)聽機制實現(xiàn)跨模塊調(diào)用的思路詳解
之前一個項目,有兩個模塊,A 模塊需要依賴 B 模塊,但現(xiàn)在 B 模塊有地方需要調(diào)用 A 模塊的方法,如果直接依賴,又會產(chǎn)生循環(huán)依賴問題,最終選擇使用 spring 的事件監(jiān)聽來解決該問題,下面給大家介紹Spring 事件監(jiān)聽機制實現(xiàn)跨模塊調(diào)用的思路,感興趣的朋友一起看看吧2024-05-05詳解java中的PropertyChangeSupport與PropertyChangeListener
這篇文章主要介紹了詳解java中的PropertyChangeSupport與PropertyChangeListener的相關(guān)資料,需要的朋友可以參考下2017-09-09支付寶開發(fā)平臺之第三方授權(quán)登錄與獲取用戶信息
本文主要介紹了第三方授權(quán)登錄與獲取用戶信息的實例方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03