Android PDF Creator Tutorial using iTextG Library
Hello Developers, Today we will discuss about Android PDF Creator , how to create pdf document in Android using the iTextG library. iTextG is a very popular multiplatform library for creating and manipulating PDF documents programmatically. It is available for Java, .Net, Android androidd other platforms. We will create a simple android pdf creater app, where you can add some text and create a pdf for that text. To keep this simple, we will use only text for creation of pdf in this tutorial, in later tutorials we will have a more thorough discussion involving images, tables and fonts.
- Create Android Pdf Creator App
- Go to File → New → New Project and enter your Application Name.
- Enter company domain, this is used to uniquely identify your App’s package worldwide.
- Choose project location and minimum SDK and on the next screen choose Empty Activity, since we would be adding most of the code Ourselves. Then Click on Next.
- Choose an Activity Name. Make sure Generate Layout File check box is selected, Otherwise we have to generate it ourselves.Then click on Finish. We have used the Activity Name as PdfCreatorActivity.java. This will be the default screen when the user opens the app for the first time.
Gradle will configure your project and resolve the dependencies, Once it is complete proceed for next steps.
Add dependencies and permissions for Writing Local Storage
Add the to your apps Gradle file:
To create a Pdf file, our app will need the following permissions
to add this permission, open your project’s AndroidManifest.xml and add the permission.
Layout for the App
Add the following code to the activity_pdfcreator.xml for the android pdf creator app.
Layout for the android pdf creator app is very simple. We have an EditText for Adding the content that will go into the Pdf and Create button. On clicking the create button, Pdf will be generated and will be shown by the default PdfView app for your android phone.
Add code to create Pdf Programmatically
Open the PdfCreatorActivity.java and add the following code to create pdf in android.
Let’s go through the code for android pdf creator app step by step, First, we reference to the widgets defined in the layout. We have defined one OnClickListener() for the Create Pdf button to invoke the createPdfWrapper() function. Since we are targeting Android version M, First we need to check whether the user has already granted the permissions for writing to external storage in case you are on android M or later. If the permission is not already granted, it will generate a dialog for the user to grant the permission, otherwise, if the permission is already granted it will invoke the createPdf() function. The permission model in android will be discussed in a separate post.
Let’s discuss the working of the createPdf() function.
First we get the File object for Documents directory in case the folder is not already present, we create it using the mkdir() method of the File object. Next we create a new File object in the path named HelloWorld.pdf.
Here are the steps we need to follow to create a Pdf document.
- Create a Document object.
- Get an instance of PdfWriter using the getInstance method by passing the Document that has to be written and the OutputStream the writer has to write to.
- Then we need to open the document and add the Element we want to add. Element can be Anchor, Chapter, Chunk, Header, Image, Jpeg, List, ListItem, Meta, Paragraph, Phrase, Rectangle, Section.
- Once you have written the contents, we need to close the Document. After that nothing can be written to the body anymore.
Now, the Pdf Document is created and we will use the default Pdf viewer of the android phone to view the created pdf. You must have a Pdf Viewer installed on your phone to handle this otherwise, you will get a message to download a pdf viewer. In later tutorials, we will also discuss how to render a Pdf in android app.
Now run the android pdf creator app on an actual Android Device and start creating your pdf programmatically. Ability to handle a Pdf programmatically is really handy and is used in very famous apps like CamScanner to create Pdf from images on the go. This android pdf creator was an introduction to working with Pdf in android, soon we will be covering how to handle images, fonts, graphics etc. to create more advanced pdfs and how to render the Pdf in your app itself.
What’s Next ??
After this, Soon We will cover more detailed articles on the same topic and have a thorough tutorial about handling permissions in Android M and above.
200 total views, 6 views today