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
    • 154
    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.

    1. dependencies {
    2.  
    3. compile files('libs/itextpdf-5.3.3.jar')
    4.  
    5. }

     

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

    1. // Destination Folder and File name
    2. String FILE = Environment.getExternalStorageDirectory().toString()
    3. +"/PDF/"+"EvonTech.pdf";
    4.  
    5.  
    6. // Add this permission into Manifest.xml to write PDF file in device external storage
    7. // <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    8.  
    9. // Create New Blank Document
    10. Document document = new Document(PageSize.A4);
    11.  
    12. // Create Directory in External Storage
    13.  
    14. String root = Environment.getExternalStorageDirectory().toString();
    15. File myDir = new File(root + "/PDF");
    16. myDir.mkdirs();
    17.  
    18. // Create Pdf Writer for Writting into New Created Document
    19. PdfWriter.getInstance(document, new FileOutputStream(FILE));
    20.  
    21. // Open Document for Writting into document
    22. document.open();
    23.  
    24. // User Define Method
    25. addMetaData(document);
    26. addTitlePage(document);
    27.  
    28. // Close Document after writting all content
    29. 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.

    1. // Set PDF document Properties
    2. public void addDocumentProperties(Document document){
    3.  
    4. document.addTitle("EvonTechnology");
    5. document.addSubject("Details");
    6. document.addKeywords("Android development, IOS development, Web development");
    7. document.addAuthor("Evon");
    8. document.addCreator("Tech");
    9.  
    10. }

     

    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 .
     

    1. public void addTitlePage(Document document) throws DocumentException {
    2. // Font Style for Document
    3. Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    4. Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
    5. | Font.UNDERLINE, BaseColor.GRAY);
    6. Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    7. Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
    8.  
    9. // Start New Paragraph
    10. Paragraph prHead = new Paragraph();
    11. // Set Font in this Paragraph
    12. prHead.setFont(titleFont);
    13.  
    14. // Add item into Paragraph
    15. prHead.add("EvonTechnology\n");
    16.  
    17. // Create Table into Document with 1 Row
    18. PdfPTable myTable = new PdfPTable(1);
    19.  
    20. // 100.0f mean width of table is same as Document size
    21. myTable.setWidthPercentage(100.0f);
    22.  
    23. // Create New Cell into Table
    24. PdfPCell myCell = new PdfPCell(new Paragraph(""));
    25. myCell.setBorder(Rectangle.BOTTOM);
    26.  
    27. // Add Cell into Table
    28. myTable.addCell(myCell);
    29.  
    30. prHead.setFont(catFont);
    31. prHead.add("\nInformation Technology\n\n");
    32. prHead.setAlignment(Element.ALIGN_CENTER);
    33.  
    34. // Add all above details into Document
    35. document.add(prHead);
    36. document.add(myTable);
    37.  
    38. document.add(myTable);
    39.  
    40. // Now Start another New Paragraph
    41. Paragraph prPersinalInfo = new Paragraph();
    42. prPersinalInfo.setFont(smallBold);
    43. prPersinalInfo.add("India\n\n");
    44. prPersinalInfo.add("Uttrakhand\n\n");
    45. prPersinalInfo.add("City: Dehradun. State: UK\n\n");
    46. prPersinalInfo.add("Country: India, Zip Code: 480001\n\n");
    47. prPersinalInfo
    48. .add("Mobile: 9999999999 Fax: 1111111 Email: test123@gmail.com \n\n");
    49.  
    50. prPersinalInfo.setAlignment(Element.ALIGN_CENTER);
    51.  
    52. document.add(prPersinalInfo);
    53. document.add(myTable);
    54.  
    55. document.add(myTable);
    56.  
    57. Paragraph prProfile = new Paragraph();
    58. prProfile.setFont(smallBold);
    59. prProfile.add("\n \n Profile : \n ");
    60. prProfile.setFont(normal);
    61. prProfile.add("\n This is a pure leading IT company.");
    62.  
    63. prProfile.setFont(smallBold);
    64. document.add(prProfile);
    65.  
    66. // Create new Page in PDF
    67. document.newPage();
    68. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: