MS Excel Çalışma Kitabının PDF ye dönüştürülürken Harici Kaynakların Yüklenmesini Kontrol Etme
Olası Kullanım Senaryoları
Excel dosyanız bağlantılı resimler veya nesneler içerebilir. Excel dosyanızı PDF’ye dönüştürdüğünüzde, Aspose.Cells bu harici kaynakları alır ve bunları PDF’ye döker. Ancak bazen bu harici kaynakları yüklemek istemezsiniz ve daha da fazlası, onları manipüle etmek istersiniz. Bu, WorkbookSettings.StreamProvider kullanarak yapılabilir. IStreamProvider arayüzünü uygular.
MS Excel Çalışma Kitabında Harici Kaynakların Yüklenmesine Kontrol Etmek
Aşağıdaki örnek kod, harici kaynakların yüklenmesini kontrol etmek ve bunları manipüle etmek için WorkbookSettings.StreamProvider kullanımını açıklar. Lütfen kod içinde kullanılan örnek Excel dosyasına ve kod tarafından oluşturulan çıktı PDF’sine bakın. Ekran görüntüsü, örnekteki eski harici görüntünün çıktı PDF’de bir yeni görüntü ile nasıl değiştirildiğini göstermektedir.
Örnek Kod
// 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"); | |
} |