阅读在Android中使用iText的PDF文件
我目前正在测试的Android中使用iText的PDF阅读样品,而且我有一个问题。下面的code不显示在Android模拟器什么:
I am currently testing samples of reading PDF using itext in android but i have a problem. The code below does not display anything in android emulator:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AssetManager assetManager = getAssets();
InputStream istr = null;
PdfReader reader=null;
String str= null;
try {
istr =(InputStream) assetManager.open("resume.pdf");
reader=new PdfReader(istr);
str = PdfTextExtractor.getTextFromPage(reader, 1).toString();
//str=reader.getPageContent(1).toString();
}
catch (Exception e)
{
e.printStackTrace();
}
TextView tv = (TextView) findViewById(R.id.txtview);
tv.setText(str);
}
在code是工作,但它不显示PDF的内容。
The code is working but it does not display the contents of the PDF.
我在这里想问题的不正常打开PDF文件?
I think the problem here its not opening the PDF Document properly?
我在这里的目标是从PDF文档中提取文本,并将其转移到变量在code,则显示它。
我利用iText版本5.3.3。
I am using iText Version 5.3.3.
如果您的PDF是用PDF制作商,所以它是文字而不是一个扫描的文档或其他图片,这应该这样做:
If your PDF is made with a PDF maker, so it is text and NOT a scanned document or other picture, this should do it:
String content;
PdfReader reader = null;
try {
//String fileName is the string with the path to your .pdf file, for example resources/pdfs/preface.pdf
reader = new PdfReader(fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int numberOfPages = readerTest.getNumberOfPages();
numberOfPages = numberOfPages + 1;
for (int page = 1; page < numberOfPages; page++){
try {
String content1Page = PdfTextExtractor.getTextFromPage(reader, page);
content = content + content1Page;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
现在字符串的内容
包含了PDF文本。
Now String content
contains the text of the PDF.
编辑:您也可以先试着离开了的toString()
方法在这一行:海峡= PdfTextExtractor.getTextFromPage(读卡器,1 )的ToString();
You could also first try to leave out the toString()
method in this line: str = PdfTextExtractor.getTextFromPage(reader, 1).toString();