文件管理 · 2024年1月7日

java读取pdf文件流|java 如何读取PDF文件内容

㈠ java读取pdf文件的时候出现异常

ava.lang.NoClassDefFoundError: org/fontbox/cmap/CMapParser 很明显了少了一个jar包,从了、org/fontbox/cmap/CMapParser来看,jar包名字可能带fontbox的名字,你在网络回上检索下 fontbox .jar试试能不能找到答这个jar包呢?

㈡ Java 如何实现打开PDF文件,并把文件显示出来;就是实现文件打开的功能。急,急….

java打开PDF需要借助其他的jar包的,如果我没记错的话,然后查看相应的API接口内。应该是以容流的形式进行读取,之前写过一点是往里面写数据的,生成html文然后写入PDF文件 工具有:ItextPdf、FlyingAndItext、pd4ml 这些是将html文转换为PDF文件的,读取的话直接用bufferread读取试试(我没写过!)

㈢ java中 如何将存放在数据库中的pdf、doc、jpg等文件读出来(二进制形式存放在数据)

在数据库中存放这些个二进制文件的字段是BLOB,oracle和MysqL里面都是java中读取 BLOB数据:首先做查询,拿到查询结果ResultSet rs = XXXX (和普通数据查询一样)然后:Blob blob = rs.getBlob("字段名"); 拿到你的Blob ,得到文件的二进制流:InputStream binaryStream= blob.getBinaryStream();,你的文件数据就在这个流当中,你想怎么用就怎么取,比如,读出来存到一个byte[]中,以便序列化传输,读出来构造成一个File直接存放到本地等等。举个例子吧:从这个binaryStream中读取数据到byte[]的方法,////////———————/** * 从binaryStream中读取数据到byte[]的方法 * @param in 即binaryStream * @return * @throws Exception */ public static byte[] readStreamToByteArray(InputStream in) throws Exception{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while((len = in.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.close(); in.close(); return outputStream.toByteArray(); } //

㈣ 用java读取pdf

可以使用PDFBOX0.7.3控件:import java.io.InputStream;import java.io.IOException;import org.apache.lucene.document.Document;import org.pdfbox.cos.COSDocument;import org.pdfbox.pdfparser.PDFParser;import org.pdfbox.pdmodel.PDDocument;import org.pdfbox.pdmodel.PDDocumentInformation;import org.pdfbox.util.PDFTextStripper;import com.search.code.Index;public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException {COSDocument cosDoc = null;<br/> try {cosDoc = parseDocument(is);<br/> } catch (IOException e) { closeCOSDocument(cosDoc); throw new DocCenterException("无法处理该PDF文档", e); } if (cosDoc.isEncrypted()) { if (cosDoc != null) closeCOSDocument(cosDoc); throw new DocCenterException("该PDF文档是加密文档,无法处理"); } String docText = null; try { PDFTextStripper stripper = new PDFTextStripper(); docText = stripper.getText(new PDDocument(cosDoc)); } catch (IOException e) { closeCOSDocument(cosDoc); throw new DocCenterException("无法处理该PDF文档", e); } PDDocument pdDoc = null; try {pdDoc = new PDDocument(cosDoc);<br/> PDDocumentInformation docInfo = pdDoc.getDocumentInformation();<br/> if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){<br/> title = docInfo.getTitle();} } catch (Exception e) { closeCOSDocument(cosDoc); closePDDocument(pdDoc); System.err.println("无法取得该PDF文档的元数据" + e.getMessage()); } finally { closeCOSDocument(cosDoc); closePDDocument(pdDoc); } return null;}private static COSDocument parseDocument(InputStream is) throws IOException { PDFParser parser = new PDFParser(is);parser.parse();return parser.getDocument();}private void closeCOSDocument(COSDocument cosDoc) { if (cosDoc != null) {try {cosDoc.close();} catch (IOException e) {} }}private void closePDDocument(PDDocument pdDoc) { if (pdDoc != null) { try { pdDoc.close(); } catch (IOException e) {}}}

㈤ java如何读取某个网址下的pdf文件

是不是没加载完毕啊?new PDFParser前先检查下文件大小

㈥ 怎样用JAVA编程实现读取PDF文件中的文字或英文保存到TXT文档中,不使用第三方jar包。

1、创建一个路径为要读取的txt文件的file对象rFile。2、创建一个路径为要写入的txt文件的file对象wFile。3、创建一个FileReader对象,传入rFile到构造器。4、准备一个char数组,FileReader类有一个继承自java.io.Reader的read(char[]cbuf)方法,将字符读入数组。5、创建一个FileWriter对象,传入wFile到构造器。6、FileWriter类有一个继承自java.io.Writer的write(char[]cbuf)方法,可以写入字符数组。7、最后别忘了关闭流。

㈦ java 如何读取PDF文件内容

import java.io.File;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.net.MalformedURLException;import java.net.URL;import org.pdfbox.pdmodel.PDDocument;import org.pdfbox.util.PDFTextStripper;public class PdfReader {public void readFdf(String file) throws Exception {// 是否排序boolean sort = false;// pdf文件名String pdfFile = file;// 输入文本文件名称String textFile = null;// 编码方式String encoding = "UTF-8";// 开始提取页数int startPage = 1;// 结束提取页数int endPage = Integer.MAX_VALUE;// 文件输入流,生成文本文件Writer output = null;// 内存中存储的PDF DocumentPDDocument document = null;try {try {// 首先当作一个URL来装载文件,如果得到异常再从本地文件系统//去装载文件URL url = new URL(pdfFile);//注意参数已不是以前版本中的URL.而是File。document = PDDocument.load(pdfFile);// 获取PDF的文件名String fileName = url.getFile();// 以原来PDF的名称来命名新产生的txt文件if (fileName.length() > 4) {File outputFile = new File(fileName.substring(0, fileName.length() – 4)+ ".txt");textFile = outputFile.getName();}} catch (MalformedURLException e) {// 如果作为URL装载得到异常则从文件系统装载//注意参数已不是以前版本中的URL.而是File。document = PDDocument.load(pdfFile);if (pdfFile.length() > 4) {textFile = pdfFile.substring(0, pdfFile.length() – 4)+ ".txt";}}// 文件输入流,写入文件倒textFileoutput = new OutputStreamWriter(new FileOutputStream(textFile),encoding);// PDFTextStripper来提取文本PDFTextStripper stripper = null;stripper = new PDFTextStripper();// 设置是否排序stripper.setSortByPosition(sort);// 设置起始页stripper.setStartPage(startPage);// 设置结束页stripper.setEndPage(endPage);// 调用PDFTextStripper的writeText提取并输出文本stripper.writeText(document, output);} finally {if (output != null) {// 关闭输出流output.close();}if (document != null) {// 关闭PDF Documentdocument.close();}}}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubPdfReader pdfReader = new PdfReader();try {// 取得E盘下的SpringGuide.pdf的内容pdfReader.readFdf("E://SpringGuide.pdf");} catch (Exception e) {e.printStackTrace();}}}

㈧ 怎么用java读取pdf中的表格

ITEXT插件方法/*** @param pdf PDF文件路径* @param txt 输出文本文件路径* @throws IOException*/public void parsePdf(String pdf, String txt) throws IOException {PdfReader reader = new PdfReader(pdf);PrintWriter out = new PrintWriter(new FileOutputStream(txt));Rectangle rect = new Rectangle(70, 80, 490, 580);RenderFilter filter = new RegionTextRenderFilter(rect);TextExtractionStrategy strategy;for (int i = 1; i <= reader.getNumberOfPages(); i++) {strategy = new FilteredTextRenderListener(new (), filter);out.println(PdfTextExtractor.getTextFromPage(reader, i, strategy));}out.flush();out.close();reader.close();}PDFBOX插件方法PDDocument document = PDDocument.load( args[0] );if( document.isEncrypted() ){document.decrypt( "" );}PDFTextStripperByArea stripper = new PDFTextStripperByArea();stripper.setSortByPosition( true );Rectangle rect = new Rectangle( 10, 280, 275, 60 );stripper.addRegion( "class1", rect );List allPages = document.getDocumentCatalog().getAllPages();PDPage firstPage = (PDPage)allPages.get( 0 );stripper.extractRegions( firstPage );System.out.println( "Text in the area:" + rect );System.out.println( stripper.getTextForRegion( "class1" ) );