This is a migrated thread and some comments may be shown as answers.

Getting PDF Pages as Images

4 Answers 323 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Clayton
Top achievements
Rank 1
Clayton asked on 05 Jun 2013, 11:30 PM
We are evaluating your tools to see if they will fit our needs.  One of our requirements is that we need to be able to get an image of each page of a PDF so that we can further process it in our application.  I noticed on another thread that there is a ThumbnailFactory that can be used to create an ImageSource from a RadFixedPage.  I tried to use the PdfDocumentSource like so:
ThumbnailFactory tf = new ThumbnailFactory();
PdfDocumentSource docSource = new PdfDocumentSource(new MemoryStream(myPdfByteArray), FormatProviderSettings.ReadAllAtOnce);
 
foreach (RadFixedPage pdfPage in docSource.Document.Pages)
      {
        ImageSource pageImg = tf.CreateThumbnail(pdfPage, new System.Windows.Size(myWidth, myHeight));
        // Do additional work here
      }

...but docSource.Document is always null.

How can I get an image of each page in the PDF file?

Thanks,
Clayton

4 Answers, 1 is accepted

Sort by
0
Wenjie
Top achievements
Rank 1
answered on 06 Jun 2013, 03:23 AM
test
0
Wenjie
Top achievements
Rank 1
answered on 06 Jun 2013, 03:24 AM

try this:

ThumbnailFactory tf = new ThumbnailFactory();
  
                 MemoryStream mstreamPdfShow = new MemoryStream();
                mstreamPdfShow.Write(myPdfByteArray, 0, myPdfByteArray.Length);
                mstreamPdfShow.Seek(0, System.IO.SeekOrigin.Begin);
 
PdfDocumentSource docSource = new PdfDocumentSource(mstreamPdfShow, FormatProviderSettings.ReadAllAtOnce);
 
 
  
foreach (RadFixedPage pdfPage in docSource.Document.Pages)
  
      {
  
        ImageSource pageImg = tf.CreateThumbnail(pdfPage, new System.Windows.Size(myWidth, myHeight));
  
        // Do additional work here
  
      }
0
Accepted
Kammen
Telerik team
answered on 06 Jun 2013, 02:18 PM
Hello,

Actually PdfDocumentSource is created in order to load PDF documents in RadPdfViewer from Stream or Uri. Some of the methods in it are asynchronous, so it is not expected that the Document property will be initialized synchronously.

When you just need a RadFixedDocument, you can use PdfFormatProvider instead. The exact code should look like this:
PdfFormatProvider formatProvider = new PdfFormatProvider(stream, FormatProviderSettings.ReadAllAtOnce);
RadFixedDocument document = formatProvider.Import();
 
foreach (RadFixedPage page in document..Pages)
{
    ImageSource pageImg = tf.CreateThumbnail(page , size);
    // Do additional work here
}

I hope this helps!

Regards,
Kammen
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Clayton
Top achievements
Rank 1
answered on 06 Jun 2013, 05:13 PM
Works great... thank you very much
Tags
PDFViewer
Asked by
Clayton
Top achievements
Rank 1
Answers by
Wenjie
Top achievements
Rank 1
Kammen
Telerik team
Clayton
Top achievements
Rank 1
Share this question
or