AVAILABLE NOW: Spring 2025 Release

Convert TXT to PDF in C# and Java

By Apryse | 2025 Apr 08

Sanity Image
Read time

5 min

We recently posted “Convert PDF to TXT in C# and Java” that uses the Apryse SDK. Now, we are going to go the other way around and convert TXT to PDF. The main difference is that conversion from text to PDF is less complicated because the source TXT file is always text based. On the other hand, PDF files can be text or image based. Converting an image to text requires OCR. Converting Text to PDF does not matter if the resulting PDF is image- or text-based.

The Apryse SDK converts documents as well as images to document formats such as PDF. The resulting PDF files can be text searchable or image based.

To show how this works before downloading the SDK, a simplified version of the code to convert an TXT file to PDF is below:

TXT to PDF using C#

Copied to clipboard

using pdftron;  

using pdftron.Common;  

using pdftron.PDF;  

using pdftron.SDF;  

  

public class ConvertTest  

{  

  

   static void Main()  

   {  

      PDFNet.Initialize("Place Apryse license key here");  

  

      string inputFile = @"path to input TXT file";  

      string outputFile = @"path to output PDF file";  

  

      try  

      {  

         using (PDFDoc pdfdoc = new PDFDoc())  

         {  

            // Add a dictionary  

            ObjSet set = new ObjSet();  

            Obj options = set.CreateDict();  

  

            // Convert from .txt file  

            Console.WriteLine("Converting from txt");  

            pdftron.PDF.Convert.FromText(pdfdoc, inputFile, options);  

            pdfdoc.Save(outputFile, SDFDoc.SaveOptions.e_remove_unused);  

            Console.WriteLine("Saved" + outputFile);  

         }  

      }  

      catch (PDFNetException e)  

      {  

         Console.WriteLine(e.Message);  

      }  

   }  

}  

For more information on this example, check out our full tutorial on the Apryse SDK.

TXT to PDF using Java

Copied to clipboard

import com.pdftron.common.PDFNetException;  

import com.pdftron.pdf.Convert;  

import com.pdftron.pdf.PDFDoc;  

import com.pdftron.pdf.PDFNet;  

import com.pdftron.sdf.Obj;  

import com.pdftron.sdf.ObjSet;  

import com.pdftron.sdf.SDFDoc;  

  

public class Main {  

  

  public static void main(String[] args) {  

  

    PDFNet.initialize("Place Apryse license key here");  

  

    String inputPath = "path to input TXT file";  

    String outputPath = "path to output PDF file";  

    try (PDFDoc pdfdoc = new PDFDoc()) {  

  

      // add a dictionary  

      ObjSet set = new ObjSet();  

      Obj options = set.createDict();  

  

      System.out.println("Converting from txt");  

      Convert.fromText(pdfdoc, inputPath, options);  

      pdfdoc.save(outputPath, SDFDoc.SaveMode.REMOVE_UNUSED, null);  

      System.out.println("Saved: " + outputPath);  

  

    } catch (PDFNetException e) {  

      System.out.println(e);  

    }  

  }  

}  

For more information on this example, check out our full tutorial on the Apryse SDK.

See For Yourself - Free Evaluation

Copied to clipboard

Download the Apryse SDK for free. It’s fully functional and comes with free chat and email support.

But Wait! There’s More

Stay tuned for more conversion examples to see how Apryse easily fits into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter