How to print PDF with WPF ?

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

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

Developer technologies | Windows Presentation Foundation
0 comments No comments

Answer accepted by question author
DaisyTian-1203 11,651 Reputation points Moderator
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>

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Oldest

Your answer

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