-1

I want to covert first page of pdf to image to display on gridview. After i select the image, the pdf open based on the image that i selected. How to convert first page of pdf to image on windows phone 8.1?

1 Answers1

0

I think a library called MuPDF is a way to go. I found an old sample project for WinRT (Reading PDF file in Windows store App) that contains a simple PDF viewer and it basically converts each page to a WriteableBitmap which is then displayed on the screen.

Here is a part of the code that does this:

async Task<WriteableBitmap> ConstructPageAsync()
    {
        var size = Document.GetPageSize(PageNumber);
        var width = (int)(size.X * ZoomFactor);
        var height = (int)(size.Y * ZoomFactor);

        var image = new WriteableBitmap(IsDoublePage ? width * 2 : width, height);
        IBuffer buf = new Buffer(image.PixelBuffer.Capacity);
        buf.Length = image.PixelBuffer.Length;

        if (IsDoublePage)
        {
            await Task.Run(() => Document.DrawFirtPageConcurrent(PageNumber, buf, width, height));
            await Task.Run(() => Document.DrawSecondPageConcurrent(PageNumber + 1, buf, width, height));
        }
        else
        {
            Document.DrawPage(PageNumber, buf, 0, 0, width, height, false);
        }

        // copy the buffer to the WriteableBitmap ( UI Thread )
        using (var stream = buf.AsStream())
        {
            await stream.CopyToAsync(image.PixelBuffer.AsStream());
        }

        return image;
    }

It needs some work to make it work in Windows Phone 8.1 but I think it's a good start.

Lukáš Neoproud
  • 872
  • 3
  • 10
  • 20
  • I have been use mupdf to to convert first page pf pdf to image, but once i submit the application to windows phone store, the application was rejected because MuPDFWinRT.dll has failed ContainerCheck check – user3431268 May 14 '15 at 04:11
  • I coudn't find any other free library. XFINIUM.PDF seems good, their sample app worked well. Unfortunately it costs about $500. – Lukáš Neoproud May 17 '15 at 14:01