Print Pdf Without adobe

Subash 21 Reputation points
2022-08-23T07:45:40.39+00:00

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
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-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);  
                }  
            }  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Castorix31 90,686 Reputation points
    2022-08-23T07:51:44.693+00:00

    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.