Support
Quality
Security
License
Reuse
kandi has reviewed AndroidPdfViewer and discovered the below as its top functions. This is intended to give you an instant insight into AndroidPdfViewer implemented functionality, and help decide if they suit your requirements.
Android view for displaying PDFs rendered with PdfiumAndroid
ProGuard
-keep class com.shockwave.**
Include PDFView in your layout
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Load a PDF file
pdfView.fromUri(Uri)
or
pdfView.fromFile(File)
or
pdfView.fromBytes(byte[])
or
pdfView.fromStream(InputStream) // stream is written to bytearray - native code cannot use Java Streams
or
pdfView.fromSource(DocumentSource)
or
pdfView.fromAsset(String)
.pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
.enableSwipe(true) // allows to block changing pages using swipe
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
// allows to draw something on the current page, usually visible in the middle of the screen
.onDraw(onDrawListener)
// allows to draw something on all pages, separately for every page. Called only for visible pages
.onDrawAll(onDrawListener)
.onLoad(onLoadCompleteListener) // called after document is loaded and starts to be rendered
.onPageChange(onPageChangeListener)
.onPageScroll(onPageScrollListener)
.onError(onErrorListener)
.onPageError(onPageErrorListener)
.onRender(onRenderListener) // called after document is rendered for the first time
// called on single tap, return true if handled, false to toggle scroll handle visibility
.onTap(onTapListener)
.onLongPress(onLongPressListener)
.enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
.password(null)
.scrollHandle(null)
.enableAntialiasing(true) // improve rendering a little bit on low-res screens
// spacing between pages in dp. To define spacing color, set view background
.spacing(0)
.autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
.linkHandler(DefaultLinkHandler)
.pageFitPolicy(FitPolicy.WIDTH) // mode to fit pages in the view
.fitEachPage(false) // fit each page to the view, else smaller pages are scaled relative to largest page.
.pageSnap(false) // snap pages to screen boundaries
.pageFling(false) // make a fling change only a single page like ViewPager
.nightMode(false) // toggle night mode
.load();
Document sources
pdfView.fromUri(Uri)
pdfView.fromFile(File)
pdfView.fromBytes(byte[])
pdfView.fromStream(InputStream)
pdfView.fromAsset(String)
Double tap zooming
void setMinZoom(float zoom);
void setMidZoom(float zoom);
void setMaxZoom(float zoom);
How can I fit document to screen width (eg. on orientation change)?
Configurator.onRender(new OnRenderListener() {
@Override
public void onInitiallyRendered(int pages, float pageWidth, float pageHeight) {
pdfView.fitToWidth(pageIndex);
}
});
How can I scroll through single pages like a ViewPager?
.swipeHorizontal(true)
.pageSnap(true)
.autoSpacing(true)
.pageFling(true)
License
Copyright 2017 Bartosz Schiller
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
How to open pdf files in webview in pdfactivity?
final String pdfurl = view.getHitTestResult().getExtra();
Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
intent.putExtra("PDFURL",pdfurl);
startActivity(intent);
package ak.wp.meto.activity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import ak.wp.meto.R;
public class PdfViewer extends Activity {
private TextView txt; // You can remove if you don't want this
private PDFView pdf;
String value = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_pdf);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("PDFURL");
}
System.out.println("PRINT PDFVIEWER DATA" +value);
pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
txt = findViewById(R.id.txtPdf);
String pdfUrl = value;
try{
new RetrievePdfStream().execute(pdfUrl);
}
catch (Exception e){
Toast.makeText(this, "Failed to load Url :" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).load();
}
}
}
-----------------------
final String pdfurl = view.getHitTestResult().getExtra();
Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
intent.putExtra("PDFURL",pdfurl);
startActivity(intent);
package ak.wp.meto.activity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import ak.wp.meto.R;
public class PdfViewer extends Activity {
private TextView txt; // You can remove if you don't want this
private PDFView pdf;
String value = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_pdf);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("PDFURL");
}
System.out.println("PRINT PDFVIEWER DATA" +value);
pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
txt = findViewById(R.id.txtPdf);
String pdfUrl = value;
try{
new RetrievePdfStream().execute(pdfUrl);
}
catch (Exception e){
Toast.makeText(this, "Failed to load Url :" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).load();
}
}
}
Background color of barteksc/AndroidPdfViewer
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
AndroidView(
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
update = { pdfView ->
pdfView
.fromStream(pdfView.context.resources.openRawResource(R.raw.sample))
.autoSpacing(false)
.spacing(25)
.pageFitPolicy(FitPolicy.BOTH)
.load()
},
modifier = Modifier
.fillMaxSize()
.background(Color.Red)
)
-----------------------
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
AndroidView(
factory = { context ->
PDFView(
context,
null
).also {
it.setBackgroundColor(Color.Transparent.toArgb())
}
},
update = { pdfView ->
pdfView
.fromStream(pdfView.context.resources.openRawResource(R.raw.sample))
.autoSpacing(false)
.spacing(25)
.pageFitPolicy(FitPolicy.BOTH)
.load()
},
modifier = Modifier
.fillMaxSize()
.background(Color.Red)
)
Adding Text/Annotations into existing PDF file and View/Rendering the output in android
PdfReader reader = new PdfReader("original.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("original-stamped.pdf"));
PdfContentByte cb = stamper.getOverContent(1);
cb.beginText();
cb.setTextMatrix(100, 400);
cb.showText("Text at position 100,400.");
cb.endText();
stamper.close();
reader.close();
How to convert a string containing numbers separated by comma to Integer in java?
int[] pages = {1, 5, 10, 12, 15};
i.putExtra("pages", pages);
int[] pages = intent.getIntArrayExtra("pages");
pdfView.fromAsset(result)
.pages(pages)
-----------------------
int[] pages = {1, 5, 10, 12, 15};
i.putExtra("pages", pages);
int[] pages = intent.getIntArrayExtra("pages");
pdfView.fromAsset(result)
.pages(pages)
kotlin androidpdfviewer lib doesn't seem to load
<androidx.constraintlayout.widget.ConstraintLayout
//....
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="@dimen/zeroDp"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/tvFeedback"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
//...
</androidx.constraintlayout.widget.ConstraintLayout>
private fun downloadFile(responseBody: ResponseBody) {// get responseBody after making Http req . I'm using retrofit
binding?.let {
//it.pdfView.removeAllViews()
it.pdfView.fromBytes(responseBody.bytes())
.defaultPage(pageNumber)
.onPageChange(this@PdfReaderFragment)
.enableAnnotationRendering(true)
.onLoad(this@PdfReaderFragment)
.spacing(10) // in dp
.onPageError(this@PdfReaderFragment)
.onError { onError("File not in PDF format or corrupted") }
.load()
}
}
OnPageChangeListener
OnLoadCompleteListener
OnPageErrorListener
override fun loadComplete(nbPages: Int) {
binding?.let {
printBookmarksTree(it.pdfView.tableOfContents, "-")
}
}
override fun onPageError(page: Int, t: Throwable?) {
Log.d("PDF", t?.message)
}
override fun onPageChanged(page: Int, pageCount: Int) {
pageNumber = page
Log.d("PDF", "Page number $pageNumber $pageCount")
setTitleMessageBackArrow(String.format("%s %s / %s", "your fragment title", page + 1, pageCount))
}
private fun printBookmarksTree(tree: List<Bookmark>, sep: String) {
for (b in tree) {
Log.e("PDF", String.format("%s %s, p %d", sep, b.title, b.pageIdx))
if (b.hasChildren()) {
printBookmarksTree(b.children, "$sep-")
}
}
}
-----------------------
<androidx.constraintlayout.widget.ConstraintLayout
//....
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="@dimen/zeroDp"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/tvFeedback"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
//...
</androidx.constraintlayout.widget.ConstraintLayout>
private fun downloadFile(responseBody: ResponseBody) {// get responseBody after making Http req . I'm using retrofit
binding?.let {
//it.pdfView.removeAllViews()
it.pdfView.fromBytes(responseBody.bytes())
.defaultPage(pageNumber)
.onPageChange(this@PdfReaderFragment)
.enableAnnotationRendering(true)
.onLoad(this@PdfReaderFragment)
.spacing(10) // in dp
.onPageError(this@PdfReaderFragment)
.onError { onError("File not in PDF format or corrupted") }
.load()
}
}
OnPageChangeListener
OnLoadCompleteListener
OnPageErrorListener
override fun loadComplete(nbPages: Int) {
binding?.let {
printBookmarksTree(it.pdfView.tableOfContents, "-")
}
}
override fun onPageError(page: Int, t: Throwable?) {
Log.d("PDF", t?.message)
}
override fun onPageChanged(page: Int, pageCount: Int) {
pageNumber = page
Log.d("PDF", "Page number $pageNumber $pageCount")
setTitleMessageBackArrow(String.format("%s %s / %s", "your fragment title", page + 1, pageCount))
}
private fun printBookmarksTree(tree: List<Bookmark>, sep: String) {
for (b in tree) {
Log.e("PDF", String.format("%s %s, p %d", sep, b.title, b.pageIdx))
if (b.hasChildren()) {
printBookmarksTree(b.children, "$sep-")
}
}
}
-----------------------
<androidx.constraintlayout.widget.ConstraintLayout
//....
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="@dimen/zeroDp"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/tvFeedback"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
//...
</androidx.constraintlayout.widget.ConstraintLayout>
private fun downloadFile(responseBody: ResponseBody) {// get responseBody after making Http req . I'm using retrofit
binding?.let {
//it.pdfView.removeAllViews()
it.pdfView.fromBytes(responseBody.bytes())
.defaultPage(pageNumber)
.onPageChange(this@PdfReaderFragment)
.enableAnnotationRendering(true)
.onLoad(this@PdfReaderFragment)
.spacing(10) // in dp
.onPageError(this@PdfReaderFragment)
.onError { onError("File not in PDF format or corrupted") }
.load()
}
}
OnPageChangeListener
OnLoadCompleteListener
OnPageErrorListener
override fun loadComplete(nbPages: Int) {
binding?.let {
printBookmarksTree(it.pdfView.tableOfContents, "-")
}
}
override fun onPageError(page: Int, t: Throwable?) {
Log.d("PDF", t?.message)
}
override fun onPageChanged(page: Int, pageCount: Int) {
pageNumber = page
Log.d("PDF", "Page number $pageNumber $pageCount")
setTitleMessageBackArrow(String.format("%s %s / %s", "your fragment title", page + 1, pageCount))
}
private fun printBookmarksTree(tree: List<Bookmark>, sep: String) {
for (b in tree) {
Log.e("PDF", String.format("%s %s, p %d", sep, b.title, b.pageIdx))
if (b.hasChildren()) {
printBookmarksTree(b.children, "$sep-")
}
}
}
-----------------------
<androidx.constraintlayout.widget.ConstraintLayout
//....
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="@dimen/zeroDp"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/tvFeedback"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
//...
</androidx.constraintlayout.widget.ConstraintLayout>
private fun downloadFile(responseBody: ResponseBody) {// get responseBody after making Http req . I'm using retrofit
binding?.let {
//it.pdfView.removeAllViews()
it.pdfView.fromBytes(responseBody.bytes())
.defaultPage(pageNumber)
.onPageChange(this@PdfReaderFragment)
.enableAnnotationRendering(true)
.onLoad(this@PdfReaderFragment)
.spacing(10) // in dp
.onPageError(this@PdfReaderFragment)
.onError { onError("File not in PDF format or corrupted") }
.load()
}
}
OnPageChangeListener
OnLoadCompleteListener
OnPageErrorListener
override fun loadComplete(nbPages: Int) {
binding?.let {
printBookmarksTree(it.pdfView.tableOfContents, "-")
}
}
override fun onPageError(page: Int, t: Throwable?) {
Log.d("PDF", t?.message)
}
override fun onPageChanged(page: Int, pageCount: Int) {
pageNumber = page
Log.d("PDF", "Page number $pageNumber $pageCount")
setTitleMessageBackArrow(String.format("%s %s / %s", "your fragment title", page + 1, pageCount))
}
private fun printBookmarksTree(tree: List<Bookmark>, sep: String) {
for (b in tree) {
Log.e("PDF", String.format("%s %s, p %d", sep, b.title, b.pageIdx))
if (b.hasChildren()) {
printBookmarksTree(b.children, "$sep-")
}
}
}
QUESTION
How to open pdf files in webview in pdfactivity?
Asked 2022-Feb-26 at 00:46Hello I'm developing app to open pdf files in webview in pdfactivity. I tried opening through intent its redirecting to system inbuilt apps but not pdfactivity.java. find the code below, I declared this in my webview activity,
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = WebvActivity.this.getIntent().getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.putExtra(PdfActivity.EXTRA_PDFFILENAME, "url");
startActivity(intent);
return super.shouldOverrideUrlLoading(view, url);
}
I'm using PdfViewer.jar library, In README text it is mentioned use
Intent intent = new Intent(this, WebviewActivity.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "PATH TO PDF GOES HERE");
startActivity(intent);
How can I get URL of pdf files from the webpage in webview & load them in PdfActivity. See below link for your reference, https://sourceforge.net/p/andpdf/code/HEAD/tree/tag/Beta_0_1_11/AndroidPdfViewer/activitysrc/net/sf/andpdf/
ANSWER
Answered 2022-Jan-18 at 02:33Maybe this link could be very helpful for you How to display a PDF via Android web browser without "downloading" first
You can open a pdf using Google Docs Viewer.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit