快学scala下载[pdf] pdf转doc java提取pdf(forty day)
生活随笔
收集整理的這篇文章主要介紹了
快学scala下载[pdf] pdf转doc java提取pdf(forty day)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
《快學scala》
鏈接:https://pan.baidu.com/s/1T12_C-cVwfwwJQtbb6Cvdg?
提取碼:teaj?
【注:pdf只有文字的話下面可以使用,《快學scala》轉不了,可能是識別成圖片了?反正我沒成功,有大神會的話可以評論教一下小弟】
1、java提取pdf輸出
import com.itextpdf.text.pdf.PdfDocument; import org.pdfbox.pdfparser.PDFParser; import org.pdfbox.pdmodel.PDDocument; import org.pdfbox.util.PDFTextStripper;import java.io.FileWriter; import java.io.*;public class PdfReader {public static String getTextFromPDF(String pdfFilePath){String result = null;FileInputStream is = null;PDDocument document = null;try {is = new FileInputStream(pdfFilePath);PDFParser parser = new PDFParser(is);parser.parse();document = parser.getPDDocument();PDFTextStripper stripper = new PDFTextStripper();result = stripper.getText(document);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (document != null) {try {document.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return result;}public static void main(String[] args){String str=PdfReader.getTextFromPDF("F:\\51.pdf");System.out.println(str);}}2、java?pdf轉doc
import org.pdfbox.pdmodel.PDDocument; import org.pdfbox.util.PDFTextStripper;import java.io.*;public class PdfToDoc {public PdfToDoc() {}public static void pdfToDoc(String name1) throws IOException {PDDocument doc = PDDocument.load(name1);int pagenumber = doc.getNumberOfPages();name1 = name1.substring(0, name1.lastIndexOf("."));String fileName = name1 + ".doc";createFile(fileName);FileOutputStream fos = new FileOutputStream(fileName);Writer writer = new OutputStreamWriter(fos, "UTF-8");PDFTextStripper stripper = new PDFTextStripper();stripper.setSortByPosition(true);stripper.setStartPage(1);stripper.setEndPage(pagenumber);stripper.writeText(doc, writer);writer.close();doc.close();System.out.println("pdf轉換word成功!");}private static void createDir(String destDirName) {File dir = new File(destDirName);if (dir.exists()) {System.out.println("創建目錄失敗,目標目錄已存在!");}if (!destDirName.endsWith(File.separator)) {destDirName = destDirName + File.separator;}if (dir.mkdirs()) {System.out.println("創建目錄成功!" + destDirName);} else {System.out.println("創建目錄失敗!");}}public static void createFile(String filePath) {File file = new File(filePath);if (file.exists()) {System.out.println("目標文件已存在" + filePath);}if (filePath.endsWith(File.separator)) {System.out.println("目標文件不能為目錄!");}if (!file.getParentFile().exists()) {System.out.println("目標文件所在目錄不存在,準備創建它!");if (!file.getParentFile().mkdirs()) {System.out.println("創建目標文件所在的目錄失敗!");}}try {if (file.createNewFile()) {System.out.println("創建文件成功:" + filePath);} else {System.out.println("創建文件失敗!");}} catch (IOException var3) {var3.printStackTrace();System.out.println("創建文件失敗!" + var3.getMessage());}}public static void main(String[] args) throws Exception {String a = "F:/51.pdf";pdfToDoc(a);}?
總結
以上是生活随笔為你收集整理的快学scala下载[pdf] pdf转doc java提取pdf(forty day)的全部內容,希望文章能夠幫你解決所遇到的問題。