How to print PDF with WPF ?

929Free 301 Reputation points
2020-06-22T07:40:40.21+00:00

I don't find WPF example. Who can help me? Thanks

Windows Presentation Foundation
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,783 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2020-06-23T01:54:11.147+00:00

    You can use DocumentViewer to view control that can host paginated FixedDocument content such as an XpsDocument. I will show you a sample of using DocumentViewer to print pdf:

    Here is the cs code:

     public MainWindow()
            {
                InitializeComponent();
                LoadDocumentViewer("E:\\WPF case\\0622\\pdf\\temp.xps", docViewer);
    
            }
    
            static XpsDocument xpsPackage = null;
            public static void LoadDocumentViewer(string xpsFileName, DocumentViewer viewer)
            {
                XpsDocument oldXpsPackage = xpsPackage;
                xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);
    
                FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();
                viewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
    
                if (oldXpsPackage != null)
                    oldXpsPackage.Close();
                xpsPackage.Close();
            }
    

    Add a DocumentViewer in the xaml:

        <DocumentViewer Name="docViewer"></DocumentViewer>
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.