Our product is using GroupDocs.Viewer for .NET (.NET Core 3.1) to render PDF files as JPG images, one image per page. I’ve implemented an IPageStreamFactory that reuses a single Stream and am calling viewer.View(), passing each page number until the end of the document.
My concern now is that I’m doing tons of extra work when calling View() - can anyone confirm this? If so, is there a better way to get each page of the rendered document as a new/separate stream without having all of them in memory at once?
Here’s my IPageStreamFactory:
internal class MemoryPageStreamFactory : IPageStreamFactory {
public MemoryPageStreamFactory(MemoryStream stream) {
Stream = stream;
}
public MemoryStream Stream { get; }
public Stream CreatePageStream(int pageNumber) {
Stream.Position = 0;
Stream.SetLength(0);
return Stream;
}
public void ReleasePageStream(int pageNumber, Stream pageStream) {
}
}
Here’s what my rendering process looks like. The method returns an IEnumerable so that I can iterate through the pages in the calling code, which as a result of the “yield return” will render the pages as I iterate:
LoadOptions loadOptions = new LoadOptions(fileType);
using (Viewer viewer = new Viewer(fileStream, loadOptions)) {
using (MemoryStream pageStream = new MemoryStream()) {
var pageStreamFactory = new MemoryPageStreamFactory(pageStream);
JpgViewOptions viewOptions = new JpgViewOptions(pageStreamFactory);
var viewInfoOptions = ViewInfoOptions.FromJpgViewOptions(viewOptions);
var viewInfo = viewer.GetViewInfo(viewInfoOptions);
if (maxWidth != null) {
viewOptions.MaxWidth = maxWidth.Value;
}
if (maxHeight != null) {
viewOptions.MaxHeight = maxHeight.Value;
}
for (int page = 1; page <= viewInfo.Pages.Count; page++) {
viewer.View(viewOptions, page);
yield return pageStreamFactory.Stream;
}
}
}