10 Answers, 1 is accepted
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
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
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
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))
Hi Tanya,
I've opened a support ticket.
Thank you
Blas
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
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 Started, Exporting Fixed Page to Image.
Hope this is helpful.
Regards,
Tanya
Telerik
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
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
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
}
}
}
Hi Jefferson,
Thank you for sharing your solution with the community. I am sure that someone will benefit from it.
Regards,
Dimitar