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

Convert PDF to JPG

10 Answers 1607 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Blas
Top achievements
Rank 1
Blas asked on 28 May 2015, 11:20 AM

Hi,

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

Regards

Blas

10 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 01 Jun 2015, 01:09 PM
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
0
Tanya
Telerik team
answered on 01 Jun 2015, 03:02 PM
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 -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
0
Blas
Top achievements
Rank 1
answered on 02 Jun 2015, 08:13 AM

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

0
Blas
Top achievements
Rank 1
answered on 02 Jun 2015, 02:52 PM

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))

 

0
Blas
Top achievements
Rank 1
answered on 03 Jun 2015, 10:46 AM

Hi Tanya,

 I've opened a support ticket.

Thank you

Blas

0
Mikhail
Top achievements
Rank 1
answered on 23 Jul 2018, 07:49 PM

Where can I get (download) library Telerik.Windows.Documents.UI to use ThumbnailFactory?

I have:

Telerik.Windows.Documents.Core

Telerik.Windows.Documents.Fixed

Telerik.Windows.Documents.Flow

Telerik.Windows.Documents.Flow.FormatProviders.Pdf

Telerik.Windows.Documents.Spreadsheet

Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml

Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf

Telerik.Windows.Maths

Telerik.Windows.Zip

 

Thank you

0
Tanya
Telerik team
answered on 24 Jul 2018, 11:57 AM
Hello Mikhail,

The ThumbnailFactory class is part of the API of RadPdfViewer for WPF. You can download the suite through your telerik.com account and more information on how to use the PdfViewer control you can find in the related documentation topics: Getting StartedExporting Fixed Page to Image.

Hope this is helpful.

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
0
Mikhail
Top achievements
Rank 1
answered on 24 Jul 2018, 01:47 PM

Thank you Tanya,

From what version of Telerik I can get all dll libraries? I can't find Telerik.Windows.Controls.FixedDocumentViewers.dll .

Telerik.Windows.Controls.dll
Telerik.Windows.Controls.FixedDocumentViewers.dll
Telerik.Windows.Documents.Core.dll
Telerik.Windows.Documents.Fixed.dll
Telerik.Windows.Zip.dll

0
Tanya
Telerik team
answered on 25 Jul 2018, 03:36 PM
Hello Mikhail,

The Telerik.Windows.Controls.FixedDocumentViewers.dll file is available since the Q1 2012 release of UI for WPF and should be present in the Binaries folder if you have a newer version. In case you are facing any issues using the PdfViewer control, I would like to ask you to submit your questions in the forum section dedicated to the specific component: https://www.telerik.com/forums/wpf/pdfviewer. Thank you in advance.

Regards,
Tanya
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 16 Feb 2022, 11:28 PM

Here is a sample code working.

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Telerik.Windows.Controls;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;

using Telerik.Windows.Documents.UI;
using ExportExtensions = Telerik.Windows.Media.Imaging.ExportExtensions;

namespace Pdf2Image
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Convert();
        }
        
        private void Convert()
        { 

            var diag = new RadOpenFileDialog
            {
                Filter = "Pdf|*.pdf"
            };
            diag.ShowDialog();

            try
            {
                var stream = new FileStream(diag.FileName, FileMode.Open);

                var pdfFormatProvider = new PdfFormatProvider
                {
                    ImportSettings = Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Import.PdfImportSettings.ReadOnDemand
                };

                var doc = pdfFormatProvider.Import(stream);
                var docProvider = new PdfFormatProvider();
                var data = docProvider.Export(doc);

                var provider = new PdfFormatProvider();
                viewer.Document = provider.Import(new MemoryStream(data)); // TODO: viewer = Add a RadPdfViewer control

                var factory = new ThumbnailFactory();
                var nPage = 0;
                var cDir = $"{Path.GetDirectoryName(diag.FileName)}\\pages_{Path.GetFileName(diag.FileName)[..^4]}";
                try
                {
                    Directory.CreateDirectory(cDir);
                }
                catch { }

                foreach (var page in this.viewer.Document.Pages)
                {
                    nPage++;
                    var source = factory.CreateThumbnail(page, page.Size);

                    var image = new Image
                    {
                        Source = source
                    };

                    var grid = new Grid
                    {
                        Background = Brushes.Black
                    };
                    grid.Children.Add(image);

                    var size = new Size(945, 528);
                    grid.Measure(size);
                    grid.Arrange(new Rect(new Point(0, 0), size));

                    var bitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);
                    bitmap.Render(grid);

                    var cFile = $"{cDir}\\{(nPage < 100 ? "0" : "")}{(nPage < 10 ? "0" : "")}{nPage}.jpeg";
                    if (File.Exists(cFile)) File.Delete(cFile);
                    using var fileStream = new FileStream(cFile, FileMode.Create);
                    BitmapEncoder encoder = new JpegBitmapEncoder();
                    ExportExtensions.ExportToImage(grid, fileStream, encoder);
                }
            }
            catch
            {

            }
            this.Close();

            // #jefferson2020 Happy 2025
        }


    }
}

Dimitar
Telerik team
commented on 21 Feb 2022, 09:43 AM

Hi Jefferson,

Thank you for sharing your solution with the community. I am sure that someone will benefit from it. 

Regards,

Dimitar

 
Tags
PdfProcessing
Asked by
Blas
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Blas
Top achievements
Rank 1
Mikhail
Top achievements
Rank 1
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or