How to convert, export Microsoft Excel document to Adobe PDF file using C# in ASP.NET, Sharepoint, WinForms, WPF, Azure
Online C#.NET Tutorial for Converting MS Office .xls, .xlsx file to Adobe PDF files Using .NET XDoc.PDF Library. Free online Download.
Look for HTML5 PDF Editor?
EdgePDF:
ASP.NET PDF Editor is the best HTML5 PDF Editor and
ASP.NET PDF Viewer based on XDoc.PDF, JQuery, HTML5.
It supports
ASP.NET MVC and WebForms projects.
Best Microsoft Office Excel to adobe PDF file converter SDK for Visual Studio .NET
An excellent .NET control support convert PDF to multiple Excel formats in C#.NET, includes xls, xlsx, etc
Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
Turn all Excel spreadsheet into high quality PDF without losing formatting
Evaluation library and components for PDF creation from Excel in C#.NET framework
Create fillable and editable PDF documents from Excel in both .NET WinForms and ASP.NET
Automatically transform, print Excel sheet embeded tables, hyperlinks (url link) to PDF document
Convert to PDF with embedded fonts or without original fonts fast
Professional .NET PDF converter control for batch conversion
Merge all Excel sheets to one PDF file
Export PDF from Excel with cell border or no border
Free online Excel to PDF converter without email
Quick integrate online C# source code into .NET class
C#: Convert Microsoft Excel to Adobe PDF document in Visual C# .NET Project
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.
This is a C# class example for Excel (.xlsx/.xlsm/.xltx) to PDF conversion.
#region excel(2007 or higher) to pdf (file to file)
internal static void xlsxToPdf()
{
String inputPath = @"C:\demo.xlsx";
String outputPath = @"C:\output.pdf";
//convert .docx to .pdf
XLSXDocument doc = new XLSXDocument(inputPath);
doc.ConvertToDocument(DocumentType.PDF, outputPath);
}
#endregion
#region excel(2003) to pdf (file to file)
internal static void xlsToPdf()
{
String inputPath = @"C:\demo.xls";
String outputPath = @"C:\output.pdf";
//convert .xls to .pdf
XLSDocument doc = new XLSDocument(inputPath);
doc.ConvertToDocument(DocumentType.PDF, outputPath);
}
#endregion
#region excel(2007 or higher) to pdf (stream to stream)
internal static void xlsxStreamToPdf()
{
String inputPath = @"";
byte[] arr = File.ReadAllBytes(inputPath);
Stream inputStream = new MemoryStream(arr);
XLSXDocument doc = new XLSXDocument(inputStream);
Stream outputStream = new MemoryStream();
doc.ConvertToDocument(DocumentType.PDF, outputStream);
}
#endregion
#region excel(2003) to pdf (stream to stream)
internal static void xlsStreamToPdf()
{
String inputPath = @"";
byte[] arr = File.ReadAllBytes(inputPath);
Stream inputStream = new MemoryStream(arr);
XLSDocument doc = new XLSDocument(inputStream);
Stream outputStream = new MemoryStream();
doc.ConvertToDocument(DocumentType.PDF, outputStream);
}
#endregion
C# convert two or multiple Excel documents to PDF files (batch conversion)
#region excel(2007 or higher) to pdf (batch files and single tread)
internal static void xlsxFilesToPdf()
{
String inputDirectory = @"C:\input\";
String outputDirectory = @"C:\output\";
String[] files = Directory.GetFiles(inputDirectory, "*.xlsx");
foreach (String filePath in files)
{
int startIdx = filePath.LastIndexOf("\\");
int endIdx = filePath.LastIndexOf(".");
String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
XLSXDocument doc = new XLSXDocument(filePath);
doc.ConvertToDocument(DocumentType.PDF, outputDirectory + docName + ".pdf");
}
}
#endregion
#region excel(2003) to pdf (batch files and single tread)
internal static void xlsFilesToPdf()
{
String inputDirectory = @"C:\input\";
String outputDirectory = @"C:\output\";
String[] files = Directory.GetFiles(inputDirectory, "*.xls");
foreach (String filePath in files)
{
int startIdx = filePath.LastIndexOf("\\");
int endIdx = filePath.LastIndexOf(".");
String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
XLSDocument doc = new XLSDocument(filePath);
doc.ConvertToDocument(DocumentType.PDF, outputDirectory + docName + ".pdf");
}
}
#endregion
#region excel to pdf (batch file and multiple threads)
internal static void xlsxfiles2pdf()
{
String inputDirectory = @"C:\input\";
String outputDirectory = @"C:\output\";
String[] files = Directory.GetFiles(inputDirectory, "*.xlsx");
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(xlsxToPdfThread);
thread.Start(arg);
}
foreach (Thread thread in threads)
{
thread.Join();
}
}
private static void xlsxToPdfThread(object args)
{
ConversionArgs toPdfArgs = (ConversionArgs)args;
XLSXDocument srcDoc = new XLSXDocument(toPdfArgs.SrcPath);
if (srcDoc != null)
{
srcDoc.ConvertToDocument(DocumentType.PDF, toPdfArgs.DstPath);
srcDoc.Dispose();
}
}
#endregion
C# combine multiple excel files, and convert to PDF
#region combine and convert excel(2007 or higher) to single pdf
internal static void combineAndConvertExcelToPdf()
{
String[] files = new String[] { @"C:\demo1.xlsx", @"C:\demo2.xlsx", @"C:\demo3.xlsx" };
String outputFilePath = @"C:\output.pdf";
List<MemoryStream> streams = new List<MemoryStream>();
foreach (String file in files)
{
MemoryStream outputStream = new MemoryStream();
XLSXDocument doc = new XLSXDocument(file);
doc.ConvertToDocument(DocumentType.PDF, outputStream);
streams.Add(outputStream);
}
PDFDocument.CombineDocument(streams.ToArray(), outputFilePath);
}
#endregion
#region combine and convert excel(2003) to single pdf
internal static void combineAndConvertExcel03ToPdf()
{
String[] files = new String[] { @"C:\demo1.xls", @"C:\demo2.xls", @"C:\demo3.xls" };
String outputFilePath = @"C:\output.pdf";
List<MemoryStream> streams = new List<MemoryStream>();
foreach (String file in files)
{
MemoryStream outputStream = new MemoryStream();
XLSDocument doc = new XLSDocument(file);
doc.ConvertToDocument(DocumentType.PDF, outputStream);
streams.Add(outputStream);
}
PDFDocument.CombineDocument(streams.ToArray(), outputFilePath);
}
#endregion
C# insert excel sheets into pdf document, and create a new PDF file
#region insert excel(2007 or higher) to pdf
internal static void insertExcelToPdf()
{
String filePath = @"C:\demo.xlsx";
XLSXDocument doc = new XLSXDocument(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
#region insert excel(2003) to pdf
internal static void insertExcel03ToPdf()
{
String filePath = @"C:\demo.xls";
XLSDocument doc = new XLSDocument(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
.NET Class Namespace Required
Add necessary references:
RasterEdge.Imaging.Basic.dll
RasterEdge.Imaging.Basic.Codec.dll
RasterEdge.Imaging.Drawing.dll
RasterEdge.Imaging.Font.dll
RasterEdge.Imaging.Processing.dll
RasterEdge.XImage.Raster.dll
RasterEdge.XImage.Raster.Core.dll
RasterEdge.XDoc.PDF.dll
RasterEdge.XDoc.Office.Inner.Common.dll
RasterEdge.XDoc.Office.Inner.Office03.dll
RasterEdge.XDoc.Excel.dll
Use corresponding namespaces;
using RasterEdge.Imaging.Basic;
using RasterEdge.XDoc.Excel;
Related APIs (XLSXDocument.cs or XLSDocument.cs):
public override void ConvertToDocument(DocumentType targetType, String filePath)
Description:
Convert to PDF/TIFF and save it on the disk.
public override void ConvertToDocument(DocumentType targetType, Stream stream)
Description:
Convert to PDF/TIFF and save it into stream.
public override void ConvertToDocument(DocumentType targetType, float zoomValue, String filePath)
Description:
Convert to PDF/TIFF with specified zoom value and save it on the disk.
public override void ConvertToDocument(DocumentType targetType, float zoomValue, Stream desStream)
Description:
Convert to PDF/TIFF with specified zoom value and save it into stream.
public override void ConvertToDocument(DocumentType targetType, int resolution, String filePath)
Description:
Convert to PDF/TIFF with specified resolution and save it on the disk.
public override void ConvertToDocument(DocumentType targetType, int resolution, Stream desStream)
Description:
Convert to PDF/TIFF with specified resolution and save it into stream.
public override void ConvertToDocument(DocumentType targetType, ImageCompress compression, String filePath)
Description:
Convert to PDF/TIFF with specified compression method and save it on the disk.
public override void ConvertToDocument(DocumentType targetType, ImageCompress compression, Stream desStream)
Description:
Convert to PDF/TIFF with specified resolution and save it into stream.
public override void ConvertToDocument(DocumentType targetType, string filePath, ImageOutputOption options)
Description:
Convert to PDF/TIFF with specified settings through options and save it on the disk.
public override void ConvertToDocument(DocumentType targetType, Stream desStream, ImageOutputOption options)
Description:
Convert to PDF/TIFF with specified resolution and save it into stream.