I've been reading all through StackOverflows regarding the limitation of printing PDF in UWP. One of the threads How to print PDF in UWP without loosing quality after rasterization to PNG summarizes it pretty well.
I took the following steps to print the images of the PDF file
Load the PDF from the local folder
StorageFile f = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("pdffile.pdf"); PdfDocument doc = await PdfDocument.LoadFromFileAsync(f); Load(doc);
Convert the PDF to images and put the images into observable collection and saves it into the local folder
async void Load(PdfDocument pdfDoc) { PdfPages.Clear(); for (uint i = 0; i < pdfDoc.PageCount; i++) { BitmapImage image = new BitmapImage(); var page = pdfDoc.GetPage(i); using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) { await page.RenderToStreamAsync(stream); await image.SetSourceAsync(stream); } PdfPages.Add(image); StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("pdffilepage" + i +".jpg"); var stream2 = await file.OpenStreamForWriteAsync(); var serializer = new DataContractSerializer(typeof(ObservableCollection<BitmapImage>)); serializer.WriteObject(stream2, PdfPages[(int)i]); await stream2.FlushAsync(); } Debug.WriteLine("Writing file finished"); }
In this step I got the following error:
System.Runtime.Serialization.InvalidDataContractException: "Type 'Windows.UI.Xaml.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required."
I have no idea what to do with the above mentioned error.