how i can Convert excel work shet as array to pdf
Loading
how i can Convert excel work shet as array to pdf
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasad RaveendranRank #320 · 7.8K reputationPosted Aug 27, 2023, 12:23 PM EDT
You need to install the iTextSharp library from NuGet. Open the NuGet Package Manager Console and run:
Create a C# console application and add the following code to your program:
using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using OfficeOpenXml; namespace ExcelToPDFConverter { class Program { static void Main(string[] args) { string excelFilePath = "your_excel_file.xlsx"; // Replace with your Excel file path string sheetName = "Sheet1"; // Replace with your sheet name string pdfOutputPath = "output.pdf"; // Replace with desired output PDF path var arrayData = ReadExcelToArray(excelFilePath, sheetName); ConvertArrayToPDF(arrayData, pdfOutputPath); Console.WriteLine("Conversion complete!"); } static string[][] ReadExcelToArray(string excelFilePath, string sheetName) { var arrayData = new string[0][]; FileInfo fileInfo = new FileInfo(excelFilePath); using (var package = new ExcelPackage(fileInfo)) { var worksheet = package.Workbook.Worksheets[sheetName]; var start = worksheet.Dimension.Start; var end = worksheet.Dimension.End; int rows = end.Row - start.Row + 1; int cols = end.Column - start.Column + 1; arrayData = new string[rows][]; for (int row = start.Row; row <= end.Row; row++) { arrayData[row - start.Row] = new string[cols]; for (int col = start.Column; col <= end.Column; col++) { var cellValue = worksheet.Cells[row, col].Text; arrayData[row - start.Row][col - start.Column] = cellValue; } } } return arrayData; } static void ConvertArrayToPDF(string[][] arrayData, string pdfOutputPath) { using (FileStream fs = new FileStream(pdfOutputPath, FileMode.Create)) { Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.GetInstance(document, fs); document.Open(); PdfPTable table = new PdfPTable(arrayData[0].Length); foreach (var row in arrayData) { foreach (var cellValue in row) { PdfPCell cell = new PdfPCell(new Phrase(cellValue)); table.AddCell(cell); } } document.Add(table); document.Close(); } } } }Remember to replace
"your_excel_file.xlsx"with the path to your Excel file,"Sheet1"with the name of the sheet you want to convert, and"output.pdf"with the desired output PDF file name.This code reads the Excel data, creates a PDF document, and populates it with the data in a tabular format using iTextSharp.
Leon DRank #851 · 2.6K reputationPosted Aug 28, 2023, 10:29 PM EDT
You can use Free Spire.XLS to convert Excel to PDF.
https://www.e-iceblue.com/Download/download-excel-for-net-free.html
You can also install the library via NuGet: Install-Package FreeSpire.XLS
The example below shows how to convert an Excel worksheet as PDF:
using Spire.Xls; namespace ConvertWorksheetToPdf { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load a sample Excel file workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.xlsx"); //Set worksheets to fit to page when converting workbook.ConverterSetting.SheetFitToPage = true; //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Save to PDF worksheet.SaveToPdf("WorksheetToPdf.pdf"); } } }The above code snippet uses the Workbook.LoadFromFile() method to load an Excel file from disk, you are also able to load an Excel file from stream by using the Workbook.LoadFromStream() method.
For more information, check the following article:
https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Excel-Conversion/NET-Excel-New-method-of-Convert-Excel-to-PDF.html
Mohammad HussainRank #513 · 4.8K reputationPosted Aug 28, 2023, 4:58 AM EDT
using OfficeOpenXml; // Load the Excel package using (var package = new ExcelPackage(new FileInfo("your-excel-file.xlsx"))) { var worksheet = package.Workbook.Worksheets[0]; // Loop through rows and columns to populate your data structure List> excelData = new List>(); for (int row = 1; row rowData = new List(); for (int col = 1; col <= worksheet.Dimension.Columns; col++) { rowData.Add(worksheet.Cells[row, col].Text); } excelData.Add(rowData); } // Now you have your Excel data in excelData array }using iTextSharp.text; using iTextSharp.text.pdf; // Create a new PDF document using (var document = new Document()) { var pdfFilePath = "output.pdf"; var writer = PdfWriter.GetInstance(document, new FileStream(pdfFilePath, FileMode.Create)); document.Open(); foreach (var rowData in excelData) { foreach (var cellData in rowData) { // Add cell data to PDF document document.Add(new Paragraph(cellData)); } // Add a new line after each row document.Add(Chunk.NEWLINE); } }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 28, 2023, 11:34 PM EDT
using System; using System.IO; using ClosedXML.Excel; using PdfSharp.Pdf; using PdfSharp.Drawing; class Program { static void Main(string[] args) { string excelFilePath = "YourExcelFile.xlsx"; string pdfFilePath = "ConvertedPDF.pdf"; // Load the Excel file using (XLWorkbook workbook = new XLWorkbook(excelFilePath)) { // Create a PDF document PdfDocument pdfDocument = new PdfDocument(); PdfPage pdfPage = pdfDocument.AddPage(); // Convert Excel content to PDF XGraphics gfx = XGraphics.FromPdfPage(pdfPage); XImage image = XLWorkbookExtensions.ToImage(workbook.Worksheet(1), gfx); gfx.DrawImage(image, 0, 0); // Save PDF pdfDocument.Save(pdfFilePath); } } }Marvin ReidRank #1,963 · 746 reputationPosted Sep 6, 2023, 1:50 PM EDT
You can convert Excel to PDF using the DocumentConverter. Here is some code that you can try:
using (DocumentConverter documentConverter = new DocumentConverter()) { var input = @"C:\Input\input.xlsx"); var output = @"C:\Output\output.pdf"); var jobData = DocumentConverterJobs.CreateJobData(input, output, DocumentFormat.Pdf); jobData.JobName = "Convert to PDF"; var documentWriter = new DocumentWriter(); documentConverter.SetDocumentWriterInstance(documentWriter); var renderingEngine = new AnnWinFormsRenderingEngine(); documentConverter.SetAnnRenderingEngineInstance(renderingEngine); var job = documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(job); }https://www.leadtools.com/help/sdk/tutorials/dotnet-console-convert-files-with-the-document-converter.html