java 如何在Android中使用iText打开pdf文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17296236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open pdf file using iText in Android?
提问by TJM
I'm new in Android . I'm implementing PDF document using iText in Android . I want to read PDF document in my application. I don't have an idea how to access PDF doc in my app. I have created but when I run the application I get exception for FileNotFoundException
and I also import a PDF file in SD card through DD-MS. But the PDF file does not open in Android. Here is my code:
我是 Android 新手。我正在 Android 中使用 iText 实现 PDF 文档。我想在我的应用程序中阅读 PDF 文档。我不知道如何在我的应用程序中访问 PDF 文档。我已经创建了,但是当我运行应用程序时,我得到了例外FileNotFoundException
,我还通过 DD-MS 在 SD 卡中导入了一个 PDF 文件。但 PDF 文件无法在 Android 中打开。这是我的代码:
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.codec.Base64.InputStream;
public class MainActivity extends Activity
{
private static String FILE = "xyz.pdf";
/** Called when the activity is first created. */
@Override
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;
int n = 0;
try
{
istr =(InputStream) assetManager.open("xyz.pdf");
reader=new PdfReader(istr);
n=reader.getNumberOfPages();
Log.e("n value:","-> " +n);
str=reader.getPageContent(2).toString();
}
catch(Exception e)
{
e.printStackTrace();
}
TextView tv = (TextView) findViewById(R.id.textOne);
tv.setText(str);
}
}
回答by ridoy
As far as i know,iText is only for PDF creation, it doesn't contain viewer part.It is a parser, not a renderer. So you need to choose some another library to view pdf.But theare are some excellent code example in SO which you can view whether it meets your requirement..
据我所知,iText 仅用于创建 PDF,它不包含查看器部分。它是一个解析器,而不是一个渲染器。因此,您需要选择其他一些库来查看 pdf。但是 SO 中有一些出色的代码示例,您可以查看它是否满足您的要求。
You can try other solution rather than iText..
- Render a PDF file using Java on Android
- http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/
您可以尝试其他解决方案而不是 iText ..
-在 Android 上使用 Java 渲染 PDF 文件
- http://andpdf.svn.sourceforge.net/viewvc/andpdf/trunk/
And an excellent working code last of all..
- Example of code to implement a PDF reader
最后还有一个出色的工作代码..
-实现 PDF 阅读器的代码示例
回答by Rahul Sirwani
We can Extract data from iText. Here i am extracting text from PDFs file and showing on Edit-text :
我们可以从 iText 中提取数据。在这里,我从 PDF 文件中提取文本并在 Edit-text 上显示:
String pat = data.getData().getPath();
File f = new File(pat);
static PdfReader read;
read = new PdfReader(new FileInputStream(f));
PdfReaderContentParser parser;
parser = new PdfReaderContentParser(read);
StringWriter strw;
strw = new StringWriter();
TextExtractionStrategy stretegy;
stretegy = parser.processContent(j, new SimpleTextExtractionStrategy());
strw.write(stretegy.getResultantText());
String da = strw.toString();
edt1.setText(da);