Text files can be easily edited by any text editing program. If you want to prevent changes when others view the files, you can convert them to PDF. In this article, we will demonstrate how to convert text files to PDF in Java using Spire.PDF for Java.
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>10.10.7</version> </dependency> </dependencies>
Convert Text Files to PDF in Java
The following are the main steps to convert a text file to PDF using Spire.PDF for Java:
- Read the text in the text file into a String object.
- Create a PdfDocument instance and add a page to the PDF file using PdfDocument.getPages().add() method.
- Create a PdfTextWidget instance from the text.
- Draw the text onto the PDF page using PdfTextWidget.draw() method.
- Save the result file using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.FileFormat; import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.*; import java.awt.geom.Rectangle2D; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ConvertTextToPdf { public static void main(String[] args) throws Exception { //Read the text from the text file String text = readTextFromFile("Input.txt"); //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Add a page PdfPageBase page = pdf.getPages().add(); //Create a PdfFont instance PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11); //Create a PdfTextLayout instance PdfTextLayout textLayout = new PdfTextLayout(); textLayout.setBreak(PdfLayoutBreakType.Fit_Page); textLayout.setLayout(PdfLayoutType.Paginate); //Create a PdfStringFormat instance PdfStringFormat format = new PdfStringFormat(); format.setLineSpacing(20f); //Create a PdfTextWidget instance from the text PdfTextWidget textWidget = new PdfTextWidget(text, font, PdfBrushes.getBlack()); //Set string format textWidget.setStringFormat(format); //Draw the text at the specified location of the page Rectangle2D.Float bounds = new Rectangle2D.Float(); bounds.setRect(0,25,page.getCanvas().getClientSize().getWidth(),page.getCanvas().getClientSize().getHeight()); textWidget.draw(page, bounds, textLayout); //Save the result file pdf.saveToFile("TextToPdf.pdf", FileFormat.PDF); } public static String readTextFromFile(String fileName) throws IOException { StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(new FileReader(fileName)); String content = null; while ((content = br.readLine()) != null) { sb.append(content); sb.append("\n"); } return sb.toString(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.