This test worked for me (10.0.19041.0 is my Windows 10 SDK version, should be changed if you have other versions) :
References :
// Add reference :
// "C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.19041.0\Windows.winmd"
// "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll"
Test with a random pdf
LoadPdfDocument(@"E:\sample.pdf");
Test Function to load the pages of pdf, then print them on the default printer
private async void LoadPdfDocument(string sFile)
{
try
{
var storagePdfFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(sFile);
Windows.Data.Pdf.PdfDocument pdfDocument;
pdfDocument = await Windows.Data.Pdf.PdfDocument.LoadFromFileAsync(storagePdfFile, "");
uint nPageIndex = 0;
while (nPageIndex < pdfDocument.PageCount)
{
using (Windows.Data.Pdf.PdfPage pdfPage = pdfDocument.GetPage(nPageIndex))
{
using (Windows.Storage.Streams.InMemoryRandomAccessStream memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
{
Windows.Data.Pdf.PdfPageRenderOptions pdfPageRenderOptions = new Windows.Data.Pdf.PdfPageRenderOptions();
await pdfPage.RenderToStreamAsync(memStream);
var bi = new System.Windows.Media.Imaging.BitmapImage();
bi.BeginInit();
//bi.CacheOption = BitmapCacheOption.OnLoad;
// using System.IO
Stream inputStream = memStream.AsStream();
bi.StreamSource = inputStream;
bi.EndInit();
// Test saving to file (use pdfPageRenderOptions to increase size for better quality)
//BitmapEncoder encoder = new PngBitmapEncoder();
//encoder.Frames.Add(BitmapFrame.Create(bi));
//using (var fileStream = new System.IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Page_" + nPageIndex.ToString("00") + ".png", System.IO.FileMode.Create))
//{
// encoder.Save(fileStream);
//}
var dw = new DrawingVisual();
using (var dc = dw.RenderOpen())
{
dc.DrawImage(bi, new Rect { Width = bi.Width, Height = bi.Height });
}
var pd = new PrintDialog();
pd.PrintVisual(dw, "Page_" + nPageIndex.ToString("00"));
}
}
nPageIndex++;
}
}
catch (System.Exception ex)
{
MessageBox.Show("Error : " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}