Convert PDF to JPG

7 posts, 0 answers
  1. Blas
    Blas avatar
    55 posts
    Member since:
    Dec 2007

    Posted 28 May 2015 Link to this post

    Hi,

    Is there any way to convert/rasterize an existing PDF file to JPG file(s) using PDFProcessing library?

    Regards

    Blas

  2. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 01 Jun 2015 Link to this post

    Hello Blas,

    RadPdfProcessing is independent of UI library and has no notion of the way the document should be visualized. This is why is not possible to export a PDF document to image using this library.

    In order to achieve your goal, you could use the PdfViewer control to import the document and create images from its pages using the ThumbnailFactory class. The following snippet demonstrates how to achieve this for the first page of the RadFixedDocument:

    PdfFormatProvider docProvider = new PdfFormatProvider();
    var data = docProvider.Export(doc);
     
    PdfFormatProvider provider = new PdfFormatProvider(new MemoryStream(data), FormatProviderSettings.ReadOnDemand);
    this.viewer.Document = provider.Import();
     
    ThumbnailFactory factory = new ThumbnailFactory();
    RadFixedPage page = this.viewer.Document.Pages[0];
    ImageSource source = factory.CreateThumbnail(page, page.Size);
     
    Image image = new Image();
    image.Source = source;
     
    // This adds white background, because the image that ThumbnailFactory creates is transparent.
    Grid grid = new Grid();
    grid.Background = Brushes.White;
    grid.Children.Add(image);
     
    Size size = new Size(600, 800);
    grid.Measure(size);
    grid.Arrange(new Rect(new Point(0, 0), size));
     
    RenderTargetBitmap bitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);
    bitmap.Render(grid);
      
    using (var fileStream = new FileStream("test.jpeg", FileMode.Create))
    {
        System.Windows.Media.Imaging.BitmapEncoder encoder = new JpegBitmapEncoder();
        ExportExtensions.ExportToImage(grid, fileStream, encoder);
    }

    The ThumbnailFactory returns a picture of the content of the document and it may not have background set. The additional steps in the snippet above are used to set a background for the image.

    Hope this helps.

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 01 Jun 2015 Link to this post

    I am sorry I misled you in my previous reply.

    You should be aware that the example from my previous post will work only for the RadPdfViewer from the Telerik UI for WPF suite. You could refer the corresponding dll-s in your project and convert the document.

    If this doesn't fit your needs, you could check this article, which explains how to create a printable panel.

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. Blas
    Blas avatar
    55 posts
    Member since:
    Dec 2007

    Posted 02 Jun 2015 in reply to Tanya Link to this post

    Thank you Tanya for your answer, although I couldn't test it, because the code provided uses the ThumbnailFactory WPF class and my project is Winform.

     Is it possible to implement this funcionality in Winform?

    Regards

    Blas

  5. Blas
    Blas avatar
    55 posts
    Member since:
    Dec 2007

    Posted 02 Jun 2015 in reply to Blas Link to this post

    Hi Tania,

     I didn't see your last response... In any case, I included in my project some WPF dlls but I'm having some problems.

     In the last line, I get an VS error saying "page: a value of the type Telerik.Windows.Pdf.Documents.Fixed.Model.RadFixedPage cannot be converted to the type Telerik.Windows.Documents.Fixed.Model.RadFixedPage.

    Viewer.LoadDocument("C:\temp\file.pdf")
    Dim factory As New Telerik.Windows.Documents.UI.ThumbnailFactory()
    Dim page As RadFixedPage = Viewer.Document.Pages.Item(0)
     
    Dim source As System.Windows.Media.ImageSource = factory.CreateThumbnail(page, New Windows.Size(page.Size.Width, page.Size.Height))

     

  6. Blas
    Blas avatar
    55 posts
    Member since:
    Dec 2007

    Posted 03 Jun 2015 in reply to Blas Link to this post

    Hi Tanya,

     I've opened a support ticket.

    Thank you

    Blas

  7. Ankit
    Ankit avatar
    1 posts
    Member since:
    Mar 2011

    Posted 20 Jul Link to this post

    I suggest you use a separate free application for conversion. A quick search on Google gave me a free converter: http://jpg-to-word-converter.en.softonic.com
Back to Top