Read the pdf content? - android

able to open the pdf files . but dont know how to save the content of the pdf file in a array or something then i can show the pdf as i want. can anybody suggest me how to save the pdf content...is there any libraries to use?
following snippet shows how to open a pdf
File file = new File("/sdcard/example1.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Log.e("path of the file",path.toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(),
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
Thanks

You should use iText library..
iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them
here is the link.
http://www.vogella.de/articles/JavaPDF/article.html

Take a look at Mono for Android. It will enable you to develop for Android using C# and .NET and will let you install and access to the iTextSharp library
The iTextSharp is a very extensive and well documented library for creating and manipulating adobe pdf documents.

Related

Android open PDF from URI starting content

I am trying to open up PDF using intent that takes the following:
Here is the code that I have:
Intent intent = new Intent(Intent.ActionVIEW)
intent.setDataAndType(Uri.parse("CONTENTURI + FILENAME","application/pdf"
try { startActivity(intent)} catch excpetion and so on.
It pops up with whatever applications I have installed, from Adobe Reader, Google PDF reader, POLARIS(as I am using Galaxy Tab 3 for testing), and none of those work. Each say unsupported document.
Does download and show the right solution in this case, or any ideas? Thanks in advance!
Following code triggers the VIEW action for pdf files (open the default PDF viewer, choose between applications capable to view PDFs or get an error if you don't have any installed).
File file = new File("your pdf path here");
if (file.exists()) {
Uri pdfPath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfPath, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
//if user doesn't have pdf reader instructing to download a pdf reader
}
}
NOTE: The PDF should not be saved local to application, or else the third party app won't have access. You should use media location.

I want to read the pdf files from assets or raw folder in android

when i m run it i m getting this error like "File or folder not found" and " file dosnt exit or can't be opened "..
This is the code sir..
i want the how to view pdf files from assets or in raw folder sir.. its very imp for my project try to slove my problem sir.. and tell me clearly i m new to android sir.. can any one send me code cloerly how to do it..
enter code here
Uri path = Uri.parse("android.resource:///com.example.pratice/raw/em");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(this, "NO Viewer", Toast.LENGTH_SHORT).show();
}
Few, if any, Android PDF viewer apps will honor the android.resource scheme, and there is no Uri scheme for assets.
If you want to serve up a PDF file, copy it from assets or a raw resource into internal storage, then use FileProvider to make it available to other apps. Here is a sample application that does this.

AcivityNotFound - viewing pdf in external pdf viewer

I am trying to view a local pdf in an external pdf viewer using this code:
Uri path = Uri.parse("android.resource://<package-name>/raw/Terms.pdf>");
try
{
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(path, "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getActivity(), "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
Even though I have Adobe PDF installed, it throws an ActivityNotFoundExcecption.
Why is that?
Few, if any, PDF viewing apps support the android:resource scheme. You need to provide your PDF file by some other means to PDF viewers, such as via FileProvider, so you can use a scheme (e.g., content) that is more likely to be supported.

Displaying PDF in a view in Android

In Android I need to display a PDF file, which is in the asset folder, in a view. Can anyone help me out with the sample code?
I don't think its possible to display it in a view. You will have to make an Intent and then call an external program to do so.
File sd = new File("example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(sd));
intent.setDataAndType(Uri.fromFile(sd), "application/pdf");
try {
getContext().startActivity(intent);
} catch(ActivityNotFoundException e){
// Show a message that a PDF viewer is not installed
Toast.makeText("No PDF reader available",Toast.LENGTH_LONG).show();
}

Pdf files as part of Android .apk

I have to build an Android application which shows a list of pdf files. These pdf files should be secured, in other words - the user of the app should not be able to get a copy of the pdf content by any means (copy/cut/print...etc). My questions now are
How should I ship the content of the pdf file along with .apk file.
If we send the content of the file in a diff format (raw/byte code), how should I convert the file back to pdf and where should I place the file on the installed machine such that it is secure.
FYI, I will be locking down current version of Adobe Redaer as the pdf viewer to view the pdf files as it doesn't allow copy/paste/print.
This is the first Android app that I am developing. So, I'm a kind of Android newbie. Any help would be greatly appreciated.
Thanks,
Navin
Probably you should store it in the res/raw folder
You can copy this(these) pdf file(s) into assets folder, thus your files will be the part of your application and can not be accessible outside, and you can also treat them as normal files (i.e. open with any pdf viewer in your case). Whenever you want to get access to context file you can call context.getAssets() .
Hope this information helps.
Update : Here is the code I am using to open a file in downloads directory, this code can open the pdf file in any compatible pdf reader if installed on phone, Tested with Adobe Reader. You will need a URI of the pdf file in assets folder to run the code.
String fileName = pdfUrl.substring(pdfUrl.lastIndexOf("/"));
Log.i("fileName", "" + fileName);
File file = new File("/sdcard/download" + fileName);
// File file = new
// File("/sdcard/download/airtel_latest000.mp3");
Intent intent;
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
downloaded = true;
} else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(pdfUrl));
downloaded = false;
}
try {
startActivityForResult(intent, 5);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}

Categories

Resources