How to create PDF file in your Android App? Complete source code using Android Studio

Поділитися
Вставка
  • Опубліковано 9 лис 2024

КОМЕНТАРІ • 89

  • @aryanbhaskar7872
    @aryanbhaskar7872 2 роки тому

    Have you used any of pdf library... Or can it convert hindi text into pdf.format..🤔

  • @harisulislam6302
    @harisulislam6302 2 роки тому

    Thank you. This very useful.
    I want to print sql data with the button click. Can you you help me?

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      Below page would help:
      programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
      There are lots of videos on my portal on interacting with database through SQL query. Please explore them in the below:
      programmerworld.co/android/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @karthikgajula2486
    @karthikgajula2486 5 років тому +1

    Thank you it was very helpful and it worked.

  • @akarshitkumar6744
    @akarshitkumar6744 3 роки тому

    vedio is good but also zoom the code and type

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому +1

      Thanks for the suggestion. Yes, from next video we will use zoom feature to show the texts more clearly.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @RanjeetAndani-dp6ok
    @RanjeetAndani-dp6ok 5 років тому +1

    this is help full thank you

  • @tengkubelmiro8336
    @tengkubelmiro8336 4 роки тому +1

    uses or overrides a deprecated API.
    Recompile with -Xlint:deprecation for details.

  • @baishakheemardi8364
    @baishakheemardi8364 3 роки тому

    Thank you sir this video is very usefull for me. But i want to know if my first page is full how to go next page. Please help sir.

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      Just keep adding the pages in your pdfDocument variable. Create multiple pages using PdfDocument.pages type and then add it to the main pdfDocument variable before writing it to the file.
      For reference, source code of this tutorial is given below:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio
      Cheers
      Programmer World
      programmerworld.co
      -

  • @shokievarun8223
    @shokievarun8223 3 роки тому

    Sir , retrieve data from firebase and create pdf in listview items
    I searched this in GitHub, stack overflow and still I couldn't find the code
    It will be more helpful
    If you make that video will be helpful for many

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      Below link shows steps to create PDF from SQLite database.
      programmerworld.co/android/how-to-fetch-data-from-sqlite-database-and-put-that-in-a-pdf-file-in-your-android-app-source-code/
      Same can be done for Firebase database. For steps on how to get data from Firebase and put it on ListView, refer to the below:
      programmerworld.co/android/how-to-develop-an-android-app-for-your-retail-business-b2c-using-firebase-database-source-code/
      Cheers
      Programmer World
      programmerworld.co/
      -

  • @evanspr1403
    @evanspr1403 4 роки тому

    Hello, is there a way to do this, but with an image that is selected from the gallery and not previously loaded on a route?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому +1

      I think that's what is shown in the below video. In the below file name (directory path) you have to just change it to your desired path (gallery in your case).
      private String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + “/myCamera/”;
      String file = directory + “3.jpg”;
      The source code is available at:
      programmerworld.co/android/how-to-convert-jpg-image-file-into-pdf-file-in-your-android-app-complete-source-code/
      Good Luck
      Programmer World
      programmerworld.co
      -

  • @manojrai561
    @manojrai561 4 роки тому

    How to create tables, nested tables, header and footer by using this inbuild pdf library?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      This App uses Canvas concept to write the PDF pages as in the below line of code:
      myPage.getCanvas()
      (Refer: programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/)
      The various methods available for the Canvas is mentioned in the below link:
      developer.android.com/reference/android/graphics/Canvas
      So, say for example, for tables you may use drawRect method. AFAIK, there is no direct methods to do the complete editing as mentioned in your query. There are some 3rd party libraries but then maintenance of such libraries should be considered while using them.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @ملخصالمباريات-ر9ط8ث

    good job
    When writing long text that goes beyond the page boundaries, how do I make it drop a line?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      Simply put a "
      " character. It should insert a new line entry in you document.
      So, basically in the below line of code, instead of "line" add "
      ":
      myPage.getCanvas().drawText(line, x, y, myPaint);
      I hope this solves your problem.
      Source code shown in this tutorial can be found in the below link for reference:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @ملخصالمباريات-ر9ط8ث
      @ملخصالمباريات-ر9ط8ث 4 роки тому

      @@ProgrammerWorld You did the same as you did, but when the long text goes beyond the page
      When I create a paragraph on an A4 page it continues, but in this form it does not
      Is there a way to specify text, for example, starting from X25 and ending at y250?

    • @ملخصالمباريات-ر9ط8ث
      @ملخصالمباريات-ر9ط8ث 4 роки тому

      @@ProgrammerWorld You are here, x10 is twice the text that starts from left to right and y 25 text starts from top to bottom and the paper size is 300 width and 600 cm.
      Here I need to stop the text at 290 wide and 590 long
      How do I do it?

  • @selimozbay
    @selimozbay 3 роки тому

    How to open the PDF file automatically after create it?

  • @felipepereira3061
    @felipepereira3061 5 років тому

    It is possible to put images from camera on pdf file too?

    • @ProgrammerWorld
      @ProgrammerWorld  5 років тому

      You should refer to my below video in this regard:
      ua-cam.com/video/sOKV3iHl5aM/v-deo.html
      I hope this video is helpful.

  • @mydevelopments46
    @mydevelopments46 3 роки тому

    Hi,
    There is a string data in the database. The length of this data varies. How do we fit this string data into pdf document according to a certain width and height? There should be more than one page depending on the size of the data. How can i do it?

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      Just check the total length of the string received from the database. If it is longer than one page's content (depending on the page length and width) then split the text into multiple pages.
      To create multiple pages, just keep adding the pages as shown in the below lines of code:
      PdfDocument.Page myPage1 = myPdfDocument.startPage(myPageInfo);
      // Write into myPage1
      myPdfDocument.finishPage(myPage1);
      PdfDocument.Page myPage2 = myPdfDocument.startPage(myPageInfo);
      // Write into myPage2
      myPdfDocument.finishPage(myPage2);
      . . .
      PdfDocument.Page myPagen = myPdfDocument.startPage(myPageInfo);
      // Write into myPagen
      myPdfDocument.finishPage(myPagen);
      For reference, the complete source code shown in this tutorial is also shared in the below link:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @mydevelopments46
      @mydevelopments46 3 роки тому

      @@ProgrammerWorld Thank you for response. How can I establish the relationship between page height and width and string length?

  • @sameditions6928
    @sameditions6928 4 роки тому

    Please give tutorial on how to convert pdf to word in android studio

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      Thanks for the suggestion. Will try to make a video on this topic.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @AtharvaJaiswal-yl5vs
    @AtharvaJaiswal-yl5vs Рік тому

    I run in my mobile i cant see where is the pdf or it havent been created! please help me

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      As per the code shown in this video, the file gets created at the below:
      String myFilePath = Environment.getExternalStorageDirectory().getPath() + “/myPDFFile.pdf”;
      So, the location of the file should be in the root folder of your phone's storage, for emulator it is named as - sdk_gphone64_x86_64. But this name differ for your device. The easiest way to navigate to this folder will be to go to the download folder of your device and then go one level up in the directory structure.
      For reference, details shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @audib8384
    @audib8384 3 роки тому

    Its not working for text more than 1 pages. How do i make it??

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      It will work for multiple pages also.
      Simply keep adding the pages in the myPdfDocument as shown below:
      // Adding 1st page:
      PdfDocument.PageInfo myPageInfo = new PdfDocument.PageInfo.Builder(300,600,1).create();
      PdfDocument.Page myPage = myPdfDocument.startPage(myPageInfo);
      Paint myPaint = new Paint();
      String myString = myEditText.getText().toString();
      int x = 10, y=25;
      for (String line:myString.split(“
      ”)){
      myPage.getCanvas().drawText(line, x, y, myPaint);
      y+=myPaint.descent()-myPaint.ascent();
      }
      myPdfDocument.finishPage(myPage);
      /****************************************************/
      // Repeating the code for 2nd page:
      PdfDocument.PageInfo myPageInfo2 = new PdfDocument.PageInfo.Builder(300,600,2).create();
      PdfDocument.Page myPage2 = myPdfDocument.startPage(myPageInfo2);
      Paint myPaint2 = new Paint();
      String myString2 = "Some Text"
      int x = 10, y=25;
      for (String line:myString2.split(“
      ”)){
      myPage2.getCanvas().drawText(line, x, y, myPaint2);
      y+=myPaint2.descent()-myPaint2.ascent();
      }
      myPdfDocument.finishPage(myPage2);
      /****************************************************/
      // Repeat further for 3rd page
      .... and so on.
      So, the above should help in creating a multiple page pdf.
      For reference, complete source code shown in this project is available in the below link:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Cheers
      Programmer World
      programmerworld.co/
      -

  • @raedjaber3090
    @raedjaber3090 4 роки тому

    Hello, I did exactly as in the video. Everything works properly on the Simulator, but on the phone I can't open the pdf file,
    it shows an error: "Cannot display PDF (pdfname.pdf is of invalid format)"

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      It should work on the phone also. Please check for your the source code of this project at the below link:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @youcoool12
    @youcoool12 5 років тому

    How can i change the text size and maybe make some text bold or italic?

    • @ProgrammerWorld
      @ProgrammerWorld  5 років тому +1

      You should use paint settings for your page.
      The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
      More options of paint can be seen in below Android developer portal:
      developer.android.com/reference/android/graphics/Paint.html

  • @walidpivano117
    @walidpivano117 4 роки тому

    It is possible to load pdf file to firebase?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      Yes, it is possible to use Firebase database for storing files such as pdf. You can watch my below video which explains the steps to create your end to end encryption chat App using Firebase database:
      ua-cam.com/video/bVXT_AkHIEQ/v-deo.html
      Though in my tutorial it shows only text interaction with Firebase but similar steps can be used for files. I will try to create another video to show these steps.
      Thanks for bringing this up.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @gwintowniagwt4204
    @gwintowniagwt4204 4 роки тому

    Hello, I did exactly as in the video. Everything works properly on the Simulator, but not on the phone. Why?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      There could be various reasons for the App to not work on the phone.
      Check whether required permissions are given to the App in the phone. Also check whether the path to which the file is being written exists and you have write permission on that.
      Good Luck
      Programmer World
      programmerworld.co
      -

  • @anikbiswas6775
    @anikbiswas6775 4 роки тому

    sir if i want to extract text from pdf in android studio what should i do?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      I think what you are looking for is explained in my below video tutorial:
      ua-cam.com/video/lOnBeHaRNZc/v-deo.html
      This video explains how to create your PDF reader Android App.
      I hope it is useful. Let me know if it helped.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @syedazeemaftab7930
    @syedazeemaftab7930 5 років тому

    can you please demonstrate how to fetch data from the sqlite database and put that in the pdf?

    • @ProgrammerWorld
      @ProgrammerWorld  5 років тому

      You can watch below video for the same:
      ua-cam.com/video/uqyY7jM2cZI/v-deo.html
      -

  • @fakhrulsyuja6466
    @fakhrulsyuja6466 4 роки тому

    how to use more edit text to creat pdf file?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      I think, you can simply add more edit texts in your layout and in your Java code collect the texts from all the edit texts in one single string and then use it to write to your PDF.
      Cheers
      Programmer World
      programmerworld.co
      -

    • @fakhrulsyuja6466
      @fakhrulsyuja6466 4 роки тому

      @@ProgrammerWorld done, i can add many text haha.. thanks for your tutorial dude.. wish all u the best 👍

  • @refthaadjiedwimaputra
    @refthaadjiedwimaputra 3 роки тому

    Why doesn't it work in my android studio v4.1?

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      It should work in Android Studio 4.1 version also as long as the chosen API level is 19 and above.
      It is because this App uses PdfDocument Class to create the PDF. As this class was Added in API level 19 of Android which is basically Android 4.4(Kitkat), so API level of 19 and above should be chosen while creating this App.
      Cheers
      Programmer World
      programmerworld.co
      -

    • @refthaadjiedwimaputra
      @refthaadjiedwimaputra 3 роки тому

      @@ProgrammerWorld I've selected API level 30. but still doesn't work, WRITE_EXTERNAL_STORAGE no longer provides write access when targeting Android 11+, even when using requestLegacyExternalstorage. then what should I do?

  • @aniketkushwaha1747
    @aniketkushwaha1747 4 роки тому

    sir it did'nt worked i am having a error in logcat which says :
    java.io.FileNotFoundException: /storage/emulated/0/text_recognition.pdf: open failed: EACCES (Permission denied)
    the device i am using does not have a sdcard inserted can you please help me i want to save it in my specified folder so that it is available from the storage also.

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому +1

      Either you do not have permission to write to an external storage or as you mentioned the external storage does not exists. You can always write your file to another location such as download folder using: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
      For complete source code of this tutorial, you can refer to the below link:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Hope it helps.
      Good Luck
      Programmer World
      programmerworld.co
      -

    • @aniketkushwaha1747
      @aniketkushwaha1747 4 роки тому

      @@ProgrammerWorld
      I am using android 10 and the android documentation says that getExternalStoragePublicDirectory() is deprecated in api29 and the other methods like getExternalFilesDir() it stores the file in storage/android/data/domain_name/files section but i want to store it at storage/download can you tell me how to do that.

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      @@aniketkushwaha1747 Even if it is deprecated, it will work. For alternate APIs, please check Android developer portal. They will definitely suggest workaround.
      Good Luck
      Programmer World
      programmerworld.co
      -

  • @aswathysanthosh8217
    @aswathysanthosh8217 3 роки тому

    sir it did'nt worked i am having a error

  • @udaogugua8249
    @udaogugua8249 4 роки тому

    Sir, I understand everything, but please can you tell me the code e.g. while reading book on PDF and you cut it and turn it one again and then still remain the chapter you've stopped reading. Thanks for sharing it to me.

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      I assume that you are looking for a pdf reader App here. How to create a pdf reader App is explained in the below video:
      ua-cam.com/video/lOnBeHaRNZc/v-deo.html
      Now, in this reader App, if you want to read any specific page then just set the page number in the below line of code:
      stringParser = PdfTextExtractor.getTextFromPage(pdfReader, 1).trim();
      The complete source code of this App can be found in the below link:
      programmerworld.co/android/how-to-create-your-pdf-reader-android-app-complete-source-code/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @udaogugua8249
      @udaogugua8249 4 роки тому +1

      Thanks. You're one of the best.

    • @udaogugua8249
      @udaogugua8249 4 роки тому

      Sir, please that place you add only number 1 I just want to see my last page if I resume the app. How can I write that code in order not see only number 1 page. Thanks

  • @abhijeetsingh2842
    @abhijeetsingh2842 3 роки тому

    i will show error, when I click on the button, on text view error is printing and pdf is not generating

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      I assume the error is being thrown from the below catch part of the code:
      try {
      myPdfDocument.writeTo(new FileOutputStream(myFile));
      }
      catch (Exception e){
      e.printStackTrace();
      myEditText.setText(“ERROR”);
      }
      [The above piece code is taken from: programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/]
      Now, the most likely reason for this ERROR will be that the file in which you are trying is either does not exists or there is some issue with the myFile variable accessing the correct file.
      To resolve this, I will suggest to try with some other filename and in some other location like App's data folder, one such example is shown in the below:
      programmerworld.co/android/how-to-create-microsoft-word-document-docx-file-from-your-android-app/
      Even then the issue is there then try to debug the code and check the exception thrown. If you can copy the value of e (Exception) thrown in the catch part here, we can try to check on that.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @manojbagul1927
    @manojbagul1927 4 роки тому

    is application ko share ka batton kase lagaye sir plz bataye

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      I do not have any video which shows the steps to share the file. However, if interested, you can watch my below video which shows how you can navigate to another App such gmaill, whatsapp, Facebook, etc.
      ua-cam.com/video/XvH748sImco/v-deo.html
      Good Luck
      Programmer World
      programmerworld.co
      -

  • @dhivyajaisankar142
    @dhivyajaisankar142 3 роки тому

    sir how to share pdf f iles programatically kindly help

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      Below details should help in sharing the file through other Apps, such as Email client, WhatsApp, etc.:
      programmerworld.co/android/how-to-share-file-or-text-on-gmail-whatsapp-sms-bluetooth-from-your-android-app-share-button/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @razushikh4553
    @razushikh4553 5 років тому

    sir, thank you for your valuable tutorials.
    Can I make pdf in bangla text????

    • @ProgrammerWorld
      @ProgrammerWorld  5 років тому +1

      I think that should be possible. Just check setTextLocale option in the style of the PDF page you are creating. They support all major languages and locale of the world. Refer to the link below:
      developer.android.com/reference/android/graphics/Paint#setTextLocale(java.util.Locale)
      By the way complete source code of this tutorial is shared at:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Hope this is useful.

    • @anikbiswas6775
      @anikbiswas6775 4 роки тому

      Razu bro does it work for bagla language?

    • @razushikh4553
      @razushikh4553 4 роки тому +1

      @@anikbiswas6775 yes, bro.

    • @anikbiswas6775
      @anikbiswas6775 4 роки тому

      razu vai,ki change kora lagbe code e?naki ja ache tai hobe?

    • @razushikh4553
      @razushikh4553 4 роки тому

      @@anikbiswas6775 myPaint.setTextLocal(new Local ("bn"));

  • @PJPRODUCTIONS_155
    @PJPRODUCTIONS_155 3 роки тому

    it is showing error what should i do?

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      What is the error you are observing? Can you please elaborate?
      By the way for reference, complete source code shown in this project is available in the below link as well:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Cheers
      Programmer World
      programmerworld.co/
      -

  • @dev.faizan
    @dev.faizan 4 роки тому

    hi i add 2 pdfs in my app and app size become 20 mb but my pdf size is less then 500 KB
    WHY this happen?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому +1

      Theoretically, this should not happen as the size of App is independent of the files it creates.
      In practice, it might be happening because of cache memory. Certain Apps in Android keeps lots of information in cache. So, it depends upon the API you have used to add the PDFs that what all information it has saved in cache.
      Killing the App from task manager is one of the easiest way to clean the cache.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @TheNoobDhruv
    @TheNoobDhruv 2 роки тому

    image se pdf file kaise bnaye

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      Refer below link:
      programmerworld.co/android/how-to-convert-jpg-image-file-into-pdf-file-in-your-android-app-complete-source-code/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @nadiaaidan5225
    @nadiaaidan5225 4 роки тому

    It doesn’t work for me i dont find à pdf on the files

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      Any error you are getting?
      Try to create the file in some other folders such as downloads or image folder. Check if you have the "write to external storage" permission for this App.
      Good Luck
      Programmer World
      programmerworld.co
      -

    • @refthaadjiedwimaputra
      @refthaadjiedwimaputra 3 роки тому

      Me too.

  • @pendidikindonesia
    @pendidikindonesia 4 роки тому

    why i always get error in PdfDocument
    The error is:
    Call requires API level 19(current min is 14):new android.graphics.PdfDocument
    can you help me for fix it sir?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      Because your minimum SDK is set to 14.
      Please go to your build.gradle file (App level and not project level) and change the minSdkVersion attribure to 19 in this file. Please make sure to sync the gradle file when prompted after this change to have the changes reflected in your project.
      Good Luck
      Programmer World
      programmerworld.co
      -

  • @blakeyoung115
    @blakeyoung115 3 роки тому

    this code does not work.

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      For latest version of Android, please use code from below video:
      ua-cam.com/video/RcxtWbrT5BI/v-deo.html
      Cheers
      Programmer World
      programmerworld.co
      -