Convert TXT to PDF in C#, VB, and Java

We recently posted “Convert PDF to TXT in C#, VB, and Java” that uses the LEADTOOLS Document Converter class. 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.

Once you install the LEADTOOLS SDK, take a look at the .NET Core Document Converter demo in the LEADTOOLS21\Examples\DotNetCore\CommandLine\DocumentConverterDemo folder. The Document Converter Demo 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#

using (var documentConverter = new DocumentConverter())   
    var inFile = Path.Combine(ImagesPath.Path, @"input.txt");   
    var outFile = Path.Combine(ImagesPath.Path, @"output.pdf");   
    var format = DocumentFormat.Pdf;   
    var jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format);   
    jobData.JobName = "TXT conversion to PDF";   

    var documentWriter = new DocumentWriter();   
    var job = documentConverter.Jobs.CreateJob(jobData);   

    if (job.Status == DocumentConverterJobStatus.Success)   
        Console.WriteLine("{0} Errors", job.Status);   
        foreach (var error in job.Errors)   
           Console.WriteLine("  {0} at {1}: {2}", error.Operation,
               error.InputDocumentPageNumber, error.Error.Message);   

For more information on this example, check out our full C# tutorial on the LEADTOOLS Document Converter.

TXT to PDF using VB

Using documentConverter As DocumentConverter = New DocumentConverter()
    Dim inFile = Path.Combine(ImagesPath.Path, "input.txt")
    Dim outFile = Path.Combine(ImagesPath.Path, "output.pdf")
    Dim format = DocumentFormat.Pdf
    Dim jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format)
    jobData.JobName = "TXT conversion to PDF"
    Dim documentWriter = New DocumentWriter()
    Dim job = documentConverter.Jobs.CreateJob(jobData)

    If job.Status = DocumentConverterJobStatus.Success Then
        Console.WriteLine("{0} Errors", job.Status)

        For Each [error] In job.Errors
            Console.WriteLine("  {0} at {1}: {2}", [error].Operation, [error].InputDocumentPageNumber, [error].[Error].Message)
    End If
End Using

More information about this example can be found in the Document Converter class documentation.

TXT to PDF using Java

String inFile = Path.Combine(ImagesPath.Path, @"input.txt");   
String outFile = Path.Combine(ImagesPath.Path, @"output.pdf");   
DocumentFormat format = DocumentFormat.Pdf;   

DocumentConverter documentConverter = new DocumentConverter();
DocumentWriter docWriter = new DocumentWriter();

DocumentConverterJobData jobData = 
   DocumentConverterJobs.CreateJobData(inFile, outFile, format);  
jobData.setJobName("TXT Conversion");
DocumentConverterJob job = docConverter.getJobs().createJob(jobData);

if (job.getErrors().size() > 0)
	for (DocumentConverterJobError error : job.getErrors())
		System.out.println("%2fnError during conversion: " + error.getError().getMessage());
	System.out.println("Successfully converted file to " + outFile);

For more information on the Java Document Converter, check out our full tutorial on the LEADTOOLS Java Document Converter.

Try for Free

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days, and comes with free chat and email support.

But Wait! There’s More

Did you see our previous post, “Convert and Base64 Encode Images and PDFs in C#”? Stay tuned for more conversion examples to see how the LEADTOOLS document converter will easily fit 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 via email or call us at 704-332-5532.


Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in Document Converter and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *