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
Related
I am loading an html page from a web server into a Xamarin Forms WebView. This html page contains links to a PDF file.
In iOS, when the user clicks the link, the PDF file is downloaded and opened.
In Android, when the user clicks the link, nothing happens. Note that several PDF-Viewers are installed on the Android device.
Is there any configuration necessary for make this work in Android:
Here is the code:
if (dict.ContainsKey("url")){
string url = dict["url"];
if (Uri.IsWellFormedUriString(url, UriKind.Absolute)){
NotifWebView.Source = url;
} else {
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = #"<html><body><h1>Fehler!</h1><p>Ungültige oder keine URL oder ungültiges HTML.</p></body></html>";
NotifWebView.Source = htmlSource;
}
}
Thanks for your help!
You can use
https://github.com/xamarin/recipes/tree/master/Recipes/xamarin-forms/Controls/display-pdf
The WebView control can display PDF files on the iOS platform, but not on the Android and Windows Phone platforms due to lack of platform support.You need to create a custom renderer to acheve it in android platform.The process for doing this is as follows:
Create a Xamarin.Forms custom control
Consume the custom control from Xamarin.Forms.
Create the custom renderer for the control on each platform.
If you're willing to spend some money, it looks like Syncfusion has a solution for viewing and editing PDF files.
http://www.syncfusion.com/products/xamarin
So i have a textView and sometimes my textView contains links, currently when i click in this link an intent with a list of downloaded browsers opens. now what i want is, i wanna open this link inside the current app, say in a WebView. how can i do this?
we do have multiple articles/questions on same topic but i have no idea what this articles/questions are really about.
like this one https://gist.github.com/ksoichiro/3394338 gave me an ClassCastException
android.text.style.URLSpan cannot be cast to pb.myPackage.LinkUtils$SensibleUrlSpan
or this one handle textview link click in my android app
is there's any straight simple way to achieve this? please let me know.
You could use Chrome Custom Tabs, this is faster than a webview in loading webpages and is pretty simple to customize.
First include the Library:
dependencies {
...
compile 'com.android.support:customtabs:25.1.1'
}
Then in your Activity when you call the method to open a browser add these lines of java:
String url = ¨https://google.com/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent intent = builder.build();
intent.launchUrl(this, Uri.parse(url));
Refer to it's docs for more customization at:
https://developer.chrome.com/multidevice/android/customtabs
Recently was added this support library, but I couldn't find any example.
What the purpose of this library?
Could you post any example using this library?
CustomTabs is used to open links in a browser that supports CustomTabs. Most likely opening is done on Chrome, hence CustomTabs is part of chromium platform.
Purpose is to avoid implementing WebViews in your application and yet giving you option for styling actual chrome tabs, like toolbar color, title, various exit/enter transition, adding action buttons and menues. CustomTabs will allow your application bind to the chrome service and make chrome work as part of your application. Styling which will give you feel the opened web resource is part of your application.
Beside the styling, CustomTabs will give you full chrome web capabilities that probably couldn't be achieved with standard WebView.
Here are demos, which are straight forward.
Edit:
A snippet from my application which is "simplified" version of the Google's demo, lacking fallback mechanism, for now.
Usage of the helper is the following:
Initialize it when your activity is alive
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview);
mCustomTabHelper = new SimpleCustomChromeTabsHelper(this);
}
2. When the instance is alive and we have an url ready to be opened we can call:
mCustomTabHelper.prepareUrl(mProduct.getRedirectUrl());
Which will bind to the Chrome service, if not previously bind, or will just notify Chrome service that we might be opening that link in the future.
CustomTabSession can be used to open or prepare multiple url.
Open the url
mCustomTabHelper.openUrl(mProduct.getRedirectUrl());
The overloaded method of openUrl is using sort of ui options builder that is replica of the CustomTabIntent.Builder, but I have dropped the CustomTabsSession argument so the helper later will build CustomTabIntent internally.
I'm running Chrome Dev version along stable one. If I choose the stable one, I'm not able to use CustomTabsat all. As Google advices, CustomTabs will only work on Chrome 45 and beta versions of Chrome.
Demo from my application: https://youtu.be/fnIZwuJXjHI
Edit: Post
Try this:
gradle dependency:
dependencies {
...
compile 'com.android.support:customtabs:25.1.0'
}
Code :
Uri uri = Uri.parse("https://github.com/mzelzoghbi");
// create an intent builder
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// Begin customizing
// set toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
// build custom tabs intent
CustomTabsIntent customTabsIntent = intentBuilder.build();
// launch the url
customTabsIntent.launchUrl(activity, uri);
There is demo project on github, mentioned by #NikolaDespotoski, which can be partially reusable.
Solution is based on this article.
Add project shared to your project. Shared is a name of project (I don't know why Google didn't add it into customtabs library). link to shared project
Copy Activity helper from demo project to your project and put correct package. CustomTabActivityHelper
To pre-fetch url use CustomTabActivityHelper#mayLaunchUrl method (if needed) and CustomTabActivityHelper#openCustomTab to open Chrome custom tab.
For instance openning custom tab:
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, uri,
new CustomTabActivityHelper.CustomTabFallback() {
#Override
public void openUri(Activity activity, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Pre-fetching of url is more complicated. You can see this demo for better understanding.
I'm having what seems to be a pretty small issue in the Phonegap 1.3.0 application I am working on. It is for Android devices.
The problem is stemming from the ads I am trying to incorporate into my app. I am trying t use Leadbolt's no-SDK offer wall as well as their banner ads.
I am loading the offer wall within an iframe in my app, inside of a hidden DIV, and then displaying it when required - this part is working great.
The problem comes when I click on one of the links on the offer wall: instead of launching the clicked URL in a new External/Native browser, the link is being opened within the iframe. The same thing happens when someone clicks the banners, though these are integrated by inserting a in the location you want the ad to show rather than via an iframe. (Maybe the script injects an iframe, dunno, the end behavior is the same)
What I'm trying to do now is to implement a method in Java to catch any clicks, and open a new browser if the link is not relative/local - i.e. if it is prefixed by "http", "https", or "market" protocols
And here lies the real issue - the only experience I have with Java has been messing with Phoengap a bit :(
I have been reading and testing things all day, but finally I must resort to asking those of you who are more knowledgeable than I am in regards to Java programming.
Here is what I have been attempting so far:
package com.phonegap.my_great_app;
import android.os.Bundle;
import android.webkit.WebView;
import com.phonegap.*;
public class MyGreatApp extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
/* Intercept clicked links, and open external URLs in external browser */
public boolean shouldOverrideUrlLoading(DroidGap super, String url) {
if (url != null && url.startsWith("market://") || url.startsWith("http://") || url.startsWith("https://")) {
/* Open new WebView or external browser with URL */
/* Do it here */
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else {
/* Do nothing, it's relative URL from my app */
return false;
}
}
}
}
I am sure this code is laughably wrong, but hopefully it can illustrate what I'm trying to achieve here. Truth be told, I have no idea if shouldOverrideUrlLoading() is even the right method to be calling for this problem!
I will be very grateful for any advice or code samples you can offer! Thanks :)
EDIT: Just to be clear - I cannot alter the iframe source code so javascript or changing the link targets is not an option due to XSS issues.
You can open external links in the default browser instead of opening inside your phonegap application. This can be done in two ways easily.
One is by using Phonegap medthod like this,
navigator.app.loadUrl('http://stackoverflow.com');
/*NOTE : In some phonegap versions it does not works*/
Another way is using jquery mobile, you can do like this,
YouTube Channel
Thats all.
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">