Render pdf using wpf

972 Views Asked by At

I need to write my own pdf viewer (UserControl). I use pdfium.dll for that. The wrapper of it is PdfiumService. This service can render pages to BitmapSource. PdfViewer displays pages in VirtualizingStackPanel in ScrollViewer. Any ideas how can I do lazy render for pdf? The problem is if pdf is about 20mb (1000 pages), rendered pages take about 2gb RAM.

Can VirtualizingStackPanel help me? I didn't find any events for "BeginVirtualizing" or something else. Any easy ways to know what item is displaying now?

Maybe something like that:

  1. Calculate how many pages can be displayed at once.
  2. See ScrollViewer's offset.
  3. Calculate the index of page is now displaying.
  4. Render 5 pages next to current.

Are there any ready solutions, or some tips, or ideas for this?

1

There are 1 best solutions below

3
On

well, i had a bit of that with images from books....the trouble is not really the gui where you put the bitmap, but how you get the images from the library...is it one by one, sequentially or randomly ?

In fact, if you use a VirtualizingStackPanel it will only manage the gui element to create or destroy, but if you have a full collection of bitmaps in memory you are dead.

One way is to create Page object whitout the bitmap, and create the image when needed + add a timer that will clear all "oldest images"

I do something like that in CBR; and i use a custom control to display pages

private BitmapImage _Image = null;
    /// <summary>
    /// the image 
    /// </summary>
    public BitmapImage Image
    {
        get
        {
            if (_Image == null)
                _Image = (DocumentFactory.Instance.GetService(Parent) as BookService).GetImageFromStream(Parent.FilePath, FilePath);

            ImageLastAcces = DateTime.Now;
            return _Image;
        }
        set { _Image = value; }
    }