Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a PDF file in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 147
    Comment on it

    This tutorial will help you to generate a PDF file in Android. Follow below steps to generate PDF file.

     

    Step 1 :- First download .jar files from following URL and put it into your application's libs folder.

    Download URL:- https://www.dropbox.com/s/ir76ssy5b3ins22/itextpdf-5.3.3.jar?dl=0

     

    Step 2 :- Open application build.gradle file and add this itexpdf-5.3.3.jar file as dependency library in it.

    dependencies {
    
     compile files('libs/itextpdf-5.3.3.jar') 
    
    } 

     

    Step 3 :- Now write a code to generate a PDF file, open you MainActivity.java class and add following code in activity onCreate() method.

    // Destination Folder and File name
    String FILE = Environment.getExternalStorageDirectory().toString()
    +"/PDF/"+"EvonTech.pdf";
    
    
    // Add this permission into Manifest.xml to write PDF file in device external storage
    //  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    // Create New Blank Document
     Document document = new Document(PageSize.A4);
    
    // Create Directory in External Storage
    
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/PDF");
    myDir.mkdirs();    
    
    // Create Pdf Writer for Writting into New Created Document
    PdfWriter.getInstance(document, new FileOutputStream(FILE));
    
    // Open Document for Writting into document
    document.open();    
    
    // User Define Method
    addMetaData(document);    
    addTitlePage(document);
    
    // Close Document after writting all content
    document.close();

     

    Step 4 :- We will create a new method addDocumentProperties(Document document), in this method, we will define document properties such as file title, subject, keywords etc.

    // Set PDF document Properties
    public void addDocumentProperties(Document document){
    
    document.addTitle("EvonTechnology");
    document.addSubject("Details");
    document.addKeywords("Android development, IOS development, Web development");
    document.addAuthor("Evon");
    document.addCreator("Tech");
    
    }

     

    Step 5 :- We will create another method called addTitlePage(Document document). In this method, we will define document font style, add some tables to show data vertically and will add a new paragraph .
     

    public void addTitlePage(Document document) throws DocumentException {
            // Font Style for Document
            Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
            Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
                    | Font.UNDERLINE, BaseColor.GRAY);
            Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
            Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
    
            // Start New Paragraph
            Paragraph prHead = new Paragraph();
            
            // Set Font in this Paragraph
            prHead.setFont(titleFont);
    
            // Add item into Paragraph
            prHead.add("EvonTechnology\n");
    
            // Create Table into Document with 1 Row
            PdfPTable myTable = new PdfPTable(1);
    
            // 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(""));
            myCell.setBorder(Rectangle.BOTTOM);
    
            // Add Cell into Table
            myTable.addCell(myCell);
    
            prHead.setFont(catFont);
            prHead.add("\nInformation Technology\n\n");
            prHead.setAlignment(Element.ALIGN_CENTER);
    
            // Add all above details into Document
            document.add(prHead);
            document.add(myTable);
    
            document.add(myTable);
    
            // Now Start another New Paragraph
            Paragraph prPersinalInfo = new Paragraph();
            prPersinalInfo.setFont(smallBold);
            prPersinalInfo.add("India\n\n");
            prPersinalInfo.add("Uttrakhand\n\n");
            prPersinalInfo.add("City: Dehradun. State: UK\n\n");
            prPersinalInfo.add("Country: India, Zip Code: 480001\n\n");
            prPersinalInfo
                    .add("Mobile: 9999999999 Fax: 1111111 Email: test123@gmail.com \n\n");
    
            prPersinalInfo.setAlignment(Element.ALIGN_CENTER);
    
            document.add(prPersinalInfo);
            document.add(myTable);
    
            document.add(myTable);
    
            Paragraph prProfile = new Paragraph();
            prProfile.setFont(smallBold);
            prProfile.add("\n \n Profile : \n ");
            prProfile.setFont(normal);
            prProfile.add("\n This is a pure leading IT company.");
    
            prProfile.setFont(smallBold);
            document.add(prProfile);
    
            // Create new Page in PDF
            document.newPage();
        }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: