Print dialog and print prewiew dialog for WPF

前端 未结 3 1309
既然无缘
既然无缘 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 13:01

    this is the sample solution for print prewiew : MainWindow.xaml

    
            
                                    
                    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
                        
                    
                
            
    
    
    

    MainWindow.xaml.cs

     private void Button_Click(object sender, RoutedEventArgs e)
            {
                //Fix the size of the document
                PrintDialog pd = new PrintDialog();
                FD.PageHeight = pd.PrintableAreaHeight;
                FD.PageWidth = pd.PrintableAreaWidth;
                FD.PagePadding = new Thickness(30);
                FD.ColumnGap = 0;
                FD.ColumnWidth = pd.PrintableAreaWidth;
    
                ////to print the document directly without print preview
                //IDocumentPaginatorSource dps = FD;
                //pd.PrintDocument(dps.DocumentPaginator, "flow doc");
    
                //Print preview the document before printing
                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 PrintPreview(Document);
                windows.ShowDialog();
            }
    

    PrintPreview.xaml

            
            
    
    

    PrintPreview.xaml.cs

    public partial class PrintPreview : Window
    {
        private FixedDocumentSequence _document;
        public PrintPreview(FixedDocumentSequence document)
        {
            _document = document;
            InitializeComponent();
            PreviewD.Document = document;
        }
    }
    

提交回复
热议问题