Here I am going to explain how to create a PDF dynamically in android
Step 1
Download itextpdf jar file and add it as external library.
Step 2
Create a PDF file

try {
Document document = new Document(PageSize.A4);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "AndroidDhina");
folder.mkdir();
File pdfFile = new File(folder, "ShopName"+".pdf");
if (pdfFile.createNewFile()) {
pdfFile.createNewFile();
}
// Create Pdf Writer for Writting into New Created Document
try {
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
// Open Document for Writting into document
document.open();
// User Define Method
addMetaData(document);
addTitlePage(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
public void addMetaData(Document document)
{
document.addTitle("Android");
document.addSubject("Bill");
document.addKeywords("Dhina,Bill,Product");
document.addAuthor("Android");
document.addCreator("Dhina");
}
public void addTitlePage(Document document) throws DocumentException
{
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.NORMAL);
Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
| Font.UNDERLINE, BaseColor.GREEN);
// Start New Paragraph
Paragraph prHead = new Paragraph();
prHead.setFont(titleFont);
prHead.add("Android\n");
document.add(prHead);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm aa");
String formattedDate = dateFormat.format(new Date()).toString();
Paragraph prSubHead = new Paragraph();
prSubHead.setFont(catFont);
prSubHead.add("AndroidDhina" + '\n');
prSubHead.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
document.add(prSubHead);
prSubHead= new Paragraph();
prSubHead.setFont(normalFont);
prSubHead.add(formattedDate);
prSubHead.setAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);
document.add(prSubHead );
PdfPTable myTable = new PdfPTable(3);
// 100.0f mean width of table is same as Document size
myTable.setWidthPercentage(100.0f);
// Create New Cell into Table
PdfPCell myCell = new PdfPCell(new Paragraph("Product Name"));
// Add Cell into Table
myTable.addCell(myCell);
myCell = new PdfPCell(new Paragraph("Sales"));
myTable.addCell(myCell);
myCell = new PdfPCell(new Paragraph("Rate"));
myTable.addCell(myCell);
for (int i = 0, count = 8; i < count; ++i)
{
myCell= new PdfPCell(new Paragraph("Product"+i));
myTable.addCell(myCell);
myCell= new PdfPCell(new Paragraph(10+i+""));
myTable.addCell(myCell);
myCell= new PdfPCell(new Paragraph(15+i+""));
myTable.addCell(myCell);
}
document.add(myTable);
prSubHead= new Paragraph();
prSubHead.setFont(normalFont);
prSubHead.add("K K Nagar"+ '\n');
prSubHead.add("Chennai" + '\n');
prSubHead.setAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);
document.add(prSubHead);
// Create new Page in PDF
document.newPage();
}
Happy Coding:)
Enthusiastic words written in this blog helped me to enhance my skills as well as helped me to know how I can help myself on my own. I am really glad to come at this platform.
ReplyDeletefree invoice maker