Print dialog and print prewiew dialog for WPF

前端 未结 3 1307
既然无缘
既然无缘 2021-01-31 11:59

Is there a print dialog for WPF that is combinated whit a print preview dialog in WPF like Google Chrome or Word does?

At this moment I use a the print preview

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 12:39

    Your requirements can be achieved in a number of ways, for instance, you can use the PrintDialog class. The following MSDN pages contains descriptions as well as some sample code:

    • WinForms: System.Windows.Forms.PrintDialog
    • WPF: System.Windows.Controls.PrintDialog (thanks to Bartosz)

    Alternatively it can be achieved via C# ,for example, consider the next code:

     private string _previewWindowXaml =
        @"
                          
         ";
    
    internal void DoPreview(string title)
    {
        string fileName = System.IO.Path.GetRandomFileName();
        FlowDocumentScrollViewer visual = (FlowDocumentScrollViewer)(_parent.FindName("fdsv1"));
    
        try
        {
            // write the XPS document
            using (XpsDocument doc = new XpsDocument(fileName, FileAccess.ReadWrite))
            {
                XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
                writer.Write(visual);
            }
    
            // Read the XPS document into a dynamically generated
            // preview Window 
            using (XpsDocument doc = new XpsDocument(fileName, FileAccess.Read))
            {
                FixedDocumentSequence fds = doc.GetFixedDocumentSequence();
    
                string s = _previewWindowXaml;
                s = s.Replace("@@TITLE", title.Replace("'", "'"));
    
                using (var reader = new System.Xml.XmlTextReader(new StringReader(s)))
                {
                    Window preview = System.Windows.Markup.XamlReader.Load(reader) as Window;
    
                    DocumentViewer dv1 = LogicalTreeHelper.FindLogicalNode(preview, "dv1") as DocumentViewer;
                    dv1.Document = fds as IDocumentPaginatorSource;
    
    
                    preview.ShowDialog();
                }
            }
        }
        finally
        {
            if (File.Exists(fileName))
            {
                try
                {
                    File.Delete(fileName);
                }
                catch
                {
                }
            }
        }
    } 
    

    What it does: it actually prints the content of a visual into an XPS document. Then it loads the "printed" XPS document and displays it in a very simple XAML file that is stored as a string, rather than as a separate module, and loaded dynamically at runtime. The resulting Window has the DocumentViewer buttons: print, adjust-to-max-page-width, and so on.

    I also added some code to hide the Search box. See this answer to WPF: How can I remove the searchbox in a DocumentViewer? for how I did that.

    The effect is like this:

    The XpsDocument can be found in the ReachFramework dll and the XpsDocumentWriter can be found in the System.Printing dll both of which must be added as references to the project

提交回复
热议问题