2. Printing Images
•
•
Taking and sharing photos is one of the most popular uses for mobile
devices.
•
•
•
If your application takes photos, displays them, or allows users to
share images, you should consider enabling printing of those images
in your application.
•
•
•
The Android Support Library provides a convenient function for
enabling image printing using a minimal amount of code and simple
set of print layout options.
3. Printing Images
•
The Android Support Library PrintHelper class provides a simple way to print of images.
•
The class has a single layout option, setScaleMode(), which allows you to print with one of two options:
•
•
•SCALE_MODE_FIT
This option sizes the image so that the whole image is shown within the printable area of the page.
•
•
•SCALE_MODE_FILL
This option scales the image so that it fills the entire printable area of the page. Choosing this setting
means that some portion of the top and bottom, or left and right edges of the image is not printed. This
option is the default value if you do not set a scale mode.
•
Both scaling options for setScaleMode() keep the existing aspect ratio of the image intact. The following
code example shows
how to create an instance of the PrintHelper class, set the scaling option, and start the printing process:
•
4. Printing HTML Documents
•
•
Printing out content beyond a simple photo on Android requires
composing text and graphics in a print document.
•
•
The Android framework provides a way to use HTML to compose
a document and print it with a minimum of code.
•
•
In Android 4.4 (API level 19), the WebView class has been updated to
enable printing HTML content.
•
•
•
The class allows you to load a local HTML resource or download
a page from the web, create a print job and hand it off to Android's
print services.
•
•
Following Code shows you how to quickly build an HTML document
containing text and graphics and use WebView to print it.
6. Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
Code for PrintPdf.java
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.support.v4.print.PrintHelper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
PrintPdf.java code continue….
public class PrintPdf extends Activity implements
OnClickListener {
/** Called when the activity is first created. */
Button Image;
Button Html;
private WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Image = (Button) findViewById(R.id.BtnImage);
Html = (Button) findViewById(R.id.BtnHtml);
Image.setOnClickListener(this);
Html.setOnClickListener(this);
}
7. Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
PrintPdf.java code continue….
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == Image) {
doPhotoPrint();
}
if (v == Html) {
doWebViewPrint();
}
}
public class PrintPdf extends Activity implements
OnClickListener {
/** Called when the activity is first created. */
Button Image;
Button Html;
private WebView mWebView;
/** Called when the activity is first created. */
PrintPdf.java code continue….
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Image = (Button) findViewById(R.id.BtnImage);
Html = (Button) findViewById(R.id.BtnHtml);
Image.setOnClickListener(this);
Html.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == Image) {
doPhotoPrint();
}
if (v == Html) {
doWebViewPrint();
}
}
8. Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
PrintPdf.java code continue….
PrintPdf.java code continue….
// Method for Image to PDF
private void doPhotoPrint() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String
url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i("print", "page finished loading " + url);
createWebPrintJob(view);
mWebView = null;
}
});
// Generate an HTML document on the fly:
String htmlDocument = "<html><body><h1>World's First PDF using
Android's PrintManager</h1><p>This is going to be fun for " +
"every android developer...</p></body></html>";
PrintHelper photoPrinter = new PrintHelper(this);
// PrintHelper photoPrinter = new PrintHelper(this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
Bitmap bitmap =
BitmapFactory.decodeResource(getResources(),R.drawable.icon);
photoPrinter.printBitmap("droids.jpg - test print",
bitmap);
}
// Method for HTML -> PDF Conversion
private void doWebViewPrint() {
// Create a WebView object specifically for printing
//WebView webView = new WebView(getActivity());
WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {
webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML",
"UTF-8", null);
9. Create/Add Activity Name: PrintPdf.java
Use Following Code for PrintPdf.java
PrintPdf.java code continue….
Research prospect company.
Identify audience.
Define presales support (for example, engineers).
Plan // Keep a reference to WebView object until you pass the
PrintDocumentAdapter to the PrintManager
mWebView = webView;
}
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager =
(PrintManager)this.getSystemService(Context.PRINT_SERVICE
);
// Get a print adapter instance
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
PrintPdf.java code continue….
// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + "
Document";
PrintJob printJob = printManager.print(jobName,
printAdapter,
new PrintAttributes.Builder().build());
// Save the job object for later status checking
//mPrintJobs.add(printJob);
}
}
meeting agenda.
Call and confirm meeting ahead of time.
10. Manifest File Code
Main Code
New Classes used in the above code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Viewflipper"
android:versionCode="1"
android:versionName="1.0">
PrintHelper Helper for printing bitmaps.
PrintHelperKitkat Kitkat specific PrintManager API
implementation.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="19"
android:targetSdkVersion="19"/>
PrintManager
System level service for accessing the printing
capabilities of the platform.
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".PrintPdf"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
PrintDocumentAdapter
Base class that provides the content of a document
to be printed.
PrintJob
This class represents a print job from the perspective
of an application.
11. App Screen with two options for Image and
HTML Printing and PDF Generation
12. Save as PDF Dialog Box, Enter
Filename and Hit Save
13. PDF Files Generated With App
For more details on Printing Content using Android visit following link:
https://developer.android.com/training/printing/index.html
HTML to PDF
Image to PDF
14. Contact Us
Lets Nurture UK
Lets Nurture INDIA
Gumption Business Centre
Glydegate
Bradford
West Yorkshire
BD5 0BQ
United Kingdom
+44(01274)271727
www.letsnurture.co.uk
312 Kalasagar Mall
Nr. Satadhar Cross Road
Ahmedabad
Gujarat
380061
India
(+91) 7940095953
www.letsnurture.com