PDFにレンダリングする際のMS Excelワークブックの外部リソースの読み込みを制御する
可能な使用シナリオ
Excelファイルには、リンクされた画像やオブジェクトなどの外部リソースが含まれている場合があります。ExcelファイルをPDFに変換すると、Aspose.Cellsはこれらの外部リソースを取得し、PDFにレンダリングします。ただし、これらの外部リソースを読み込みたくないことがあるかもしれません。さらに、それらを操作したいとも思うかもしれません。これは PdfSaveOptions.StreamProvider を使用して実現できます。これは IStreamProvider インターフェースを実装しています。
PDFに変換する際のMS Excelブックの外部リソースの読み込みを制御する
以下のサンプルコードは、外部リソースの読み込みを制御し、それらを操作するためにPdfSaveOptions.StreamProviderをどのように使用するかを説明しています。コード内で使用されるsample Excel fileおよびコードによって生成されたoutput PDFを確認してください。screenshotでは、サンプルのExcelファイル内のold external imageが出力されたPDF内のnew imageに置き換えられた様子が示されています。
サンプルコード
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Implement IStreamProvider | |
class MyStreamProvider implements IStreamProvider { | |
public void closeStream(StreamProviderOptions options) throws Exception { | |
System.out.println("-----Close Stream-----"); | |
} | |
public void initStream(StreamProviderOptions options) throws Exception { | |
System.out.println("-----Init Stream-----"); | |
// Read the new image in a memory stream and assign it to Stream property | |
File imgFile = new File( srcDir + "newPdfSaveOptions_StreamProvider.png"); | |
byte[] bts = new byte[(int) imgFile.length()]; | |
FileInputStream fin = new FileInputStream(imgFile); | |
fin.read(bts); | |
fin.close(); | |
ByteArrayOutputStream baout = new ByteArrayOutputStream(); | |
baout.write(bts); | |
baout.close(); | |
options.setStream(baout); | |
} | |
}//MyStreamProvider | |
// ------------------------------------------------ | |
// ------------------------------------------------ | |
void Run() throws Exception { | |
// Load source Excel file containing external image | |
Workbook wb = new Workbook(srcDir + "samplePdfSaveOptions_StreamProvider.xlsx"); | |
// Specify Pdf Save Options - Stream Provider | |
PdfSaveOptions opts = new PdfSaveOptions(); | |
opts.setOnePagePerSheet(true); | |
opts.setStreamProvider(new MyStreamProvider()); | |
// Save the workbook to Pdf | |
wb.save(outDir + "outputPdfSaveOptions_StreamProvider.pdf", opts); | |
} |