How to make a thumbnail from a PdfDocument

Using Syncfusion's PdfDocument we create a PDF from a photo (of a text document). The app allows for several of these PDFs to be made.

Next, we would like the user to be able to select one of those PDFs by viewing thumbnails of the PDFs on their device. The PDFs are stored locally on the device. How do we accomplish that?

We can readily get a List of Paths to the PDFs that are to be used for creating the thumbnails. How do we get a thumbnail for each? How do we display them in a CollectionView?

Thanks


12 Replies

JT Jeyalakshmi Thangamarippandian Syncfusion Team May 7, 2024 01:14 PM UTC

Hi will Autio,

We can generate thumbnail image for every PDF document first page using our Syncfusion.Maui.PdfToImageConverter.

Class PdfToImageConverter - API Reference (syncfusion.com)

Kindly use the following code snippet for reference,

//Initialize Pdf to Image converter instance.

PdfToImageConverter imageConverter = new PdfToImageConverter(pdfStream);

//Converts PDF to image stream with reduced scaling for thumbnail

Stream imageStream = imageConverter.Convert(0, 0.5f);

//Dispose PDF to image converter instance.

imageConverter.Dispose();

Kindly use the following link to download the NuGet package,

NuGet Gallery | Syncfusion.Maui.PdfToImageConverter 25.1.42


Regards,

Jeyalakshmi T



WA Will Autio replied to Jeyalakshmi Thangamarippandian May 7, 2024 07:49 PM UTC

Hi  Jeyalakshmi,


Thanks for your reply. What I'm not sure how to do is:

Given a PDF on a device in our app's allowed file space, to read in the PDF into a pdfStream.


And let's see if I understand this correctly: 

I read in data from a PDF into a pdfStream.

Use the pdfStream to construct a  PdfToImageConverter.

Use the imageConverter to convert the PDF to an image and resize it (I really like that part) so that it can behave like a thumbnail and be displayed in an Image object within a CollectionView.


That sounds easy. 


Regards,

Will



KS Karmegam Seerangan Syncfusion Team May 8, 2024 04:55 PM UTC

Yes, you're correct, that's the exact process. You can follow those steps and let us know if need any further assistance. 



WA Will Autio replied to Karmegam Seerangan May 8, 2024 09:14 PM UTC

What I'm not sure how to do is:

Given a PDF on a device in our app's allowed file space, how to read in the PDF into a pdfStream.

Thanks,

Will



JT Jeyalakshmi Thangamarippandian Syncfusion Team May 9, 2024 01:26 PM UTC

Hi Will,

Kindly use the following UG documentation to open an existing PDF document in .NET MAUI.

Open and Save PDF Documents in .NET MAUI | Syncfusion

Kindly use the following UG documentation for saving the stream as a physical file, https://help.syncfusion.com/file-formats/pdf/loading-and-saving-document/open-and-save-pdf-documents-in-maui#helper-files-for-net-maui

A complete working sample for loading PDF document and saving the stream as a physical file can be downloaded from Github.


Regards,

Jeyalakshmi T



WA Will Autio May 9, 2024 05:51 PM UTC

That did not answer my question:


This is what I needed:

Stream file2 = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read);

And then I modified it for my scenatio:

Stream file2 = new FileStream(Path_and_Filename, FileMode.Open, FileAccess.Read, FileShare.Read);


AG Anantha Gokula Raman Jeyaraman Syncfusion Team May 10, 2024 02:47 PM UTC

Hi Will,

It is not possible to get the file stream using FileStream in .NET MAUI.

To get the PDF as stream from app memory, we can use the approach used in Open and Save PDF Documents in .NET MAUI | Syncfusion


To get the PDF from device external storage, we may need to use File picker - .NET MAUI | Microsoft Learn

Regards,

Anantha Gokula Raman J



WA Will Autio June 3, 2024 04:16 PM UTC

HI. I'm finally back on this project. 

In Xamarin Forms, we had to use a DependencyService in order to read a PDF into a MemoryStream like this:

https://support.syncfusion.com/kb/article/7799/how-to-load-pdf-from-file-system?isInternalRefresh=False

It is not an embedded resource or a URI. But I do have the full path to where the PDFs are located. How do we read in a PDF in Maui? Do we need a  DependencyService like in Xamarin or if there is another way what is it, please.

Regards,

Will



JT Jeyalakshmi Thangamarippandian Syncfusion Team June 4, 2024 01:28 PM UTC

Hi Will,

Currently, we are working on this sample and will update further details on June 6, 2024.

Regards,

Jeyalakshmi T




JT Jeyalakshmi Thangamarippandian Syncfusion Team June 6, 2024 12:31 PM UTC

Hi Will,

Kindly use the following link to download the sample to achieve your requirement,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfToImageSample971522379

In Android, Kindly include necessary permissions in the "AndroidManifest.xml" and "MainActivity.cs" file as per the sample for accessing external storage.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Regards,

Jeyalakshmi T



WA Will Autio June 7, 2024 01:38 AM UTC

Hi  Jeyalakshmi ,

Thanks for the sample code. It did not work immediately. But from reading the 

public partial MemoryStream GetFileStream()

I determined that what I needed was to pass in the FullPath of the pdf that I wanted displayed so that GetFileStream looked something like this:

        public partial MemoryStream GetFileStream(string fullPath)

        {

            if (!CheckAndRequestPermissions())

            {

                return new MemoryStream(File.ReadAllBytes(fullPath));

            }

            return null;

        }


Regards,

Will



JT Jeyalakshmi Thangamarippandian Syncfusion Team June 7, 2024 01:51 PM UTC

Hi Will,

Yes, we need to provide the full path of the PDF from storage to get the file stream.


Regards,

Jeyalakshmi T



Loader.
Up arrow icon