Convert XLS and XLSX to PDF with .NET Core (C#, VB, and Java)

We have recently fielded several questions about converting Office formats such as Excel to PDF using .NET Core. The LEADTOOLS Document Converter SDK class makes this a quick and easy task while still providing flexibility for any scenario.

Read more about LEADTOOLS Document Converter technology.

Once you install the LEADTOOLS SDK, take a look at the .NET Core Document Converter demo in the LEADTOOLS21\Examples\DotNetCore\CommandLine\DocumentConverterDemo folder. The Document Converter Demo converts documents as well as images to document formats such as PDF. The resulting PDF files can be text searchable or image based. It leverages LEAD’s AI-powered OCR to convert images and non-searchable documents to text-searchable document formats.

It is worth mentioning that the LEADTOOLS Document Converter does not require third-party assemblies to perform the conversion of XLS and XLSX. In other words, the converter does not require the Microsoft Office interop assemblies to convert any LEADTOOLS supported file format to any other LEADTOOLS supported format such as XLS and XLSX. This simplifies deployment as well as licensing.

To show how this works before downloading the SDK, a simplified version of the code to convert an XLSX file to PDF is below:

XLSX to PDF using C#


using (DocumentConverter documentConverter = new DocumentConverter())   
{   
    var inFile = Path.Combine(ImagesPath.Path, @"input.xlsx");   
    var outFile = Path.Combine(ImagesPath.Path, @"output.pdf");   
    var format = DocumentFormat.Pdf;   
    var jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format);   
    jobData.JobName = "XLSX conversion to PDF";   

    var documentWriter = new DocumentWriter();   
    documentConverter.SetDocumentWriterInstance(documentWriter);   

    // Handles any annotations or comments in the input spreadsheet
    var renderingEngine = new AnnWinFormsRenderingEngine();   
    documentConverter.SetAnnRenderingEngineInstance(renderingEngine);   

    var job = documentConverter.Jobs.CreateJob(jobData);   
    documentConverter.Jobs.RunJob(job);   

    if (job.Status == DocumentConverterJobStatus.Success)   
        Console.WriteLine("Success");   
    else   
    {   
        Console.WriteLine("{0} Errors", job.Status);   
        foreach (var error in job.Errors)   
           Console.WriteLine("  {0} at {1}: {2}", error.Operation,
               error.InputDocumentPageNumber, error.Error.Message);   
    }   
}

For more information on this example, check out our full C# tutorial on the LEADTOOLS Document Converter.

XLSX to PDF using VB


Using documentConverter As DocumentConverter = New DocumentConverter()
    Dim inFile = Path.Combine(ImagesPath.Path, "input.xlsx")
    Dim outFile = Path.Combine(ImagesPath.Path, "output.pdf")
    Dim format = DocumentFormat.Pdf
    Dim jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format)
    jobData.JobName = "XLSX conversion to PDF"
    Dim documentWriter = New DocumentWriter()
    documentConverter.SetDocumentWriterInstance(documentWriter)
    Dim renderingEngine = New AnnWinFormsRenderingEngine()
    documentConverter.SetAnnRenderingEngineInstance(renderingEngine)
    Dim job = documentConverter.Jobs.CreateJob(jobData)
    documentConverter.Jobs.RunJob(job)

    If job.Status = DocumentConverterJobStatus.Success Then
        Console.WriteLine("Success")
    Else
        Console.WriteLine("{0} Errors", job.Status)

        For Each [error] In job.Errors
            Console.WriteLine("  {0} at {1}: {2}", [error].Operation, [error].InputDocumentPageNumber, [error].[Error].Message)
        Next
    End If
End Using

More information about this example can be found in the Document Converter class documentation.

XLSX to PDF using Java


String inFile = Path.Combine(ImagesPath.Path, @"input.xlsx");   
String outFile = Path.Combine(ImagesPath.Path, @"output.pdf");   
DocumentFormat format = DocumentFormat.Pdf;   

DocumentConverter documentConverter = new DocumentConverter();
DocumentWriter docWriter = new DocumentWriter();
docConverter.setDocumentWriterInstance(docWriter);

DocumentConverterJobData jobData = 
   DocumentConverterJobs.CreateJobData(inFile, outFile, format);  
jobData.setJobName("Xlsx Conversion");
DocumentConverterJob job = docConverter.getJobs().createJob(jobData);
docConverter.getJobs().runJob(job);

if (job.getErrors().size() > 0)
   for (DocumentConverterJobError error : job.getErrors())
      System.out.println("%2fnError during conversion: " + error.getError().getMessage());
else
   System.out.println("Successfully converted file to " + outFile);

For more information on the Java Document Converter, check out our full tutorial on the LEADTOOLS Java Document Converter.

Try for Free

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days, and comes with free chat and email support.

But Wait! There’s More

Did you see our previous post, “Convert PDF to Text in C#, VB, and Java”? Stay tuned for more conversion examples to see how the LEADTOOLS document converter will easily fit into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at 704-332-5532.

About 

Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in Document Converter and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *