Android: How to open a PDF file at a specific page - android

I have seen code here which will open a PDF file in a PDF viewer. Is it possible to modify the code to make the PDF viewer open the PDF at a specific page?

No. You cannot control 3rd party application unless it gives this possibility.

Yes there is a way to open a specific page in a pdf file
Inside Main Activity
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), pdfActivity.class);
intent.putExtra("pageno", 5); // add this to pass the page number
startActivity(intent);
}
});
Inside Pdf Activity
pdfView.fromAsset("SAMPLE_FILE.pdf")
.defaultPage(getIntent().getIntExtra("pageno",0)) // this is used to scroll to the page required
.enableAnnotationRendering(true)
.scrollHandle(new DefaultScrollHandle(this))
.load();
If you don't want it to pass through intent
pdfView.fromAsset("SAMPLE_FILE.pdf")
.defaultPage(65) // Write the number of the page
.enableAnnotationRendering(true)
.scrollHandle(new DefaultScrollHandle(this))
.load();

Yes You can open a Specific page in pdf file.
first of all create a global varble like
// give a defualt value
int pageNumber=0;
// secondly create a button in search dilaoge to find exact page in activty.
btn.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = 1;
String input = String.valueOf(textSearch.getText());
if (input != null && !input.equals("")) {
position =
Integer.valueOf(String.valueOf(textSearch.getText()));
}
// assign globar var pageNumber to finding position
pageNumber = position-1;
textSearch.setText("");
alertDialog1.dismiss();
}
// in Third Step just passed the pageNumber in defualt function.
pdfViewr.from("Your uri String ").default(pageNumber).load();

for ezpdf reader:
add intent.putExtra("page", 110);
But ezpdf is quite old. So I recommend you to checkout my new opensource pdf viewer and annotator project:
https://github.com/KnIfER/PolymPic
Intent it = new Intent(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra("page", yourPageIndex)
.setDataAndType(yourUri, "application/pdf");
startActivity(it);
It's based on PDFium, which is fast and can even run pretty well on the android 4.4.

Andrejs has recommended that I should modify an open source pdf viewer app. When I become more confident at Android programming I may attempt that. Meanwhile, I am following a different tack. I am storing images of the individual PDF pages as separate jpg files and using WebView / WebViewClient to display the individual pages. These can be zoomed and panned (as in a PDF Viewer) and it is easy to make my app move to the next or previous page when I touch the screen near the edge of the page.

Open a PDF file to a specific page
To target an HTML link to a specific page in a PDF file, add #page=[page number] to the end of the link's URL.
For example, this HTML tag opens page 4 of a PDF file named myfile.pdf:
<A HREF="http://www.example.com/myfile.pdf#page=4">

Related

Open multiple links in external browser

I have a list of web urls. I need to open them all at once in an external browser where each link's page results in a new tab. So for example, if the list has a link to google.com and yahoo.com they should both be opened in the browser in their own separate tabs.
To open a single link within an external browser is pretty straight forward - create an ACTION_VIEW intent, set the url and call startActivity. But how can I do this with multiple urls? Doing multiple startActivity calls won't work. Any ideas?
Thank you for your time!
My answer is based on a little research so I am not sure about the exact code but I will suggest an approach here
First you can have a Fragment or Activity with the WebView view type in the layout file respectively and depending on your use case, get the reference for the WebView. We will pass your list of urls to the activity containing the Webview
Say, we do it like so
val arrayList = getIntent().getArrayListExtra("urls")
val webView = findViewById<WebView>(R.id.myWebview)
Now we can listen to scroll changes of the WebView and wait for the user to reach the end of the WebView page as given in the following link
How to check the scrollend of WebView
And then when the scroll end is reached, we can load the next url from the array
webView.setOnScrollChangeListener((view, i, i1, i2, i3) -> {
int r1 = webView.computeVerticalScrollRange();
int r2 = webView.computeVerticalScrollExtent();
if (i1 == (r1 - r2)) {
webView.loadUrl(arrayList.get(1) // Or Whatever your next
index is
// Maybe maintain a global variable for which url is currently
loaded
}
});
To make the experience nice for the user you can consider setting a WebViewClient for your WebView and override onProgressChanged to show the progress to the user, just in case he doesn't feel lost

How to create PDF of using content of activity in android?

I have create an app of payroll system in android and it needs to be save the salary slip in PDF format that's why i need to create pdf of using content from SalarySlip.java.
Thanks.
For devices with API level 19 or more you can use the built in PrintedPdfDocument.
This class is a helper for creating a PDF file for given print attributes. It is useful for implementing printing via the native Android graphics APIs.
// open a new document
PrintedPdfDocument document = new PrintedPdfDocument(context,printAttributes);
// start a page
Page page = document.startPage(0);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
//close the document
document.close();
For more advance Features use iText

MuPDF: clickListener on hyperlinks

I use mupdf to view pdf files.
I built Android project using this instructions
However I can't click on the link in the text, e.x. url (it's highlighted but viewer doesn't react on click)
How could I jump through link in pdf file when click on it?
I'm also interested in alternative free pdf libraries for Android that supports hyperlinks.
We have a similar feature in our Android customizable magazine app, using muPDF: it is possible to "tap" on the links in the text, either external links, or links to other pages.
This app is open source and available on Github. It should be easy for you to review the code, and see how we implemented this feature.
"https://github.com/libreliodev/android" this link provides you pdf library which suppourts hyperlinks but it has lot other stuff,
what you need to do is after downloading the zip from the link extract it and load it in eclipse
1) create a activity and declare it in manifest
public class MyActivity extends BaseActivity /** base activity is available in library itself **/
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent resultIntent = new Intent(this, MuPDFActivity.class);
resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.setData(Uri.parse("give the path of your pdf location"));
resultIntent.putExtra(Magazine.FIELD_TITLE, "Title");
startActivity(resultIntent);
}
}
it will load the pdf

View to display PDFPage

I'm implementing some kind of magazine viewer (to browse pages like in android gallery), which content is taken from pdf file; pages must be zoomable. At first, I've tried to work with separate pictures from that pdf. I borrowed ImageViewTouch and ImageViewTouchViewPager. It works well, but I couldn't keep more than 3-4 big pics in memory (OutOfMemory error) , so that implementation is not suitable for me. (I need 5 big pictures in memory plus memory for about 50 little pictures to show table of contents)
So I've decided to work with pdf file. I've tried to display pdf with PdfViewer.jar, but this implementation with PDFViewerActivity is missing pictures or text when you try to zoom in or zoom out.
Can you advice me how to display my pdf like in android gallery? Or, at least, tell me, please, if you know views, which can display PDFPage with zooming option?
Edited: I do not need to open side app to view pdf. I need to dosplay it in my app.
You can give this a try:
public static void openPdf(Context context, String filename) {
File targetFile = new File(filename);
Uri targetUri = Uri.fromFile(targetFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
try {
context.startActivity(intent);
} catch (ActivityNotFoundException act ) {
Toast.makeText(context, "Please install a pdf reader to view help",Toast.LENGTH_LONG).show();
}
}

Open URL or PDFreader in custom screen Android

I am working on application where I need to open inbuilt apps in a custom screen like in Popup window
but when I invoke the inbuilt app the screen size is full screen while I need customize screen size. The inbuilt app can be anything like browser or pdfreader.
Here is my code :-
String strurl = "/sdcard/download/28889.pdf";
File file = new File(strurl);
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();
}
This opens up new window in my app but I need Popup kind of window where I need to show this pdfreader to get display.
By design, Android does not allow one app to run inside another (among other things, for obvious security reasons). So if that is what you want to do, you are out of luck.
BUT you can use a WebView (I let you consult the doc on this class) to display a webpage inside your app.
There are also some pdf libraries out there. A word of caution though, I worked on a pdf reader on Android 1 year ago and implementing a pdf reader was just not the best solution. Handling the pdf decoding on server side and serving a view of this image (or conversion to html) was the best solution.
This way you don't use too much computing power to read the pdf (surprisingly you need some serious cpu to read a pdf).
Things may have changed since I made that development (I was really constrained by the time factor) though but I still don't think that unless you explicitely need to display a pdf document and not just a document (for example a legitimate reason would be that you need to check the signature of the pdf on the Android device), you should not use the pdf format on a phone.
For web URL You can use Webview
For PDF
You need to make your pdf reader or you can use any open source pdf reader to make your own customized version of the pdf reader which you can use in your app.
Some of the them are
1) http://www.mupdf.com/
2) http://sourceforge.net/projects/pdfedit/
3) Search on Google/SO for other Links
Here is the Example code for pdf Reader:
http://lokeshatandroid.blogspot.in/2012/07/android-pdf-reader.html

Categories

Resources