Control External Resources with Aspose.Cells for .NET

Introduction

In today’s digital landscape, converting Excel spreadsheets to PDF documents is a common and essential task. Whether you’re preparing reports, financial data, or presentation materials, ensuring that your PDFs reflect your intended format is crucial. Aspose.Cells for .NET provides a powerful library that allows you to control this conversion process in detail, especially when dealing with external resources like images. In this guide, we’ll explore how to effectively manage external resources during the Excel to PDF conversion process using Aspose.Cells. Let’s dive in!

Prerequisites

Before we get started, ensure you have the following ready:

  1. Visual Studio or any .NET-compatible IDE: This will be your development environment.
  2. Aspose.Cells for .NET: If you haven’t installed it yet, visit the Aspose Downloads page to get the latest version.
  3. Basic Knowledge of C#: Familiarity with C# will be beneficial. If you need clarification on any concepts, feel free to look them up.
  4. Sample Excel File: Prepare an Excel file, such as “samplePdfSaveOptions_StreamProvider.xlsx,” that contains external resources you wish to convert.
  5. Image File for Testing: Use an image file like “newPdfSaveOptions_StreamProvider.png” as an external resource during the conversion.

Import Necessary Packages

To begin, you’ll need to import the required namespaces from the Aspose.Cells library. Add the following using directives at the top of your C# file:

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Aspose.Cells;
using Aspose.Cells.Drawing;
using Aspose.Cells.Rendering;
using System;

These namespaces provide the essential classes and methods for your tasks.

Step 1: Create a Stream Provider Class

First, create a stream provider class that implements the IStreamProvider interface. This class will enable you to control how external resources are loaded.

class MyStreamProvider : IStreamProvider
{
    public void CloseStream(StreamProviderOptions options)
    {
        Debug.WriteLine("-----Close Stream-----");
    }

    public void InitStream(StreamProviderOptions options)
    {
        string sourceDir = "Your Document Directory";
        Debug.WriteLine("-----Init Stream-----");
        
        // Load the image into a memory stream
        byte[] bts = File.ReadAllBytes(Path.Combine(sourceDir, "newPdfSaveOptions_StreamProvider.png"));
        MemoryStream ms = new MemoryStream(bts);
        options.Stream = ms;
    }
}
  • CloseStream: This method is called when the stream is closed, currently logging a debug message.
  • InitStream: This method reads the external image file as a byte array, converts it into a memory stream, and assigns it to the options.Stream property.

Step 2: Set Up Source and Output Directories

Next, define the directories for your Excel file and the output PDF.

// Source directory
string sourceDir = "Your Document Directory";
// Output directory
string outputDir = "Your Document Directory";

Replace "Your Document Directory" with the actual path on your system where your files are located.

Step 3: Load Your Excel File

Now, load the Excel file from which you want to create the PDF.

// Load the source Excel file containing external images
Workbook wb = new Workbook(sourceDir, "samplePdfSaveOptions_StreamProvider.xlsx");

The Workbook class from Aspose.Cells represents your Excel file, which may include various external resources like images.

Step 4: Set PDF Save Options

Before saving the workbook as a PDF, specify your desired save options.

// Specify PDF Save Options - Stream Provider
PdfSaveOptions opts = new PdfSaveOptions
{
    OnePagePerSheet = true // Save each sheet on a new page
};

This creates an instance of PdfSaveOptions, allowing you to customize the PDF format. The OnePagePerSheet option ensures that each Excel sheet appears on a separate page in the final PDF.

Step 5: Assign Your Stream Provider

Connect your Workbook instance with the MyStreamProvider class you created earlier.

wb.Settings.StreamProvider = new MyStreamProvider();

This line ensures that whenever external resources are encountered during conversion, your custom provider will manage them accordingly.

Step 6: Save the Workbook as PDF

Now, save your Excel workbook as a PDF.

// Save the workbook to PDF
wb.Save(outputDir + "outputPdfSaveOptions_StreamProvider.pdf", opts);

By calling the Save method on the workbook object and passing the output directory along with the PDF options, you convert the Excel file into a well-formatted PDF.

Step 7: Confirm Successful Execution

Finally, it’s good practice to confirm that the process completed successfully.

Console.WriteLine("ControlLoadingOfExternalResourcesInExcelToPDF executed successfully.\r\n");

This message will inform you about the status of your operation, providing useful feedback.

Conclusion

You’ve now mastered the process of controlling external resources during Excel to PDF conversions using Aspose.Cells! By following these steps, you can ensure that your documents accurately include images and other external elements, resulting in a polished final product every time.

FAQ’s

What is Aspose.Cells?

Aspose.Cells is a powerful library for .NET developers that enables the creation, manipulation, conversion, and rendering of Excel files in various formats.

How do I download Aspose.Cells?

You can download the latest version from the Download link.

Can I try Aspose.Cells for free?

Yes! You can access a free trial by visiting the Free trial page.

Where can I find support for Aspose.Cells?

For support-related queries, visit the Aspose Support forum.

How can I obtain a temporary license for Aspose.Cells?

You can apply for a temporary license here.