A way is to convert the .pdf to bitmaps with Windows.Data.Pdf
(by adding references to
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
and
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll)
then you can print the bitmaps silently
Print Pdf Without adobe
Subash
21
Reputation points
Hi,
I need to programmatically silent print a pdf file located in my file system without having external tools like adobe. I am currently using Gembox library to achieve this which requires a commercial licence. Is there any inbuild solution in WPF without using external tools or libraries?
Regards,
Subash C.
Developer technologies | Windows Presentation Foundation
Developer technologies | Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2 answers
Sort by: Most helpful
-
Castorix31 91,416 Reputation points2022-08-23T07:51:44.693+00:00 -
Castorix31 91,416 Reputation points2022-09-05T10:27:25.827+00:00 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); } }