Print dialog and print prewiew dialog for WPF

前端 未结 3 1305
既然无缘
既然无缘 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:41

    What you want to do, is to create an xpsDocument out from the content you want to print (a flowDocument) and use that XpsDocument to preview the content, for example let say you have the following Xaml, with a flowDocument that you want to print its content :

     
        
            
            
        
        
            
                
                    
                    WPF
                
    
                
                    WPF, which stands for
                    Windows Presentation Foundation ,
                    is Microsoft's latest approach to a GUI framework, used with the .NET framework.
                    Some advantages include:
                
    
                
                    
                        
                            It's newer and thereby more in tune with current standards
                        
                    
                    
                        
                            Microsoft is using it for a lot of new applications, e.g. Visual Studio
                        
                    
                    
                        
                            It's more flexible, so you can do more things without having to write or buy new controls
                        
                    
                
    
            
                
        
    
    

    the flowDocument Sample is from Wpf tutorials site

    the print button Click event handler should looks like this :

     private void Button_Click(object sender, RoutedEventArgs e)
        {
    if (File.Exists("printPreview.xps"))
                {
                    File.Delete("printPreview.xps");
                }
            var xpsDocument = new XpsDocument("printPreview.xps", FileAccess.ReadWrite);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            writer.Write(((IDocumentPaginatorSource)FD).DocumentPaginator);            
            Document = xpsDocument.GetFixedDocumentSequence();
            xpsDocument.Close();
            var windows = new PrintWindow(Document);
            windows.ShowDialog();
        }
    
    public FixedDocumentSequence Document { get; set; }
    

    so here you are mainly :

    • Creating an Xps document and storing it in printPreview.xps file,
    • Writing the FlowDocument content into that file,
    • passing the XpsDocument to the PrintWindow in which you will handle the preview and the print actions,

    here how the PrintWindow looks like :

    
        
            
            
        
        
            
            
        
                    
        
    
    

    and the code behind :

    public partial class PrintWindow : Window
    {
        private FixedDocumentSequence _document;
        public PrintWindow(FixedDocumentSequence document)
        {
            _document = document;
            InitializeComponent();
            PreviewD.Document =document;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //print directly from the Xps file 
        }
    }
    

    the final result looks something like this

    enter image description here

    Ps: to use XpsDocument you should add a reference to System.Windows.Xps.Packaging namespace

提交回复
热议问题