Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create PDF file using iText or some other library on android?

How to create PDF file using iText or some other library on android?

Is there any tutorial on iText for android?

Thanks

like image 289
Khushwant Avatar asked Dec 08 '11 17:12

Khushwant


People also ask

Can I use iText for free?

To answer your question: iText can be used for free in situations where you also distribute your software for free. As soon as you want to use iText in a closed source, proprietary environment, you have to pay for your use of iText.

Is iText PDF open source?

Over 20 years ago, the code that formed the basis for the iText PDF library as we know it today was written in an open-source environment. Even today, its code remains open-source.

Does iText 7 support .NET core?

NET Standard 1.6 and up have been supported since iText 7.1.


2 Answers

It's Easy,For example

Here the Code in my repository (updated link)

gradle.build

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.madgag:scpkix-jdk15on:1.47.0.1'
compile 'com.itextpdf:itextpdf:5.0.6'

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        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"
    tools:context="com.example.hackro.itext.MainActivity">
    
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnwrite"
        android:text="PDF"
        android:onClick="GeneratePDF"
        />
    </RelativeLayout>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.itextpdf.text.pdf.BaseFont;
import java.io.File;

public class MainActivity extends Activity {

    private static final String LOG_TAG = "GeneratePDF";


    private BaseFont bfBold;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



    }


    public void GeneratePDF(View view)
    {
        // TODO Auto-generated method stub
        String filename = "david";
        String filecontent = "Contenido";
        Metodos fop = new Metodos();
        if (fop.write(filename, filecontent)) {
            Toast.makeText(getApplicationContext(),
                    filename + ".pdf created", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(getApplicationContext(), "I/O error",
                    Toast.LENGTH_SHORT).show();
        }
    }

  }

Metodos.java

 import android.util.Log;
    
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Element;
    import com.itextpdf.text.Font;
    import com.itextpdf.text.Paragraph;
    import com.itextpdf.text.Phrase;
    import com.itextpdf.text.pdf.PdfPCell;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfWriter;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * Created by hackro on 24/11/15.
     */
    public class Metodos {
    
    
        public Boolean write(String fname, String fcontent) {
            try {
                String fpath = "/sdcard/" + fname + ".pdf";
                File file = new File(fpath);
    
                if (!file.exists()) {
                    file.createNewFile();
                }
    
                Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
                Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12);
    
    
                Document document = new Document();
    
                PdfWriter.getInstance(document,
                        new FileOutputStream(file.getAbsoluteFile()));
                document.open();
    
                document.add(new Paragraph("Sigueme en Twitter!"));
    
                document.add(new Paragraph("@DavidHackro"));
                document.close();
    
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }}
    

Result

Good Luck

like image 177
David Hackro Avatar answered Oct 24 '22 23:10

David Hackro


I have created a sample project for creating the pdf file from data using itextpdf/itext7 library

Example project link: https://github.com/rheyansh/RPdfGenerator

Add below dependancy in your application gradle:

implementation 'com.itextpdf:itext7-core:7.1.12'

Important Notes

Add WRITE_EXTERNAL_STORAGE permission in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Add File Provider in AndroidManifest.xml

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.rheyansh.rpdfgenerator.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

Add XML resource folder (see provider_paths.xml in example folder)

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

Create RPdfGenerator class

import android.content.Context
import android.os.Environment
import com.rheyansh.model.RTransaction
import android.widget.Toast
import com.itextpdf.io.font.constants.StandardFonts
import com.itextpdf.kernel.colors.ColorConstants
import com.itextpdf.kernel.font.PdfFontFactory
import com.itextpdf.kernel.pdf.PdfDocument
import com.itextpdf.kernel.pdf.PdfWriter
import com.itextpdf.kernel.pdf.action.PdfAction
import com.itextpdf.layout.Document
import com.itextpdf.layout.element.Paragraph
import com.itextpdf.layout.element.Table
import com.itextpdf.layout.element.Text
import com.itextpdf.layout.property.TextAlignment
import com.itextpdf.layout.property.UnitValue
import com.rheyansh.lenden.model.RPdfGeneratorModel
import java.io.File
import java.io.FileOutputStream

object RPdfGenerator {

    private val linkSample = "https://github.com/rheyansh/RPdfGenerator"

    fun generatePdf(context: Context, info: RPdfGeneratorModel) {

        val FILENAME = info.header + ".pdf"
        val filePath = getAppPath(context) + FILENAME

        if (File(filePath).exists()) {
            File(filePath).delete()
        }

        val fOut = FileOutputStream(filePath)
        val pdfWriter = PdfWriter(fOut)

        // Creating a PdfDocument
        val pdfDocument =
            PdfDocument(pdfWriter)
        val layoutDocument = Document(pdfDocument)

        // title
        addTitle(layoutDocument, info.header)

        //add empty line
        addEmptyLine(layoutDocument,1)

        //Add sub heading
        val appName = "RPdfGenerator"
        addSubHeading(layoutDocument, "Generated via: ${appName}")
        addLink(layoutDocument, linkSample)

        //add empty line
        addEmptyLine(layoutDocument,1)

        // customer reference information
        addDebitCredit(layoutDocument, info)

        //add empty line
        addEmptyLine(layoutDocument,1)

        //Add sub heading
        addSubHeading(layoutDocument, "Transactions")

        //Add list
        addTable(layoutDocument, info.list)

        layoutDocument.close()
        Toast.makeText(context, "Pdf saved successfully to location $filePath", Toast.LENGTH_LONG).show()

        //FileUtils.openFile(context, File(filePath))
    }

    private fun getAppPath(context: Context): String {
        val dir = File(
            Environment.getExternalStorageDirectory()
                .toString() + File.separator
                    + context.resources.getString(R.string.app_name)
                    + File.separator
        )
        if (!dir.exists()) {
            dir.mkdir()
        }
        return dir.path + File.separator
    }

    private fun addTable(layoutDocument: Document, items: List<RTransaction>) {

        val table = Table(
            UnitValue.createPointArray(
                floatArrayOf(
                    100f,
                    180f,
                    80f,
                    80f,
                    80f,
                    100f
                )
            )
        )

        // headers
        //table.addCell(Paragraph("S.N.O.").setBold())
        table.addCell(Paragraph("Item").setBold())
        table.addCell(Paragraph("Customer").setBold())
        table.addCell(Paragraph("Qty").setBold())
        table.addCell(Paragraph("Price/Q").setBold())
        table.addCell(Paragraph("Total").setBold())
        table.addCell(Paragraph("Date").setBold())

        // items
        for (a in items) {
//            table.addCell(Paragraph(a.SNO.toString() + ""))
            table.addCell(Paragraph(a.itemName + ""))
            table.addCell(Paragraph(a.custName + ""))
            table.addCell(Paragraph(a.quantity.toString() + ""))
            table.addCell(Paragraph(a.pricePerUnit.toString() + ""))
            table.addCell(Paragraph((a.quantity * a.pricePerUnit).toString() + ""))
            table.addCell(Paragraph(a.transactionDateStr + ""))
        }
        layoutDocument.add(table)
    }

    private fun addEmptyLine(layoutDocument: Document, number: Int) {
        for (i in 0 until number) {
            layoutDocument.add(Paragraph(" "))
        }
    }

    private fun addDebitCredit(layoutDocument: Document, info: RPdfGeneratorModel) {

        val table = Table(
            UnitValue.createPointArray(
                floatArrayOf(
                    100f,
                    160f
                )
            )
        )

        table.addCell(Paragraph("Total Credit").setBold())
        table.addCell(Paragraph(info.totalCredit + ""))
        table.addCell(Paragraph("Total Debit").setBold())
        table.addCell(Paragraph(info.totalDebit + ""))
        table.addCell(Paragraph("Total Profit").setBold())
        table.addCell(Paragraph(info.totalProfit + ""))

        layoutDocument.add(table)
    }

    private fun addSubHeading(layoutDocument: Document, text: String) {
        layoutDocument.add(
            Paragraph(text).setBold()
                .setTextAlignment(TextAlignment.CENTER)
        )
    }

    private fun addLink(layoutDocument: Document, text: String) {

        val blueText: Text = Text(text)
            .setFontColor(ColorConstants.BLUE)
            .setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD))

        layoutDocument.add(
            Paragraph(blueText)
                .setAction(PdfAction.createURI(text))
                .setTextAlignment(TextAlignment.CENTER)
                .setUnderline()
                .setItalic()
        )
    }

    private fun addTitle(layoutDocument: Document, text: String) {
        layoutDocument.add(
            Paragraph(text).setBold().setUnderline()
                .setTextAlignment(TextAlignment.CENTER)
        )
    }
}

RPdfGeneratorModel

class RPdfGeneratorModel(list: List<RTransaction>, header: String) {

    var list = emptyList<RTransaction>()
    var header = ""
    var totalCredit = ""
    var totalDebit = ""
    var totalProfit = ""

    init {
        this.list = list
        this.header = header
        calculateTotal(list)
    }

    private fun calculateTotal(items: List<RTransaction>) {
        val totalPlus = items.map {
            if (it.transType == RTransactionType.plus) {
                it.totalPrice
            } else { 0.0 }
        }.sum()

        val totalMinus = items.map {
            if (it.transType == RTransactionType.minus) {
                it.totalPrice
            } else { 0.0 }
        }.sum()

        val final = totalPlus - totalMinus
        totalDebit = "-" + totalMinus.toString()
        totalCredit = totalPlus.toString()
        totalProfit = final.toString()
    }
}

RTransaction model

enum class RTransactionType { plus, minus }

class RTransaction {

    var itemName: String = ""
    var custName: String = ""
    var transType: RTransactionType = RTransactionType.plus
    var pricePerUnit: Double = 0.0
    var quantity: Int = 0
    var totalPrice: Double = 0.0
    var transactionDateStr: String = ""

    constructor() {
    }
}

write below functions in your activity class for creating dummy data

private fun dummyModel(): RPdfGeneratorModel {
        val list = dummyTransactions()
        val header = "Statement"
        val dummy = RPdfGeneratorModel(list, header)
        return dummy
    }

    private fun dummyTransactions(): List<RTransaction> {

        val list = arrayListOf<RTransaction>()

        val i1 = RTransaction()
        i1.custName = "Johan Store"
        i1.itemName = "Snacks"
        i1.quantity = 4
        i1.pricePerUnit = 40.0
        i1.totalPrice = i1.quantity * i1.pricePerUnit
        i1.transactionDateStr = "10 Sep, 20"
        i1.transType = RTransactionType.plus
        list.add(i1)


        val i2 = RTransaction()
        i2.custName = "Alice Store"
        i2.itemName = "Chocolate"
        i2.quantity = 3
        i2.pricePerUnit = 79.0
        i2.totalPrice = i2.quantity * i2.pricePerUnit
        i2.transactionDateStr = "9 Sep, 20"
        i2.transType = RTransactionType.plus
        list.add(i2)

        val i3 = RTransaction()
        i3.custName = "Alexa Mall"
        i3.itemName = "Shoes"
        i3.quantity = 2
        i3.pricePerUnit = 177.0
        i3.totalPrice = i3.quantity * i3.pricePerUnit
        i3.transactionDateStr = "9 Sep, 20"
        i3.transType = RTransactionType.minus
        list.add(i3)

        val i4 = RTransaction()
        i4.custName = "Zainab Baba"
        i4.itemName = "Chips"
        i4.quantity = 5
        i4.pricePerUnit = 140.0
        i4.totalPrice = i4.quantity * i4.pricePerUnit
        i4.transactionDateStr = "8 Sep, 20"
        i4.transType = RTransactionType.plus
        list.add(i4)

        list.add(i1)
        list.add(i2)
        list.add(i3)
        list.add(i4)

        list.add(i1)
        list.add(i2)
        list.add(i3)
        list.add(i4)


        return list
    }

Now call RPdfGenerator function. Make sure to ask WRITE_EXTERNAL_STORAGE permission before calling. For more details checkout example project

val dummyInfo = dummyModel()
                RPdfGenerator.generatePdf(this, dummyInfo)
like image 30
Raj Sharma Avatar answered Oct 25 '22 00:10

Raj Sharma