How to Start Convert PDF Read PDF Edit PDF PDF Report Builder 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 Text Converter Library
How to convert text file (txt) to PDF document using C#.NET in ASP.NET, Windows apps


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









How to convert text file to PDF document 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