Hi,
I’m using Aspose Cells to convert XLS, XLSX file to PDF file and looking for an API to do the following job:
- When the XLS file contains an external image which linked to an Image in Internet, how can I convert the XLS file to PDF with the external image inside it? Currently Aspose Cells only convert to PDF without loading the external image. Here is the sample code I used and the file
using(var workbook = new Aspose.Cells.Workbook(inFile))
{
workbook.AcceptAllRevisions();
workbook.CalculateFormula();
workbook.Save(outFile, SaveFormat.Pdf);
}
sample_file.zip (25.2 KB)
I’m using Aspose Cells 24.4 .NET.
Thank you for your help.
@dunghnguyen
To convert an XLS or XLSX file to a PDF while including external images linked from the internet, you can implement a custom stream provider in Aspose.Cells. This allows you to control how external resources are loaded during the conversion process.
Here’s a step-by-step guide on how to achieve this:
-
Create a Stream Provider Class: Implement a class that adheres to the IStreamProvider
interface. This class will manage how external resources, such as images, are handled.
-
Set Up Source and Output Directories: Define the paths for your input Excel file and the output PDF file.
-
Load Your Excel File: Use the Workbook
class to load the Excel file that contains the external images.
-
Configure PDF Save Options: Create an instance of PdfSaveOptions
to customize the PDF output. You can set options like OnePagePerSheet
to ensure each sheet is saved on a separate page.
-
Assign Your Stream Provider: Link your Workbook
instance to your custom stream provider. This ensures that any external resources encountered during the conversion will be processed by your provider.
-
Save the Workbook as PDF: Finally, call the Save
method on the workbook object, passing in the output directory and the PDF options.
Here’s a sample code snippet demonstrating this process in C#:
using System.IO;
using Aspose.Cells;
using Aspose.Cells.Rendering;
class MyStreamProvider : IStreamProvider
{
public void CloseStream(StreamProviderOptions options)
{
// Handle stream closing if necessary
}
public void InitStream(StreamProviderOptions options)
{
// Load the external image into a memory stream
byte[] imageBytes = File.ReadAllBytes("path_to_your_image.png");
MemoryStream memoryStream = new MemoryStream(imageBytes);
options.Stream = memoryStream;
}
}
public class ConvertExcelToPdf
{
public static void Run()
{
// Load the Excel file
Workbook workbook = new Workbook("path_to_your_excel_file.xlsx");
// Set PDF save options
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.OnePagePerSheet = true;
// Assign the custom stream provider
workbook.Settings.StreamProvider = new MyStreamProvider();
// Save the workbook to PDF
workbook.Save("output_file.pdf", pdfOptions);
}
}
Make sure to replace "path_to_your_image.png"
and "path_to_your_excel_file.xlsx"
with the actual paths to your image and Excel file, respectively.
By following these steps, you should be able to include external images in your PDF output when converting from Excel using Aspose.Cells. For more detailed information, you can refer to the official documentation on controlling external resources during the conversion process.
Sources:
[1]: Control External Resources in Excel to PDF in Aspose.Cells
The code you show only help in case of the image are in the local machine where I can Load the data into memory. The image I want to included is from an Internet image so I can’t load the data like above
@dunghnguyen
We can obtain the correct results by testing on the latest version v25.5 using the following sample code. Please refer to the attachment. 50528322_out.pdf (150.7 KB)
using (var workbook = new Aspose.Cells.Workbook(filePath + "50528322.xls"))
{
workbook.AcceptAllRevisions();
workbook.CalculateFormula();
PdfSaveOptions options = new PdfSaveOptions();
options.OnePagePerSheet = true;
workbook.Save(filePath + "50528322_out.pdf", options);
}
In addition, when testing with the file “50528322. xlsx”, there were no images in the PDF. When opening the sample file through Office 365, we found that the image cannot be displayed in Excel either. The PDF export result is consistent with Excel. Please refer to the attachment. result.png (42.7 KB)
We recommend you to kindly try using our latest version: Aspose.Cells for .NET v25.5.
@dunghnguyen
Please set WorkbookSettings.ResourceProvider with your stream provider to prevent/allow loading external resource as the following :
Workbook workbook = new Workbook(dir + "50528322.xls");
workbook.Settings.ResourceProvider = new StreamProvider();
workbook.Save(dir + "dest.pdf");
internal class StreamProvider : Aspose.Cells.IStreamProvider
{
public static void CopyStream(Stream srcStream, Stream dstStream)
{
if (srcStream == null)
throw new ArgumentNullException("srcStream");
if (dstStream == null)
throw new ArgumentNullException("dstStream");
byte[] buf = new byte[4096];
while (true)
{
int bytesRead = srcStream.Read(buf, 0, buf.Length);
// Read returns 0 when reached end of stream. Checking for negative too to make it conceptually close to Java.
if (bytesRead <= 0)
break;
else
dstStream.Write(buf, 0, bytesRead);
}
}
public void InitStream(StreamProviderOptions options)
{
if(options.DefaultPath.StartsWith("https:"))
{
//end
WebRequest request = WebRequest.Create(options.DefaultPath);
WebResponse response = request.GetResponse();
// FIX: Do not specify capacity since the length of the response is sometimes not known.
MemoryStream dstStream = new MemoryStream();
using (Stream responseStream = response.GetResponseStream())
{
CopyStream(responseStream, dstStream);
}
dstStream.Position = 0;
options.Stream = dstStream;
}
else
{
options.Stream = File.Create(options.DefaultPath);
}
}
public void CloseStream(StreamProviderOptions options)
{
options.Stream.Close();
}
}
See : Control loading of External Resources in MS Excel Workbook while rendering to PDF|Documentation