- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Now, use itext pdf API for converting text to pdf. Also, you can download a jar file of this by searching on google or from the below-provided link.
Download Jar file: Download
Documentation: Show Documentation
Simple code for converting Text to Pdf.
In this code, Program takes input from the user as file name, and this file is converted into pdf view. Pdf file names have been given as static like newPdf.pdf but You can change easily or set as by user Input pattern.
Code :
import java.io.*;
import java.util.Scanner;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
class ConvertPdf{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a file name with extension like txt");
String filename = scan.next();
try{
File file = new File(filename);
Document document = new Document();
if(file.exists()){
PdfWriter write = PdfWriter.getInstance(document,new FileOutputStream("newText.pdf"));
FileReader read = new FileReader(file);
BufferedReader br = new BufferedReader(read);
String text = "";
document.open();
while((text = br.readLine())!=null){
System.out.println(text);
document.add(new Paragraph(text));
document.add(Chunk.NEWLINE);
}
document.close();
write.close();
}
else{
System.out.println("Your file in not exist");
}
}catch(Exception e){
System.out.println("Your error is:"+e);
}
}
}
More Questions :
- How to make To-Do List in React
- How to Use Bootstrap in React
- How to run python command in java
If you have any queries, you can put questions on Comment.....
Comments
Post a Comment
Any Query Regarding Article Ask me in comment