Getting PDF Pages as Images

5 posts, 1 answers
  1. Clayton
    Clayton avatar
    2 posts
    Member since:
    Nov 2011

    Posted 05 Jun 2013 Link to this post

    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
  2. Wenjie
    Wenjie avatar
    35 posts
    Member since:
    Mar 2013

    Posted 05 Jun 2013 Link to this post

    test
  3. UI for WPF is Visual Studio 2017 Ready
  4. Wenjie
    Wenjie avatar
    35 posts
    Member since:
    Mar 2013

    Posted 05 Jun 2013 Link to this post

    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
      
          }
  5. Answer
    Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 06 Jun 2013 Link to this post

    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.

  6. Clayton
    Clayton avatar
    2 posts
    Member since:
    Nov 2011

    Posted 06 Jun 2013 Link to this post

    Works great... thank you very much
Back to Top
UI for WPF is Visual Studio 2017 Ready