How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator 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 CSV with .NET XDoc.PDF SDK in C#


Advanced .NET PDF Component Allows C# Users to Create PDF from CSV File in C# Programming Language in .NET Project











If you need to convert CSV to PDF document, it's unnecessary to convert CSV files to .xls or .xlsx formats before converting to PDF. With our C#.NET RasterEdge CSV to PDF conversion control, CSV can be converted to PDF file directly. Please refer to following C# demo code to have a quick evaluation of our CSV PDF conversion function.





C# convert single csv file to PDF


The C# source code below shows how to convert a csv file to PDF document in file path or Stream object in C#.NET project.



        #region csv file to pdf (file to file)
        internal static void csvToPdf()
        {
            String inputPath = @"C:\demo.csv";
            String outputPath = @"C:\output.pdf";
            //convert .docx to .pdf
            CSVDocument doc = new CSVDocument(inputPath);
            doc.ConvertToDocument(DocumentType.PDF, outputPath);
        }
        #endregion

        #region csv to pdf (stream to stream)
        internal static void csvStreamToPdf()
        {
            String inputPath = @"";
            byte[] arr = File.ReadAllBytes(inputPath);
            Stream inputStream = new MemoryStream(arr);
            CSVDocument doc = new CSVDocument(inputStream);
            Stream outputStream = new MemoryStream();
            doc.ConvertToDocument(DocumentType.PDF, outputStream);
        }
        #endregion




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


Here you can convert multiple CSV files or all CSV files under one directory to PDF files using C#



        #region csv to pdf (batch files and single tread)
        internal static void csvFilesToPdf()
        {
            String inputDirectory = @"C:\input\";
            String outputDirectory = @"C:\output\";
            String[] files = Directory.GetFiles(inputDirectory, "*.csv");
            foreach (String filePath in files)
            {
                int startIdx = filePath.LastIndexOf("\\");
                int endIdx = filePath.LastIndexOf(".");
                String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
                CSVDocument doc = new CSVDocument(filePath);
                doc.ConvertToDocument(DocumentType.PDF, outputDirectory + docName + ".pdf");
            }
        }
        #endregion

        #region csv to pdf (batch file and multiple threads)
        internal static void csvfiles2pdf()
        {
            String inputDirectory = @"C:\input\";
            String outputDirectory = @"C:\output\";
            String[] files = Directory.GetFiles(inputDirectory, "*.csv");
            List<ConversionArgs> args = new List<ConversionArgs>();
            foreach (String filePath in files)
            {
                int startIdx = filePath.LastIndexOf("\\");
                int endIdx = filePath.LastIndexOf(".");
                String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
                ConversionArgs arg = new ConversionArgs(filePath, outputDirectory + docName + ".pdf");
                args.Add(arg);
            }

            List<Thread> threads = new List<Thread>();
            foreach (ConversionArgs arg in args)
            {
                Thread thread = new Thread(csvToPdfThread);
                thread.Start(arg);
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }
        }

        private static void csvToPdfThread(object args)
        {
            ConversionArgs toPdfArgs = (ConversionArgs)args;
            CSVDocument srcDoc = new CSVDocument(toPdfArgs.SrcPath);
            if (srcDoc != null)
            {
                srcDoc.ConvertToDocument(DocumentType.PDF, toPdfArgs.DstPath);
                srcDoc.Dispose();
            }
        }
        #endregion




C# combine multiple csv files, and convert to PDF


The following C# source code explains how to combine multiple CSV files and convert to PDF document in C# code.



        #region combine and convert csv to single pdf
        internal static void combineAndConvertCSVToPdf()
        {
            String[] files = new String[] { @"C:\demo1.csv", @"C:\demo2.csv", @"C:\demo3.csv" };
            String outputFilePath = @"C:\output.pdf";
            List<MemoryStream> streams = new List<MemoryStream>();
            foreach (String file in files)
            {
                MemoryStream outputStream = new MemoryStream();
                CSVDocument doc = new CSVDocument(file);
                doc.ConvertToDocument(DocumentType.PDF, outputStream);
                streams.Add(outputStream);
            }
            PDFDocument.CombineDocument(streams.ToArray(), outputFilePath);
        }
        #endregion




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


The following C# source code explains how to add csv file to PDF pages directly using C#



        #region insert csv to pdf
        internal static void insertCSVToPdf()
        {
            String filePath = @"C:\demo.csv";
            CSVDocument doc = new CSVDocument(filePath);
            MemoryStream stream = new MemoryStream();
            doc.ConvertToDocument(DocumentType.PDF, stream);
            PDFDocument pdf = new PDFDocument(stream);
            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