how to add line break in a text read by BufferReader in attempt to convert it to pdf with iText7

Here is the text file I create

     try {
    File info = new File("StudentInfo.txt");
    
    if(!info.exists()) {
        info.createNewFile();
    }
    
    PrintWriter D = new PrintWriter(info);
    
    D.println("Student Name "+" : "+"MUSTAPHA HASSAN;\n"+"Class \t\t"+"\t\t: "+"PRIM 
              6A;\n"+"No.in Class \t "+" : "+"43;\n"+"Position\t\t\t: "+"2nd;");                
    D.close();
    
    }catch (Exception e) {

    }

I got this output for the text File

   Student Name  : MUSTAPHA HASSAN;
   Class                : PRIM 6A;
   No.in Class    : 43;
   Position         : 2nd;

    

Converting Text File to pdf

  try {
    String Z = "C:\\Users\\TEXT.pdf";
    OutputStream D = new FileOutputStream(Z);
    PdfWriter PW = new PdfWriter(D);
    PdfDocument Test = new PdfDocument(PW);
    Document document = new Document(Test, PageSize.A4);
  
      Paragraph T = new Paragraph();
      
       
     public static void PARA (Paragraph T , String line) {
         StringTokenizer tokenizer = new StringTokenizer(line, ";");
         while (tokenizer.hasMoreTokens()) {
             T.add(new Paragraph(tokenizer.nextToken()));
         }
     }

      BufferedReader bf = new BufferedReader(new FileReader("C:\\Users\\StudentInfo.txt"));
        String line = bf.readLine();
        PARA(T , line);
         while ((line = bf.readLine()) != null) {
             PARA(T , line);
         
         }
         bf.close();

     document.add(T);
     document.close();
  }catch (Exception e) {

  }

the pdf output is this

 Student Name : MUSTAPHA HASSANClass : PRIM 6ANo.in Class : 43Position : 2nd

is there any way that the pdf output will Looks the same as that in the text ?

I want the pdf output in this form

   Student Name  : MUSTAPHA HASSAN;
   Class                : PRIM 6A;
   No.in Class    : 43;
   Position         : 2nd;

Guide me please.

1 answer

  • answered 2020-09-23 20:00 Najibu Tanimu

       public static void PARA (Paragraph T , String line) {
         StringTokenizer tokenizer = new StringTokenizer(line, ";");
         while (tokenizer.hasMoreTokens()) {
            //Remove T.add(new Paragraph(tokenizer.nextToken()));
    
         //Replace it with
         T.add(tokenizer.nextToken());
        
         }
    
     }
    

    then as Steve say's "Try PARA(T , line + '\r');"

    finally you have this as output

             Student Name : MUSTAPHA HASSAN
             Class        : PRIM 6A
             No.in Class  : 43
             Position     : 2nd