
Friday, June 9, 2017
Display PDF in assets folder inside APK
Display PDF in assets folder inside APK
The example "Display PDF using PdfRenderer" show how to display PDF stored in sdcard, this example show how to display PDF stored in assets inside APK.

First, you have to create assets folder and copy your PDF into it.
Then you have to edit aaptOptions in build.gradle (Module: app), not to compass "pdf" file.
aaptOptions {
noCompress "pdf"
}
Layout file, refer to the example "Create PDF using PdfDocument".
MainActivity.java
package com.blogspot.android_er.androidpdf;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
ImageView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView = (ImageView)findViewById(R.id.pdfview);
try {
openPDF();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,
"Something Wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
}
private void openPDF() throws IOException {
//open file in assets
AssetManager assetManager = getAssets();
AssetFileDescriptor assetFileDescriptor =
assetManager.openFd("test.pdf");
ParcelFileDescriptor fileDescriptor =
assetFileDescriptor.getParcelFileDescriptor();
//open file from sdcard
/*
String targetPdf = "/sdcard/test.pdf";
File file = new File(targetPdf);
ParcelFileDescriptor fileDescriptor = null;
fileDescriptor = ParcelFileDescriptor.open(
file, ParcelFileDescriptor.MODE_READ_ONLY);
*/
//min. API Level 21
PdfRenderer pdfRenderer = null;
pdfRenderer = new PdfRenderer(fileDescriptor);
final int pageCount = pdfRenderer.getPageCount();
Toast.makeText(this,
"pageCount = " + pageCount,
Toast.LENGTH_LONG).show();
//Display page 0
PdfRenderer.Page rendererPage = pdfRenderer.openPage(1);
int rendererPageWidth = rendererPage.getWidth();
int rendererPageHeight = rendererPage.getHeight();
Bitmap bitmap = Bitmap.createBitmap(
rendererPageWidth,
rendererPageHeight,
Bitmap.Config.ARGB_8888);
rendererPage.render(bitmap, null, null,
PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
pdfView.setImageBitmap(bitmap);
rendererPage.close();
pdfRenderer.close();
assetFileDescriptor.close();
}
}
Remark:If you reported with the error "java.io.IOException: not create document. Error:", read next post.
Sunday, April 16, 2017
Difference Between EPUB PDF MOBI and AZW E Book Standards
Difference Between EPUB PDF MOBI and AZW E Book Standards

Thursday, April 13, 2017
Display PDF using PdfRenderer
Display PDF using PdfRenderer

The class android.graphics.pdf.PdfRenderer enables rendering a PDF document. This example show how to:
MainActivity.java
package com.blogspot.android_er.androidpdf;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
String targetPdf = "/sdcard/MagPi54.pdf";
ImageView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdfView = (ImageView)findViewById(R.id.pdfview);
try {
openPDF();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,
"Something Wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
}
private void openPDF() throws IOException {
File file = new File(targetPdf);
ParcelFileDescriptor fileDescriptor = null;
fileDescriptor = ParcelFileDescriptor.open(
file, ParcelFileDescriptor.MODE_READ_ONLY);
//min. API Level 21
PdfRenderer pdfRenderer = null;
pdfRenderer = new PdfRenderer(fileDescriptor);
final int pageCount = pdfRenderer.getPageCount();
Toast.makeText(this,
"pageCount = " + pageCount,
Toast.LENGTH_LONG).show();
//Display page 0
PdfRenderer.Page rendererPage = pdfRenderer.openPage(0);
int rendererPageWidth = rendererPage.getWidth();
int rendererPageHeight = rendererPage.getHeight();
Bitmap bitmap = Bitmap.createBitmap(
rendererPageWidth,
rendererPageHeight,
Bitmap.Config.ARGB_8888);
rendererPage.render(bitmap, null, null,
PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
pdfView.setImageBitmap(bitmap);
rendererPage.close();
pdfRenderer.close();
fileDescriptor.close();
}
}
layout/activity_main.xml, add a ImageView to display the PDF:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_id="@+id/activity_main"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_paddingBottom="@dimen/activity_vertical_margin"
android_paddingLeft="@dimen/activity_horizontal_margin"
android_paddingRight="@dimen/activity_horizontal_margin"
android_paddingTop="@dimen/activity_vertical_margin"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidpdf.MainActivity">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_margin="20dp"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold"/>
<ImageView
android_id="@+id/pdfview"
android_layout_width="match_parent"
android_layout_height="match_parent" />
</LinearLayout>
uses-permission of "android.permission.READ_EXTERNAL_STORAGE" is needed in AndroidManifest.xml to read file from sdcard.
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.blogspot.android_er.androidpdf">
<application
android_allowBackup="true"
android_icon="@mipmap/ic_launcher"
android_label="@string/app_name"
android_supportsRtl="true"
android_theme="@style/AppTheme">
<activity android_name=".MainActivity">
<intent-filter>
<action android_name="android.intent.action.MAIN" />
<category android_name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android_name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
Next:
- Display PDF in assets folder (inside APK)
Related:
- Create PDF using PdfDocument
Thursday, April 10, 2014
Fujitsu ScanSnap S1500 Instant PDF Sheet Fed Scanner For PC
I have never had a dedicated document scanner before. In the past, I used the Auto-document feeder on my all-in-one printer/scanner/fax machines to scan documents into adobe acrobat. That meant for each batch of papers, I needed to set the color/resolution/paper size/quality settings and then work through Acrobat in order to save each file. It worked, but it was slow going.
Then I bought this new scansnap 1500, based on the ratings of the earlier model (the scansnap 510). I installed only the main program to my computer (the software also includes OCR programs, organizational programs, and acrobat 9, which I already have). I hooked up the scanner and stuck in a stack of old notes I took from some college courses--some notes were in color, some were b&w, some on two sides, some on one side, and some of the pages were upside down. I hit the only button on the machine (SCAN), and to my sheer amazement, it started flying through the stack of notes without any additional prompts or effort. It then automatically saved my file as a pdf--all of the pages were in order, color pages were in color, b&w pages were in b&w, etc. The only issue I noticed was that if there were any marks whatsoever on the back side of my note papers, it included those blank pages in the file.
Review about Fujitsu ScanSnap S1500 Instant PDF Sheet-Fed Scanner for PC
I purchased this scanner as a first step in creating an electronic filing system for a home office. As I accumulated 20 filing cabinet drawers of paper documents going paperless seemed to be an answer.Purchase of the Fujitsu S1500 was based on the numerous 5 star reviews. I am reasonably tech literate and have used the scanner for about 2 weeks. Here are my initial impressions:
Pros -
1. Speed: This scanner seems blazingly fast. Not a resource hog. Will scan duplex 20-25ppm at default settings on Win2K/1. 8mhz/1gb platform.
2. Scan quality very good at all settings.
3. Size and looks: Small desk footprint. Looks great open or closed.
4. Excellent paper feeding as long as documents are same size and 20# or greater paper weight.
Cons -
1. NO TWAIN DRIVER. I knew this going in but did not appreciate how much this would cripple the device in day to day use. Let me explain. I had envisioned creating a virtual file cabinet in my computer using Windows Directory/Folder/File system then scanning my paper documents into this system as pdf files. The problem is that the native Fujitsu software does not let you append an existing pdf file!!!
As an example take a file for utility bills. D:/Home Expenses/Utilities/Gas_Electric. pdf. You can easily scan in all prior bills(limited by 50 sheet ADF)and a pdf with one page for each bill will be created. But if you had more than 50 prior bills or when next months bill comes then the next scan you perform to include these new documents will create a new pdf and does not allow the option of appending this current scan to the existing pdf. As you can see one could easily end up with 1000s of individual pdf documents in your filing system. The work around is to launch Adobe Acrobat (not reader) and use it to open the old composite pdf and use Acrobat to append the newly created pdf to the prior file. This work around takes longer than scanning in a 30 page document. Twain based scanners easily work with existing windows file structure and allow you to append an existing pdf without invoking Acrobat. .
Features:
- One button searchable PDF creation
- Intelligent paper feed detection
- Blazing 20ppm color scanning
- 50-page Automatic Document Feeder (ADF)
- Comes with Adobe Acrobat X Standard