求助!读取PDF文档内容的烦恼!

vamvin_55 2008-07-08 05:59:34
前段时间写了个word文档转化为pdf格式存储在指定路径的小程序
现在问题来了
如何能把转化的pdf文档内容读取出来捏?
我查了好多文章和资料,好像要用到pdfbox这个类库
但是大都说得模糊不清,看得我头晕脑胀
望各位用过的朋友指点一二啊!
ps:最简单的说法就是,怎样读取本地已存在的pdf文件内容。
如果方便的话,麻烦付个代码片段。
谢谢!!!
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
vamvin_55 2008-07-09
  • 打赏
  • 举报
回复

自己顶上一顶
时间比较急
大家帮帮忙啊!
vamvin_55 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 vamvin_55 的帖子:]
前段时间写了个word文档转化为pdf格式存储在指定路径的小程序
现在问题来了
如何能把转化的pdf文档内容读取出来捏?
我查了好多文章和资料,好像要用到pdfbox这个类库
但是大都说得模糊不清,看得我头晕脑胀
望各位用过的朋友指点一二啊!
ps:最简单的说法就是,怎样读取本地已存在的pdf文件内容。
如果方便的话,麻烦付个代码片段。
谢谢!!!
[/Quote]



import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;


public class Test {
public static void main(String[] args)throws Exception {
Document doc=new Document(PageSize.A4.rotate(), 10, 360, 280, 150);
System.out.println("test");


try {
File read = new File("c:\\java001.doc");
BufferedReader br = new BufferedReader(new FileReader(read));
PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf"));
doc.open();
System.out.println("test1");
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
String content="";
String temp="";
while((temp=br.readLine())!=null){
content+=temp+"\r\n";
if(content.length()>400){
doc.add(new Paragraph(content,FontChinese));
content="";
}
else {
doc.add(new Paragraph(temp,FontChinese));
}
}
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("test2");
String PATH_TO_XPDF="E:\\xpdf\\pdftotext.exe";
String filename="c:\\hello.pdf";
System.out.println("test3");
String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
Process p = Runtime.getRuntime().exec(cmd);
System.out.println("test4");
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
StringWriter out = new StringWriter();
System.out.println("test5");
char [] buf = new char[10000];
int len;
while((len = reader.read(buf))>= 0) {
// out.write(buf, 0, len);
System.out.println("the length is"+len);
System.out.println("test"+ );
}
reader.close();
String ts=new String(buf);
System.out.println("the str is"+ts);
}
}


以上是简单代码
请教下,这个代码不能实现PDF输出
错在那里呢。

oraclelogan 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 vamvin_55 的帖子:]
前段时间写了个word文档转化为pdf格式存储在指定路径的小程序
现在问题来了
如何能把转化的pdf文档内容读取出来捏?
我查了好多文章和资料,好像要用到pdfbox这个类库
但是大都说得模糊不清,看得我头晕脑胀
望各位用过的朋友指点一二啊!
ps:最简单的说法就是,怎样读取本地已存在的pdf文件内容。
如果方便的话,麻烦付个代码片段。
谢谢!!!
[/Quote]

先顶下,留个记号。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧