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

Posted on 2021-06-17 Katie

text to pdf

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();   
    documentConverter.SetDocumentWriterInstance(documentWriter);   
    
    var job = documentConverter.Jobs.CreateJob(jobData);   
    documentConverter.Jobs.RunJob(job);   

    if (job.Status == DocumentConverterJobStatus.Success)   
        Console.WriteLine("Success");   
    else   
    {   
        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()
    documentConverter.SetDocumentWriterInstance(documentWriter)
    Dim job = documentConverter.Jobs.CreateJob(jobData)
    documentConverter.Jobs.RunJob(job)

    If job.Status = DocumentConverterJobStatus.Success Then
        Console.WriteLine("Success")
    Else
        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)
        Next
    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();
docConverter.setDocumentWriterInstance(docWriter);

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

if (job.getErrors().size() > 0)
	for (DocumentConverterJobError error : job.getErrors())
		System.out.println("%2fnError during conversion: " + error.getError().getMessage());
else
	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.

LEADTOOLS Blog

LEADTOOLS Powered by Apryse,the Market Leading PDF SDK,All Rights Reserved