Convert TXT file to PDF document in .NET

Blog category: PDFOffice

July 1, 2021

It is often required to convert formatted text into a PDF document. If text has a complex formatting, it can take a lot of time and code to convert the formatted text to PDF content.

Since version 10.1 of VintaSoft Imaging .NET SDK became possible to edit existing DOCX and XLSX documents. One can use this feature to get rid of the need to convert a formatted text to PDF content programmatically. Instead of writing a lot of code for converting formatted text, one can create a DOCX document with necessary formatting and insert the required text content into the already formatted document. After that, the created DOCX document can be easily converted to PDF document using features of VintaSoft Imaging .NET SDK.

Based on the way described above, one can easily create a TXT to PDF converter following the steps below:
1. Using MS Word create DOCX document txtTemplate.docx:
2. In the application code:
This approach provides the flexibility and ease of customization for converter: to change the parameters of output document one only needs to change the parameters of txtTemplate.docx template using MS Word.


Here is a code example, which demonstrates how to convert TXT file to PDF document:
// The project, which uses this code, must have references to the following assemblies:
// - Vintasoft.Imaging
// - Vintasoft.Imaging.Office.OpenXml
// - Vintasoft.Imaging.Pdf

/// <summary>
/// Tests conversion from TXT file to a PDF document.
/// </summary>
public static void Test()
{
    ConvertTxtFileToPdfDocument("Products.txt", "txtTemplate.docx", "Products.pdf");
}

/// <summary>
/// Converts specified text file to a PDF document.
/// </summary>
/// <param name="txtFilename">The TXT filename.</param>
/// <param name="docxTemplateFilename">The DOCX template filename.</param>
/// <param name="outputPdfFilename">The output PDF filename.</param>
public static void ConvertTxtFileToPdfDocument(string txtFilename, string docxTemplateFilename, string outputPdfFilename)
{
    string text = System.IO.File.ReadAllText(txtFilename, System.Text.Encoding.Unicode);
    ConvertTextToPdfDocument(System.IO.Path.GetFileName(txtFilename), text, docxTemplateFilename, outputPdfFilename);
}

/// <summary>
/// Converts specified text to a PDF document.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="title">The document title.</param>
/// <param name="docxTemplateFilename">The DOCX template filename.</param>
/// <param name="outputPdfFilename">The output PDF filename.</param>
public static void ConvertTextToPdfDocument(string title, string text, string docxTemplateFilename, string outputPdfFilename)
{
    // create DocxDocumentEditor and use DOCX file as a document template
    using (Vintasoft.Imaging.Office.OpenXml.Editor.DocxDocumentEditor editor =
        new Vintasoft.Imaging.Office.OpenXml.Editor.DocxDocumentEditor(docxTemplateFilename))
    {
        // replace text "[title]" by document title
        editor.Body.ReplaceText("[title]", title);

        // if document body does not contain text "[content]"
        if (!editor.Body.Contains("[content]"))
            throw new ArgumentException("Template file must be contains '[content]' text.");

        // replace text "[content]" by text content
        editor.Body["[content]"] = text;

        // export DOCX document to a PDF document
        editor.Export(outputPdfFilename);
    }
}

Share this article