XDoc.PDF
Features
Tech Specs
How-to C#
How-to VB.NET
Pricing
C# PDF: How to HTML5 Viewer & Editor PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
Create PDF from Text
  |  
Home ›› XDoc.PDF ›› C# PDF: Create PDF from Text

C#.NET PDF - 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

Note: When you get the error "Could not load file or assembly 'RasterEdge.Imaging.Basic' or any other assembly or one of its dependencies. An attempt to load a program with an incorrect format", please check your configure as follows:

       

       If you are using x64 libraries/dlls, Right click the project -> Properties -> Build -> Platform target: x64.

       

       If using x86, the platform target should be x86.


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




.NET Class Namespace Required



Integrate following RasterEdge C#.NET text to PDF converter SDK dlls into your C#.NET project assemblies;


  RasterEdge.Imaging.Basic.dll


  RasterEdge.Imaging.Basic.Codec.dll


  RasterEdge.Imaging.DICOM.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.Imaging.JBIG2.dll


  RasterEdge.Imaging.JPEG2000.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XDoc.Converter.dll


  RasterEdge.XDoc.Excel.dll


  RasterEdge.XDoc.Office.Inner.Common.dll


  RasterEdge.XDoc.Office.Inner.Office03.dll


  RasterEdge.XDoc.PDF.dll


  RasterEdge.XDoc.PowerPoint.dll


  RasterEdge.XDoc.TIFF.dll


  RasterEdge.XDoc.Word.dll


  RasterEdge.XImage.AdvancedCleanup.Core.dll


  RasterEdge.XImage.OCR.dll


  RasterEdge.XImage.OCR.Tesseract.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XImage.Raster.dll


Use corresponding namespaces;


    using RasterEdge.Imaging.Basic;


    using RasterEdge.XDoc.Converter;