com.zaptech.pdfdemo.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.zaptech.pdfdemo.MainActivity.java

Source

package com.zaptech.pdfdemo;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {

    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font smallFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    private FileOutputStream fileOutputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            File sdCard = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFDemo");
            if (!sdCard.exists()) {
                sdCard.mkdirs();
            }
            File file = new File(sdCard, "sample.pdf");
            if (!file.exists()) {
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
            PdfWriter.getInstance(document, fileOutputStream);
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document);
            document.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addContent(Document document) {
        try {
            Anchor anchor = new Anchor("First Chapter", catFont);
            anchor.setName("First Chapter");
            Chapter chapter = new Chapter(new Paragraph(anchor), 1);
            Paragraph paragraph = new Paragraph("Subcategory 1", subFont);
            Section section = chapter.addSection(paragraph);
            section.add(new Paragraph("Hello"));
            paragraph = new Paragraph("Subcategory 2", subFont);
            section = chapter.addSection(paragraph);
            section.add(new Paragraph("Paragraph 1"));
            section.add(new Paragraph("Paragraph 2"));
            section.add(new Paragraph("Paragraph 3"));
            createList(section);
            Paragraph paragraph1 = new Paragraph();
            addEmptyLine(paragraph1, 5);
            section.add(paragraph1);
            document.add(chapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createList(Section section) {
        List list = new List(true, false, 10);
        list.add(new ListItem("First point"));
        list.add(new ListItem("Second point"));
        list.add(new ListItem("Third point"));
        section.add(list);
    }

    private void addTitlePage(Document document) {
        try {
            Paragraph paragraph = new Paragraph();
            addEmptyLine(paragraph, 1);
            paragraph.add(new Paragraph("Sample Title", catFont));
            addEmptyLine(paragraph, 1);
            paragraph.add(
                    new Paragraph("Report generated by:: " + System.getProperty("user.name") + ", ", smallFont));
            addEmptyLine(paragraph, 3);
            paragraph.add(new Paragraph("This document describes something which is very important ", smallFont));
            addEmptyLine(paragraph, 8);
            paragraph.add(new Paragraph(
                    "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
                    redFont));
            document.add(paragraph);
            document.newPage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addEmptyLine(Paragraph paragraph, int i) {
        for (int j = 0; j < i; j++) {
            paragraph.add("");
        }
    }

    private void addMetaData(Document document) {
        document.addTitle("My First PDF");
        document.addSubject("Sample PDF");
        document.addKeywords("Java,Android");
        document.addAuthor("Bandish Mehta");
        document.addCreator("Bandish Mehta");
    }
}