Creating Pdf with data from editText android












2















I am trying to create a pdf. Data is directly taken from edit text but the problem is that if I write any paragraph in the edit text the final out in pdf is showing all data in 1 line instead of multi-line.
Although pdf is getting created and I can see the output only in 1 single line.
Image links:



Stating activity:
enter image description here



File created:
enter image description here



File viewed:
enter image description here



public class Main2Activity extends AppCompatActivity {
Button btnCreate;
EditText editText,editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

btnCreate = (Button)findViewById(R.id.create);
editText =(EditText) findViewById(R.id.edittext);
editText2 =(EditText) findViewById(R.id.edittext2);
btnCreate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
createPdf(editText.getText().toString(),editText2.getText().toString());
}
});
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createPdf(String title,String description){
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description

PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawText(title, 20, 40, paint);
canvas.drawText(description, 20, 60, paint);
// canvas.drawText(description,1,20,20.0f,30.0f,paint);
//canvas.drawt
// finish the page
document.finishPage(page);
// draw text on the graphics object of the page

// write the document content
String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file = new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String targetPdf = directory_path+title+".pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error "+e.toString());
Toast.makeText(this, "Something wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
}









share|improve this question

























  • i think you have to find the EOF character and change the String to be written into the file accordingly.(using escape sequence)

    – ABr
    Nov 23 '18 at 10:49











  • not helpfull amy other wau

    – Syed Fuzail Abid
    Nov 26 '18 at 6:25











  • createPdf(editText.getText().toString()+"n",editText2.getText().toString());

    – ABr
    Nov 26 '18 at 6:28











  • @Abr not working out

    – Syed Fuzail Abid
    Nov 28 '18 at 9:26











  • try the maxlines property inside xml, maybe

    – ABr
    Nov 28 '18 at 10:20
















2















I am trying to create a pdf. Data is directly taken from edit text but the problem is that if I write any paragraph in the edit text the final out in pdf is showing all data in 1 line instead of multi-line.
Although pdf is getting created and I can see the output only in 1 single line.
Image links:



Stating activity:
enter image description here



File created:
enter image description here



File viewed:
enter image description here



public class Main2Activity extends AppCompatActivity {
Button btnCreate;
EditText editText,editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

btnCreate = (Button)findViewById(R.id.create);
editText =(EditText) findViewById(R.id.edittext);
editText2 =(EditText) findViewById(R.id.edittext2);
btnCreate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
createPdf(editText.getText().toString(),editText2.getText().toString());
}
});
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createPdf(String title,String description){
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description

PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawText(title, 20, 40, paint);
canvas.drawText(description, 20, 60, paint);
// canvas.drawText(description,1,20,20.0f,30.0f,paint);
//canvas.drawt
// finish the page
document.finishPage(page);
// draw text on the graphics object of the page

// write the document content
String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file = new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String targetPdf = directory_path+title+".pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error "+e.toString());
Toast.makeText(this, "Something wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
}









share|improve this question

























  • i think you have to find the EOF character and change the String to be written into the file accordingly.(using escape sequence)

    – ABr
    Nov 23 '18 at 10:49











  • not helpfull amy other wau

    – Syed Fuzail Abid
    Nov 26 '18 at 6:25











  • createPdf(editText.getText().toString()+"n",editText2.getText().toString());

    – ABr
    Nov 26 '18 at 6:28











  • @Abr not working out

    – Syed Fuzail Abid
    Nov 28 '18 at 9:26











  • try the maxlines property inside xml, maybe

    – ABr
    Nov 28 '18 at 10:20














2












2








2


1






I am trying to create a pdf. Data is directly taken from edit text but the problem is that if I write any paragraph in the edit text the final out in pdf is showing all data in 1 line instead of multi-line.
Although pdf is getting created and I can see the output only in 1 single line.
Image links:



Stating activity:
enter image description here



File created:
enter image description here



File viewed:
enter image description here



public class Main2Activity extends AppCompatActivity {
Button btnCreate;
EditText editText,editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

btnCreate = (Button)findViewById(R.id.create);
editText =(EditText) findViewById(R.id.edittext);
editText2 =(EditText) findViewById(R.id.edittext2);
btnCreate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
createPdf(editText.getText().toString(),editText2.getText().toString());
}
});
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createPdf(String title,String description){
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description

PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawText(title, 20, 40, paint);
canvas.drawText(description, 20, 60, paint);
// canvas.drawText(description,1,20,20.0f,30.0f,paint);
//canvas.drawt
// finish the page
document.finishPage(page);
// draw text on the graphics object of the page

// write the document content
String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file = new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String targetPdf = directory_path+title+".pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error "+e.toString());
Toast.makeText(this, "Something wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
}









share|improve this question
















I am trying to create a pdf. Data is directly taken from edit text but the problem is that if I write any paragraph in the edit text the final out in pdf is showing all data in 1 line instead of multi-line.
Although pdf is getting created and I can see the output only in 1 single line.
Image links:



Stating activity:
enter image description here



File created:
enter image description here



File viewed:
enter image description here



public class Main2Activity extends AppCompatActivity {
Button btnCreate;
EditText editText,editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

btnCreate = (Button)findViewById(R.id.create);
editText =(EditText) findViewById(R.id.edittext);
editText2 =(EditText) findViewById(R.id.edittext2);
btnCreate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
createPdf(editText.getText().toString(),editText2.getText().toString());
}
});
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createPdf(String title,String description){
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description

PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawText(title, 20, 40, paint);
canvas.drawText(description, 20, 60, paint);
// canvas.drawText(description,1,20,20.0f,30.0f,paint);
//canvas.drawt
// finish the page
document.finishPage(page);
// draw text on the graphics object of the page

// write the document content
String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file = new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String targetPdf = directory_path+title+".pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error "+e.toString());
Toast.makeText(this, "Something wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
}






java android android-layout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 8:22









Akshay Paliwal

2,17912436




2,17912436










asked Nov 23 '18 at 10:31









Syed Fuzail AbidSyed Fuzail Abid

112




112













  • i think you have to find the EOF character and change the String to be written into the file accordingly.(using escape sequence)

    – ABr
    Nov 23 '18 at 10:49











  • not helpfull amy other wau

    – Syed Fuzail Abid
    Nov 26 '18 at 6:25











  • createPdf(editText.getText().toString()+"n",editText2.getText().toString());

    – ABr
    Nov 26 '18 at 6:28











  • @Abr not working out

    – Syed Fuzail Abid
    Nov 28 '18 at 9:26











  • try the maxlines property inside xml, maybe

    – ABr
    Nov 28 '18 at 10:20



















  • i think you have to find the EOF character and change the String to be written into the file accordingly.(using escape sequence)

    – ABr
    Nov 23 '18 at 10:49











  • not helpfull amy other wau

    – Syed Fuzail Abid
    Nov 26 '18 at 6:25











  • createPdf(editText.getText().toString()+"n",editText2.getText().toString());

    – ABr
    Nov 26 '18 at 6:28











  • @Abr not working out

    – Syed Fuzail Abid
    Nov 28 '18 at 9:26











  • try the maxlines property inside xml, maybe

    – ABr
    Nov 28 '18 at 10:20

















i think you have to find the EOF character and change the String to be written into the file accordingly.(using escape sequence)

– ABr
Nov 23 '18 at 10:49





i think you have to find the EOF character and change the String to be written into the file accordingly.(using escape sequence)

– ABr
Nov 23 '18 at 10:49













not helpfull amy other wau

– Syed Fuzail Abid
Nov 26 '18 at 6:25





not helpfull amy other wau

– Syed Fuzail Abid
Nov 26 '18 at 6:25













createPdf(editText.getText().toString()+"n",editText2.getText().toString());

– ABr
Nov 26 '18 at 6:28





createPdf(editText.getText().toString()+"n",editText2.getText().toString());

– ABr
Nov 26 '18 at 6:28













@Abr not working out

– Syed Fuzail Abid
Nov 28 '18 at 9:26





@Abr not working out

– Syed Fuzail Abid
Nov 28 '18 at 9:26













try the maxlines property inside xml, maybe

– ABr
Nov 28 '18 at 10:20





try the maxlines property inside xml, maybe

– ABr
Nov 28 '18 at 10:20












2 Answers
2






active

oldest

votes


















0














I think that's because multiline is property of EditText, not String, so if you read it from ET it will always be in single line. You can try to set max line length, and then read characters to limit, and then add it to new String, for example ArrayList<String> (you don't know how long text will be). If line limit doesn't end with , or . or space, then you read it further till , or . or space occurs. After all that you add whole ArrayList to your pdf, ending with n each row.






share|improve this answer































    0














    Try splitting the string you get from the editText with some predefined Length and then using it in canvas.drawText()



    while(description.length()>15){
    if(description.length()>15){
    String first = description.substring(0,15);
    canvas.drawText(description, 20, 60, paint);
    }
    else{
    String second =description.substring(0,description.length());
    canvas.drawText(description, 20, 60, paint);
    }

    }


    I have not tried this one. This might work






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444986%2fcreating-pdf-with-data-from-edittext-android%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I think that's because multiline is property of EditText, not String, so if you read it from ET it will always be in single line. You can try to set max line length, and then read characters to limit, and then add it to new String, for example ArrayList<String> (you don't know how long text will be). If line limit doesn't end with , or . or space, then you read it further till , or . or space occurs. After all that you add whole ArrayList to your pdf, ending with n each row.






      share|improve this answer




























        0














        I think that's because multiline is property of EditText, not String, so if you read it from ET it will always be in single line. You can try to set max line length, and then read characters to limit, and then add it to new String, for example ArrayList<String> (you don't know how long text will be). If line limit doesn't end with , or . or space, then you read it further till , or . or space occurs. After all that you add whole ArrayList to your pdf, ending with n each row.






        share|improve this answer


























          0












          0








          0







          I think that's because multiline is property of EditText, not String, so if you read it from ET it will always be in single line. You can try to set max line length, and then read characters to limit, and then add it to new String, for example ArrayList<String> (you don't know how long text will be). If line limit doesn't end with , or . or space, then you read it further till , or . or space occurs. After all that you add whole ArrayList to your pdf, ending with n each row.






          share|improve this answer













          I think that's because multiline is property of EditText, not String, so if you read it from ET it will always be in single line. You can try to set max line length, and then read characters to limit, and then add it to new String, for example ArrayList<String> (you don't know how long text will be). If line limit doesn't end with , or . or space, then you read it further till , or . or space occurs. After all that you add whole ArrayList to your pdf, ending with n each row.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 6:48









          SkypeDoggSkypeDogg

          8711




          8711

























              0














              Try splitting the string you get from the editText with some predefined Length and then using it in canvas.drawText()



              while(description.length()>15){
              if(description.length()>15){
              String first = description.substring(0,15);
              canvas.drawText(description, 20, 60, paint);
              }
              else{
              String second =description.substring(0,description.length());
              canvas.drawText(description, 20, 60, paint);
              }

              }


              I have not tried this one. This might work






              share|improve this answer




























                0














                Try splitting the string you get from the editText with some predefined Length and then using it in canvas.drawText()



                while(description.length()>15){
                if(description.length()>15){
                String first = description.substring(0,15);
                canvas.drawText(description, 20, 60, paint);
                }
                else{
                String second =description.substring(0,description.length());
                canvas.drawText(description, 20, 60, paint);
                }

                }


                I have not tried this one. This might work






                share|improve this answer


























                  0












                  0








                  0







                  Try splitting the string you get from the editText with some predefined Length and then using it in canvas.drawText()



                  while(description.length()>15){
                  if(description.length()>15){
                  String first = description.substring(0,15);
                  canvas.drawText(description, 20, 60, paint);
                  }
                  else{
                  String second =description.substring(0,description.length());
                  canvas.drawText(description, 20, 60, paint);
                  }

                  }


                  I have not tried this one. This might work






                  share|improve this answer













                  Try splitting the string you get from the editText with some predefined Length and then using it in canvas.drawText()



                  while(description.length()>15){
                  if(description.length()>15){
                  String first = description.substring(0,15);
                  canvas.drawText(description, 20, 60, paint);
                  }
                  else{
                  String second =description.substring(0,description.length());
                  canvas.drawText(description, 20, 60, paint);
                  }

                  }


                  I have not tried this one. This might work







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '18 at 10:43









                  ABrABr

                  27027




                  27027






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444986%2fcreating-pdf-with-data-from-edittext-android%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen