This video shows the steps to create a PDF file for various contents such as Texts and Strings in your Android App. This video shows the steps using the Android Studio Java code.
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Source Code:
package com.example.createpdf;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;import java.io.File;
import java.io.FileOutputStream;public class MainActivity extends AppCompatActivity {
private EditText myEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);myEditText = findViewById(R.id.editText);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
}public void createMyPDF(View view){
PdfDocument myPdfDocument = new PdfDocument();
PdfDocument.PageInfo myPageInfo = new PdfDocument.PageInfo.Builder(300,600,1).create();
PdfDocument.Page myPage = myPdfDocument.startPage(myPageInfo);Paint myPaint = new Paint();
String myString = myEditText.getText().toString();
int x = 10, y=25;for (String line:myString.split(“\n”)){
myPage.getCanvas().drawText(line, x, y, myPaint);
y+=myPaint.descent()-myPaint.ascent();
}myPdfDocument.finishPage(myPage);
String myFilePath = Environment.getExternalStorageDirectory().getPath() + “/myPDFFile.pdf”;
File myFile = new File(myFilePath);
try {
myPdfDocument.writeTo(new FileOutputStream(myFile));
}
catch (Exception e){
e.printStackTrace();
myEditText.setText(“ERROR”);
}myPdfDocument.close();
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.createpdf”><uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
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></manifest>
Top posts/ comments from YouTube channel:
It is possible to put images from camera on pdf file too?
You should refer to my below video in this regard:
I hope this video is helpful.
sir,
I follow exactly your code but it comes out ERROR. I do not know where it went wrong.
Would appreciate for any assisance.
What is the error you are getting? Can you please copy the error message here?
Cheers
Programmer World
–
Hi can you show me your XML
Whatever code we have, it is shared on this portal or the YouTube video. We do not have any other backups of the project files with us. Hope you understand.
Cheers
Programmer World
–
Hancock https://yahoo.co.jp/
Norman
Show error message.here is my code please solve it
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.text_ID);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
}
public void createMYPdf(View view){
PdfDocument myPdfDocument=new PdfDocument();
PdfDocument.PageInfo myPageInfo=new PdfDocument.PageInfo.Builder(300,600,1).create();
PdfDocument.Page myPage=myPdfDocument.startPage(myPageInfo);
Paint myPaint=new Paint();
String myString=editText.getText().toString();
myPage.getCanvas().drawText(myString,10,25,myPaint);
myPdfDocument.finishPage(myPage);
String path= Environment.getExternalStorageDirectory().getPath()+”/myPdf.pdf”;
File myFile=new File(path);
try {
myPdfDocument.writeTo(new FileOutputStream(myFile));
} catch (Exception e){
e.printStackTrace();
editText.setText(“Error”);
}
myPdfDocument.close();
}
}
What is the error you are getting? Can you please copy the error message here?
Cheers
Programmer World
–
Same I am also facing issue after running the code, the error message I have pasted below,
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/myPDFFile.pdf: open failed: EPERM (Operation not permitted)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:575)
W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:236)
W/System.err: at java.io.FileOutputStream.(FileOutputStream.java:186)
W/System.err: at com.example.test.MainActivity.createMyPDF(MainActivity.java:131)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
W/System.err: at android.view.View.performClick(View.java:7455)
W/System.err: at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
W/System.err: at android.view.View.performClickInternal(View.java:7432)
W/System.err: at android.view.View.access$3700(View.java:835)
W/System.err: at android.view.View$PerformClick.run(View.java:28810)
W/System.err: at android.os.Handler.handleCallback(Handler.java:938)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err: at android.os.Looper.loopOnce(Looper.java:201)
W/System.err: at android.os.Looper.loop(Looper.java:288)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7842)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
W/System.err: Caused by: android.system.ErrnoException: open failed: EPERM (Operation not permitted)
W/System.err: at libcore.io.Linux.open(Native Method)
W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:567)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:273)
W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:567)
W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7728)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:561)
You are getting FileNotFoundException error. It is because the file you are trying to open to write does not exists. Most likely, you have missed to create the file in your code. Please check if the below line is included before trying to access the file in your code:
File myFile = new File(myFilePath);
Also, check read and write permissions are declared in manifest file and checked by the user in the code before working with the files:
If yes, then I will also recommend to check whether the file exists by using isExist API on file object. It will help in preventing the above exception from being thrown.
Cheers
Programmer World
https://programmerworld.co
–