PDFにレンダリングする際のMS Excelワークブックの外部リソースの読み込みを制御する

可能な使用シナリオ

Excel ファイルには、リンクされた画像やオブジェクトなどの外部リソースが含まれている場合があります。Excel ファイルを PDF に変換すると、Aspose.Cells はこれらの外部リソースを取得してそれらを PDF にレンダリングします。しかし、時々これらの外部リソースをロードしたくないだけでなく、操作したい場合があります。これは、WorkbookSettings.StreamProvider で実装された IStreamProvider インターフェースを使用することで可能です。

PDFに変換する際のMS Excelブックの外部リソースの読み込みを制御する

次のサンプルコードでは、WorkbookSettings.StreamProvider を使用して外部リソースの読み込みを制御し、それらを操作する方法について説明しています。コード内で使用される サンプル Excel ファイル とコードによって生成された 出力 PDFスクリーンショットをご確認ください。サンプル Excel ファイル古い外部イメージ新しいイメージ に置換された様子が示されています。

todo:image_alt_text

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Implement IStreamProvider
class MyStreamProvider : IStreamProvider
{
public void CloseStream(StreamProviderOptions options)
{
System.Diagnostics.Debug.WriteLine("-----Close Stream-----");
}
public void InitStream(StreamProviderOptions options)
{
string sourceDir = RunExamples.Get_SourceDirectory();
System.Diagnostics.Debug.WriteLine("-----Init Stream-----");
//Read the new image in a memory stream and assign it to Stream property
byte[] bts = File.ReadAllBytes(sourceDir + "newPdfSaveOptions_StreamProvider.png");
MemoryStream ms = new MemoryStream(bts);
options.Stream = ms;
}
}
public static void Run()
{
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
//Load source Excel file containing external image
Workbook wb = new Workbook(sourceDir + "samplePdfSaveOptions_StreamProvider.xlsx");
//Specify Pdf Save Options - Stream Provider
PdfSaveOptions opts = new PdfSaveOptions();
opts.OnePagePerSheet = true;
wb.Settings.ResourceProvider = new MyStreamProvider();
//Save the workbook to Pdf
wb.Save(outputDir + "outputPdfSaveOptions_StreamProvider.pdf", opts);
Console.WriteLine("ControlLoadingOfExternalResourcesInExcelToPDF executed successfully.\r\n");
}