'How can I open a PDF in my Android app and allow the users to use the embedded hyperlinks?
i am making a pdf reader in android app through android studio, i am using pdfviewer library in my app. pdf view workes but it doesnt working with embedded hrperlinks in pdf. .
public class StudyActivity extends AppCompatActivity implements OnPageChangeListener,OnLoadCompleteListener {
private static final String TAG = MainActivity.class.getSimpleName();
public static final String SAMPLE_FILE = "XAT.pdf";
PDFView pdfView;
Integer pageNumber = 0;
String pdfFileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.study_material);
pdfView= (PDFView)findViewById(R.id.pdfView);
displayFromAsset(SAMPLE_FILE);
}
private void displayFromAsset(String assetFileName) {
pdfFileName = assetFileName;
pdfView.fromAsset(SAMPLE_FILE)
.defaultPage(pageNumber)
.enableSwipe(true)
.swipeHorizontal(false)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.load();
}
@Override
public void onPageChanged(int page, int pageCount) {
pageNumber = page;
setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
}
@Override
public void loadComplete(int nbPages) {
PdfDocument.Meta meta = pdfView.getDocumentMeta();
printBookmarksTree(pdfView.getTableOfContents(), "-");
}
public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
for (PdfDocument.Bookmark b : tree) {
Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
if (b.hasChildren()) {
printBookmarksTree(b.getChildren(), sep + "-");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
xml file is //xml file
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimaryDark"
android:text="View PDF"
android:textColor="#ffffff"
android:id="@+id/tv_header"
android:textSize="18dp"
android:gravity="center"></TextView>
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_below="@+id/tv_header"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
in gradle
compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
compile 'com.github.bumptech.glide:glide:3.5.2'
Solution 1:[1]
In barteksc's libary the linkHandler(DefaultLinkHandler()) method is available since version: 3.2.0 beta-1. In your code you just need to add this method while calling fromAsset:
pdfView.fromAsset(SAMPLE_FILE)
.linkHandler(DefaultLinkHandler(pdfView))
.defaultPage(pageNumber)
.enableSwipe(true)
Unfortunately it was invented too late for you: in 2019
Solution 2:[2]
The fetch call is calling a URL /bloodRequest/[email protected], the corresponding server API definition has /donateBlood/:email, so the 404 error you have got is expected.
GET http://localhost:5000/bloodRequest/[email protected] 404 (Not Found) Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
Try either changing the client-side URL to /donateBlood/[email protected] or server-side API to /bloodRequest/:email.
PS: Basically, the code you are trying is syntactically valid. It must have been failed due to typos in the URL or not restarting the server after a particular change. I can see the same URL mismatch in the working code you have posted, so it should also have thrown 404. Please post the exact code you have tried if it doesn't work yet.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hell576 |
| Solution 2 | atomic_variable |
