How to Start Convert PDF Read PDF Build PDF Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF SDK Library
Create PDF from Text in C# Using XDoc.PDF SDK for .NET


C#.NET Demo code for Creating PDF from Text with XDoc.PDF Library for .NET in C#









  • Best C# text to PDF converter SDK for converting adobe PDF from TXT in Visual Studio .NET project
  • .NET control for batch converting text formats to editable & searchable PDF document
  • Free .NET library for creating PDF from TXT in both C#.NET WinForms and ASP.NET application
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • All text message can be copied and pasted to PDF file by keeping original layout
  • C#.NET class source code for creating PDF document from rich text in .NET framework project
  • Convert plain text to PDF text with multiple fonts, sizes and colors




Use Text to PDF Converter Library DLLs in C#.NET


Now you can convert text file to PDF document using the C# demo code we have offered below.



        #region text to pdf (file to file)
        internal static void textToPdf()
        {
            String inputFilePath = @"C:\demo.txt";
            String outputFilePath = @"C:\output.pdf";
            DocumentConverter.ToDocument(inputFilePath, outputFilePath, FileType.DOC_PDF);
        }
        #endregion

        #region text to pdf (stream to stream)
        internal static void textStreamToPdf()
        {
            String inputFilePath = @"C:\demo.txt";
            Stream stream = File.Open(inputFilePath, FileMode.Open);
            Stream outputStream = new MemoryStream();
            DocumentConverter.ToDocument(stream, outputStream, FileType.DOC_PDF);
        }
        #endregion




C# convert two or multiple Text files to PDF (batch convert)


        #region text to pdf (batch files)
        internal static void convertTextFilesToPdf()
        {
            String inputDirectory = @"C:\input\";
            String outputDirectory = @"C:\output\";
            String[] files = Directory.GetFiles(inputDirectory, "*.txt");

            foreach (String filePath in files)
            {
                int startIdx = filePath.LastIndexOf("\\");
                int endIdx = filePath.LastIndexOf(".");
                String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);

                DocumentConverter.ToDocument(filePath, outputDirectory + docName + ".pdf", FileType.DOC_PDF);
                
            }
        }
        #endregion




C# combine multiple Text files into PDF


        #region combine text files and convert to pdf
        internal static void combineAndConvertToPdf()
        {
            String[] files = new String[] { @"C:\demo1.txt", @"C:\demo2.txt", @"C:\demo3.txt" };
            String outputFilePath = @"C:\output.pdf";
            List<MemoryStream> streams = new List<MemoryStream>();
            foreach (String filePath in files)
            {
                FileStream inputStream = File.Open(filePath, FileMode.Open);
                MemoryStream outputStream = new MemoryStream();
                DocumentConverter.ToDocument(inputStream, outputStream, FileType.DOC_PDF);
                streams.Add(outputStream);
                inputStream.Close();
            }
            PDFDocument.CombineDocument(streams.ToArray(), outputFilePath);
        }
        #endregion




C# insert Text file into pdf document, and create a new PDF file


        #region insert text to pdf
        internal static void insertTextToPdf()
        {
            String filePath = @"C:\demo.txt";
            Stream inputStream = File.Open(filePath, FileMode.Open);
            MemoryStream desStream = new MemoryStream();
            DocumentConverter.ToDocument(inputStream, desStream, FileType.DOC_PDF);

            PDFDocument pdf = new PDFDocument(desStream);
            int pageCount = pdf.GetPageCount();
            List<BasePage> pages = new List<BasePage>();
            for (int i = 0; i < pageCount; i++)
            {
                pages.Add(pdf.GetPage(i));
            }
            String outputPdf = @"C:\output.pdf";
            PDFDocument desDoc = new PDFDocument(outputPdf);
            int insertLocation = 2;
            desDoc.InsertPages(pages.ToArray(), insertLocation);
            desDoc.Save(@"C:\desDocumcnet.pdf");
        }
        #endregion