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
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
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
Yes, you're correct, that's the exact process. You can follow those steps and let us know if need any further assistance.
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
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
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);
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
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:
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
Hi Will,
Currently, we are working on this sample and will update further details on June 6, 2024.
Regards,
Jeyalakshmi T
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
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
Hi Will,
Yes, we need to provide the full path of the PDF from storage to get the file stream.
Regards,
Jeyalakshmi T