Free Support Forum -

Converting csv build using StringBuilder to pdf

I have a StringBuilder object which is used to build a csv file. Now I want to convert this csv file to pdf without saving the csv to disk using WorkBookDesigner. I could do it reading a CSV file from disk like below

wb.Workbook = new Workbook(@"C:\New Text Document.csv");

        wb.Workbook.Save(@"C:\Result.pdf", SaveFormat.Pdf);</p><p>But I have csv file in stringbuilder and want to convert it to pdf.</p><p> </p>


Well, I think you may open/load the CSV file/data from streams (you have to build the streams by your own codes for your needs accordingly) and then convert to your desired PDF format.
Workbook book = new Workbook(stream, new LoadOptions(LoadFormat.CSV));
book.Save(“output.pdf”, SaveFormat.Pdf);

Also, if you could save your CSV data to an Array or datatable, you may import that array at once using Cells.ImportArray() method, see the document for your reference:

Moreover, I have pasted a simple example on how to create CSV and PDF files from the StringBuilder here.

Sample code:

Workbook wb = new Workbook();
Worksheet wks = wb.Worksheets[0];
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i <= random.Next(1, 50); i++)
sb.Append("Test one. ");
sb.Append("Test two. ");
sb.Append("Test three. ");
sb.Append("Test four. ");
sb.Append("Test five. ");

wks.Cells[0, 0].PutValue(sb.ToString());
wks.Cells[1, 0].PutValue(sb.ToString());
wb.Save(“out1.csv”, SaveFormat.CSV);
wb.Save(“output2.pdf”, SaveFormat.Pdf);

Hope, this helps you.

thank you.