aidul23

Untitled

Sep 16th, 2021
698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.aidul23.createpdf;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.Manifest;
  6. import android.app.DatePickerDialog;
  7. import android.graphics.Bitmap;
  8. import android.graphics.drawable.BitmapDrawable;
  9. import android.graphics.drawable.Drawable;
  10. import android.os.Bundle;
  11. import android.os.Environment;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.AdapterView;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.DatePicker;
  17. import android.widget.Spinner;
  18. import android.widget.Toast;
  19.  
  20. import com.aidul23.createpdf.databinding.ActivityMainBinding;
  21. import com.google.android.material.navigation.NavigationBarView;
  22. import com.itextpdf.io.image.ImageData;
  23. import com.itextpdf.io.image.ImageDataFactory;
  24. import com.itextpdf.kernel.color.Color;
  25. import com.itextpdf.kernel.geom.PageSize;
  26. import com.itextpdf.kernel.pdf.PdfDocument;
  27. import com.itextpdf.kernel.pdf.PdfWriter;
  28. import com.itextpdf.layout.Document;
  29. import com.itextpdf.layout.element.Cell;
  30. import com.itextpdf.layout.element.Image;
  31. import com.itextpdf.layout.element.Paragraph;
  32. import com.itextpdf.layout.element.Table;
  33. import com.itextpdf.layout.property.HorizontalAlignment;
  34. import com.itextpdf.layout.property.TextAlignment;
  35. import com.karumi.dexter.Dexter;
  36. import com.karumi.dexter.PermissionToken;
  37. import com.karumi.dexter.listener.PermissionDeniedResponse;
  38. import com.karumi.dexter.listener.PermissionGrantedResponse;
  39. import com.karumi.dexter.listener.PermissionRequest;
  40. import com.karumi.dexter.listener.single.PermissionListener;
  41.  
  42. import java.io.ByteArrayOutputStream;
  43. import java.io.File;
  44. import java.io.FileNotFoundException;
  45. import java.io.FileOutputStream;
  46. import java.io.OutputStream;
  47. import java.text.DateFormat;
  48. import java.text.SimpleDateFormat;
  49. import java.util.Calendar;
  50. import java.util.Locale;
  51.  
  52. public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  53.  
  54.     private ActivityMainBinding binding;
  55.     final Calendar myCalendar = Calendar.getInstance();
  56.     private static final String TAG = "MainActivity";
  57.  
  58.     @Override
  59.     protected void onCreate(Bundle savedInstanceState) {
  60.         super.onCreate(savedInstanceState);
  61.         binding = ActivityMainBinding.inflate(getLayoutInflater());
  62.         setContentView(binding.getRoot());
  63.  
  64.         Spinner spinner = findViewById(R.id.department_spinner);
  65.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  66.                 R.array.department, android.R.layout.simple_spinner_item);
  67.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  68.         spinner.setAdapter(adapter);
  69.  
  70.         DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
  71.             @Override
  72.             public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
  73.  
  74.                 myCalendar.set(Calendar.YEAR, year);
  75.                 myCalendar.set(Calendar.MONTH, monthOfYear);
  76.                 myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  77.                 updateLabel();
  78.             }
  79.         };
  80.  
  81.         binding.dateOfSub.setOnClickListener(new View.OnClickListener() {
  82.             @Override
  83.             public void onClick(View view) {
  84.                 new DatePickerDialog(MainActivity.this, date, myCalendar
  85.                         .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
  86.                         myCalendar.get(Calendar.DAY_OF_MONTH))
  87.                         .show();
  88.             }
  89.         });
  90.  
  91.  
  92.         binding.makePdf.setOnClickListener(new View.OnClickListener() {
  93.             @Override
  94.             public void onClick(View view) {
  95.  
  96.                 String courseCode = binding.courseCode.getText().toString();
  97.                 String courseTitle = binding.courseTitle.getText().toString();
  98.                 String submissionDate = binding.dateOfSub.getText().toString();
  99.                 String teacherName = binding.teacherName.getText().toString();
  100.                 String teacherDesignation = binding.teacherDesignation.getText().toString();
  101.                 String studentId = binding.studentId.getText().toString();
  102.                 String semester = binding.studentSemester.getText().toString();
  103.                 String section = binding.studentSection.getText().toString();
  104.                 String studentName = binding.studentName.getText().toString();
  105.  
  106.                 Log.d(TAG, "onClick: "+courseCode);
  107.  
  108.                 Dexter.withContext(MainActivity.this)
  109.                         .withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
  110.                         .withListener(new PermissionListener() {
  111.                             @Override
  112.                             public void onPermissionGranted(PermissionGrantedResponse response) {
  113.                                 try {
  114.                                     createPdf(courseCode, courseTitle, submissionDate, teacherName, studentName, teacherDesignation, studentId, semester, section);
  115.                                 } catch (FileNotFoundException e) {
  116.                                     e.printStackTrace();
  117.                                     Log.d(TAG, "onClick: " + e.getMessage());
  118.                                 }
  119.                             }
  120.  
  121.                             @Override
  122.                             public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
  123.  
  124.                             @Override
  125.                             public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
  126.                         }).check();
  127.  
  128.             }
  129.         });
  130.  
  131.     }
  132.  
  133.     private void createPdf(String courseCode, String courseTitle, String submissionDate, String teacherName, String studentName, String teacherDesignation, String studentId, String semester, String section) throws FileNotFoundException {
  134.         String pdfPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString();
  135.         File file = new File(pdfPath, System.currentTimeMillis()+".pdf");
  136.         OutputStream outputStream = new FileOutputStream(file);
  137.  
  138.  
  139.         PdfWriter writer = new PdfWriter(outputStream);
  140.         PdfDocument pdfDocument = new PdfDocument(writer);
  141.         Document document = new Document(pdfDocument);
  142.  
  143.         pdfDocument.setDefaultPageSize(PageSize.A4);
  144.         document.setMargins(0, 0, 0, 0);
  145.  
  146.         Drawable drawable = getDrawable(R.drawable.assignment_cover);
  147.         Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
  148.         ByteArrayOutputStream stream = new ByteArrayOutputStream();
  149.         bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  150.         byte[] bitmapData = stream.toByteArray();
  151.  
  152.         ImageData imageData = ImageDataFactory.create(bitmapData);
  153.         Image image = new Image(imageData);
  154.  
  155.         float[] width = {160, 160};
  156.         Table table1 = new Table(width);
  157.         table1.setHorizontalAlignment(HorizontalAlignment.CENTER);
  158.  
  159.         //first row
  160.         table1.addCell(new Cell().add(new Paragraph("Course Code").setBold()));
  161.         table1.addCell(new Cell().add(new Paragraph(courseCode).setTextAlignment(TextAlignment.CENTER)));
  162.  
  163.         //second row
  164.         table1.addCell(new Cell().add(new Paragraph("Course Title").setBold()));
  165.         table1.addCell(new Cell().add(new Paragraph(courseTitle).setTextAlignment(TextAlignment.CENTER)));
  166.  
  167.         //third row
  168.         table1.addCell(new Cell().add(new Paragraph("Submission Date").setBold()));
  169.         table1.addCell(new Cell().add(new Paragraph(submissionDate).setTextAlignment(TextAlignment.CENTER)));
  170.  
  171.         float[] widthSecondTable = {200,200};
  172.         Table table2 = new Table(widthSecondTable);
  173.         table2.setHorizontalAlignment(HorizontalAlignment.CENTER);
  174.  
  175.         Log.d(TAG, "createPdf: "+teacherDesignation+" "+studentName+" "+studentId+" "+section+" "+semester);
  176.  
  177.         table2.addCell(new Cell().add(new Paragraph("Submitted To").setTextAlignment(TextAlignment.CENTER)));
  178.         table2.addCell(new Cell().add(new Paragraph("Submitted By").setTextAlignment(TextAlignment.CENTER)));
  179.  
  180.         table2.addCell(new Cell(3,1).add(new Paragraph(teacherName).setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(16)));
  181.         table2.addCell(new Cell(3,1).add(new Paragraph(studentName).setTextAlignment(TextAlignment.CENTER).setBold().setFontSize(16)));
  182.  
  183.         table2.addCell(new Cell().add(new Paragraph(teacherDesignation).setTextAlignment(TextAlignment.CENTER)));
  184.         table2.addCell(new Cell().add(new Paragraph(studentId).setTextAlignment(TextAlignment.CENTER)));
  185.  
  186.         table2.addCell(new Cell().add(new Paragraph("CSE IIUC").setTextAlignment(TextAlignment.CENTER)));
  187.         table2.addCell(new Cell().add(new Paragraph("Semester: "+semester+" "+"Section: "+section).setTextAlignment(TextAlignment.CENTER)));
  188.  
  189.  
  190.         table2.setMarginTop(40);
  191.  
  192.         document.add(image);
  193.         document.add(table1);
  194.         document.add(table2);
  195.  
  196.         document.close();
  197.         Toast.makeText(this, "PDF Created", Toast.LENGTH_SHORT).show();
  198.     }
  199.  
  200.     @Override
  201.     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  202.         String department = adapterView.getItemAtPosition(i).toString();
  203.     }
  204.  
  205.     @Override
  206.     public void onNothingSelected(AdapterView<?> adapterView) {
  207.  
  208.     }
  209.  
  210.     private void updateLabel() {
  211.         String myFormat = "MM/dd/yy"; //In which you need put here
  212.         DateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
  213.  
  214.         binding.dateOfSub.setText(sdf.format(myCalendar.getTime()));
  215.     }
  216.  
  217. }
RAW Paste Data