Create Pdf Using Pdfdocument

Posted on

Display PDF using PdfRenderer to sentiment it.

(remark: if you lot purpose final event to sentiment the PDF file, you lot accept to change targetPdf = “/sdcard/test.pdf”.)


MainActivity.java

package com.blogspot.android_er.androidcreatepdf;  import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.pdf.PdfDocument; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast;  import java.io.File; import java.io.FileOutputStream; import java.io.IOException;  populace shape MainActivity extends AppCompatActivity {      Button btnCreate;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          btnCreate = (Button)findViewById(R.id.create);         btnCreate.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View view) {                 createPdf();             }         });      }      private void createPdf(){         // create a novel document         PdfDocument document = novel PdfDocument();          // crate a page description         PdfDocument.PageInfo pageInfo =                  novel PdfDocument.PageInfo.Builder(100, 100, 1).create();          // get-go a page         PdfDocument.Page page = document.startPage(pageInfo);          Canvas sheet = page.getCanvas();          Paint pigment = novel Paint();         paint.setColor(Color.RED);          canvas.drawCircle(50, 50, 30, paint);          // complete the page         document.finishPage(page);          // Create Page ii         pageInfo = novel PdfDocument.PageInfo.Builder(500, 500, 2).create();         page = document.startPage(pageInfo);         sheet = page.getCanvas();         pigment = novel Paint();         paint.setColor(Color.BLUE);         canvas.drawCircle(200, 200, 100, paint);         document.finishPage(page);          // write the document content         String targetPdf = "/sdcard/test.pdf";         File filePath = novel File(targetPdf);         endeavor {             document.writeTo(new FileOutputStream(filePath));             Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();         } choose handgrip of (IOException e) {             e.printStackTrace();             Toast.makeText(this, "Something wrong: " + e.toString(),                     Toast.LENGTH_LONG).show();         }          // unopen the document         document.close();     } }  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="16dp"     android:paddingLeft="16dp"     android:paddingRight="16dp"     android:paddingTop="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidcreatepdf.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="https://rootandroid.org//"         android:textStyle="bold"/>      <Button         android:id="@+id/create"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Create PDF"/> </LinearLayout>  

uses-permission of “android.permission.WRITE_EXTERNAL_STORAGE” is needed inwards AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.blogspot.android_er.androidcreatepdf">      <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.WRITE_EXTERNAL_STORAGE"/>  </manifest>