void RenderShownPage() { CancelRenderingShownPage(); var pageInfo = document.GetPageInfo(indexOfShownPage); if (pageInfo == null) { PdfImageView.Image = null; return; } var pageRect = pageInfo.Rect; var pageImageViewSize = PdfImageView.Bounds.Size; var pageImageWidth = pageImageViewSize.Width; var pageImageHeight = (nfloat)Math.Ceiling(pageImageWidth * (pageRect.Height / pageRect.Width)); // Rendering a page requires you to first set up a rendering request and // specify what and how exactly we want to render. var renderRequest = new PSPDFMutableRenderRequest(document) { PageIndex = indexOfShownPage, ImageSize = new CGSize(pageImageWidth, pageImageHeight), ImageScale = PdfImageView?.Window?.BackingScaleFactor ?? 1, CachePolicy = PSPDFRenderRequestCachePolicy.ReloadIgnoringCacheData }; // Having the rendering request, we can then create a rendering task. // Think of the rendering request being a configuration object for the // render task (once the render task is created, the render request is // no longer needed). var renderTask = new PSPDFRenderTask(renderRequest); if (renderTask == null) { Console.WriteLine($"[EXAMPLE] Couldn't create a render task from render request: {renderRequest}."); PdfImageView.Image = null; return; } // We also assign a high priority for our render task because most of // the time it is initiated by the user and we therefore need the result // as fast as possible. renderTask.Priority = PSPDFRenderQueuePriority.UserInteractive; // As soon as the task finishes, it notifies its delegate, see // implemetation of the delegate protocol below. renderTask.Delegate = this; // Becuase we display only one page at a time, we can cancel a // previously issue rendering request if the rendering hasn't finished // in time. For that, we need to keep a reference to the render task. shownPageRenderTask = renderTask; // Finally, we can schedule a render task on the default render queue. PSPDFKitGlobal.SharedInstance.RenderManager.RenderQueue.Schedule(renderTask); Console.WriteLine("[EXAMPLE] Scheduled rendering shown page."); }
void CancelRenderingShownPage() { if (shownPageRenderTask == null) { return; } shownPageRenderTask.Cancel(); shownPageRenderTask = null; Console.WriteLine("[EXAMPLE] Cancelled rendering shown page."); }
public void RenderTaskDidFinish(PSPDFRenderTask task) { if (task.IsEqual(shownPageRenderTask)) { // A rendering request might finish but not have an image. This // happens for example for password protected but not yet unlocked // documents. In that case, we clear the old image and don't show // anything. var image = task.Image; if (image == null) { Console.WriteLine("[EXAMPLE] Finished rendering shown page, but image was 'null'."); PdfImageView.Image = null; return; } Console.WriteLine("[EXAMPLE] Finished rendering shown page, image is valid."); // The page is rendered properly, so we can show the result. PdfImageView.Image = image; } }