public override async Task GeneratePreviewAsync(Stream docStream, IPreviewGenerationContext context, CancellationToken cancellationToken) { var document = new Document(docStream); var pc = document.PageCount; // save the document only if this is the first round if (context.StartIndex == 0 || pc < 1) { await context.SetPageCountAsync(pc, cancellationToken).ConfigureAwait(false); } if (pc <= 0) { return; } var loggedPageError = false; context.SetIndexes(pc, out var firstIndex, out var lastIndex); for (var i = firstIndex; i <= lastIndex; i++) { cancellationToken.ThrowIfCancellationRequested(); try { using (var imgStream = new MemoryStream()) { var options = new ImageSaveOptions(SaveFormat.Png) { PageIndex = i, Resolution = context.PreviewResolution }; document.Save(imgStream, options); if (imgStream.Length == 0) { continue; } await context.SavePreviewAndThumbnailAsync(imgStream, i + 1, cancellationToken) .ConfigureAwait(false); } } catch (Exception ex) { if (await Tools.HandlePageErrorAsync(ex, i + 1, context, !loggedPageError, cancellationToken).ConfigureAwait(false)) { return; } loggedPageError = true; } } }
public override async Task GeneratePreviewAsync(Stream docStream, IPreviewGenerationContext context, CancellationToken cancellationToken) { docStream.Seek(0, SeekOrigin.Begin); var pres = new Presentation(docStream); if (context.StartIndex == 0) { await context.SetPageCountAsync(pres.Slides.Count, cancellationToken).ConfigureAwait(false); } var loggedPageError = false; context.SetIndexes(pres.Slides.Count, out var firstIndex, out var lastIndex); // calculate size based on the original aspect ratio and the expected image size. var sizeF = pres.SlideSize.Size; var ratio = Math.Min((float)Common.PREVIEW_WIDTH / sizeF.Width, (float)Common.PREVIEW_HEIGHT / sizeF.Height); var size = new Size((int)Math.Round(sizeF.Width * ratio), (int)Math.Round(sizeF.Height * ratio)); for (var i = firstIndex; i <= lastIndex; i++) { cancellationToken.ThrowIfCancellationRequested(); try { var slide = pres.Slides[i]; // generate image using (var image = slide.GetThumbnail(size)) { await context.SaveImageAsync(image, i + 1, cancellationToken).ConfigureAwait(false); } } catch (Exception ex) { if (await Tools.HandlePageErrorAsync(ex, i + 1, context, !loggedPageError, cancellationToken).ConfigureAwait(false)) { return; } loggedPageError = true; } } }
public override async Task GeneratePreviewAsync(Stream docStream, IPreviewGenerationContext context, CancellationToken cancellationToken) { var document = new Document(docStream); if (context.StartIndex == 0) { await context.SetPageCountAsync(document.Pages.Count, cancellationToken).ConfigureAwait(false); } var loggedPageError = false; context.SetIndexes(document.Pages.Count, out var firstIndex, out var lastIndex); for (var i = firstIndex; i <= lastIndex; i++) { cancellationToken.ThrowIfCancellationRequested(); using (var imgStream = new MemoryStream()) { try { var pngDevice = new PngDevice(new Resolution(context.PreviewResolution, context.PreviewResolution)); pngDevice.Process(document.Pages[i + 1], imgStream); if (imgStream.Length == 0) { continue; } await context.SavePreviewAndThumbnailAsync(imgStream, i + 1, cancellationToken) .ConfigureAwait(false); } catch (Exception ex) { if (await Tools.HandlePageErrorAsync(ex, i + 1, context, !loggedPageError, cancellationToken).ConfigureAwait(false)) { return; } loggedPageError = true; } } } }
public override async Task GeneratePreviewAsync(Stream docStream, IPreviewGenerationContext context, CancellationToken cancellationToken) { docStream.Seek(0, SeekOrigin.Begin); var document = (TiffImage)Image.Load(docStream); if (context.StartIndex == 0) { await context.SetPageCountAsync(document.Frames.Length, cancellationToken).ConfigureAwait(false); } var loggedPageError = false; context.SetIndexes(document.Frames.Length, out var firstIndex, out var lastIndex); for (var i = firstIndex; i <= lastIndex; i++) { cancellationToken.ThrowIfCancellationRequested(); try { document.ActiveFrame = document.Frames[i]; using (var imgStream = new MemoryStream()) { var options = new PngOptions(); document.Save(imgStream, options); await context.SavePreviewAndThumbnailAsync(imgStream, i + 1, cancellationToken) .ConfigureAwait(false); } } catch (Exception ex) { if (await Tools.HandlePageErrorAsync(ex, i + 1, context, !loggedPageError, cancellationToken).ConfigureAwait(false)) { return; } loggedPageError = true; } } }
public override async Task GeneratePreviewAsync(Stream docStream, IPreviewGenerationContext context, CancellationToken cancellationToken) { var document = new Workbook(docStream); var printOptions = new ImageOrPrintOptions { ImageType = GetImageType(), OnePagePerSheet = false, HorizontalResolution = context.PreviewResolution, VerticalResolution = context.PreviewResolution }; // every worksheet may contain multiple pages (as set by Excel // automatically, or by the user using the print layout) var estimatedPageCount = document.Worksheets.Select(w => new SheetRender(w, printOptions).PageCount).Sum(); if (context.StartIndex == 0) { await context.SetPageCountAsync(estimatedPageCount, cancellationToken).ConfigureAwait(false); } context.SetIndexes(estimatedPageCount, out var firstIndex, out var lastIndex); var workbookPageIndex = 0; var worksheetIndex = 0; var loggedPageError = false; // iterate through worksheets while (worksheetIndex < document.Worksheets.Count) { cancellationToken.ThrowIfCancellationRequested(); try { var worksheet = document.Worksheets[worksheetIndex]; var sheetRender = new SheetRender(worksheet, printOptions); // if we need to start preview generation on a subsequent worksheet, skip the previous ones if (workbookPageIndex + sheetRender.PageCount < context.StartIndex) { workbookPageIndex += sheetRender.PageCount; worksheetIndex++; continue; } // iterate through pages inside a worksheet for (var worksheetPageIndex = 0; worksheetPageIndex < sheetRender.PageCount; worksheetPageIndex++) { // if the desired page interval contains this page, generate the image if (workbookPageIndex >= firstIndex && workbookPageIndex <= lastIndex) { using (var imgStream = new MemoryStream()) { sheetRender.ToImage(worksheetPageIndex, imgStream); // handle empty sheets if (imgStream.Length == 0) { await context.SaveEmptyPreviewAsync(workbookPageIndex + 1, cancellationToken) .ConfigureAwait(false); } else { await context.SavePreviewAndThumbnailAsync(imgStream, workbookPageIndex + 1, cancellationToken).ConfigureAwait(false); } } } workbookPageIndex++; } } catch (Exception ex) { if (await Tools.HandlePageErrorAsync(ex, workbookPageIndex + 1, context, !loggedPageError, cancellationToken).ConfigureAwait(false)) { return; } loggedPageError = true; workbookPageIndex++; } worksheetIndex++; } // set the real count if some of the sheets turned out to be empty if (workbookPageIndex < estimatedPageCount) { await context.SetPageCountAsync(workbookPageIndex, cancellationToken).ConfigureAwait(false); } }