Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

WPF Documentviewer Druckvorschau beim Drucken

16.03.2019 (👁219)


Wenn man in WPF ein Dokument oder WPF Element drucken möchte, dann kann man den Druckvorgang als Vorschau anzeigen in einem WPF Element vom Typ: Documentviewer

        <DocumentViewer  Grid.Column="1" x:Name="PreviewD" 

                         ShowPageBorders="True"                          

                         >

        </DocumentViewer>

private void BtnPrint_Click(object sender, RoutedEventArgs e)

{

    clsPrinter.Print_WPF_Preview(Grid_Plan);

    }

In der Anwendung:

Beim Aufruf erscheint dann ein Fenster, welches das  Dokument in der Vorschau zeigt

WPF Windows

Xaml code

<Window x:Class="Schichtplaner.Print_Window"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:Schichtplaner"

        mc:Ignorable="d"

        Title="Print_Window" Height="450" Width="800">

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="60"/>

            <ColumnDefinition Width="*"/>

        </Grid.ColumnDefinitions>

        <StackPanel>

            <Button x:Name="BtnPrint" Content="Print" Click="BtnPrint_Click"></Button>

            <!--Other print operations-->

        </StackPanel>

        <DocumentViewer  Grid.Column="1" x:Name="PreviewD" 

                         ShowPageBorders="True" 

                         

                         >

        </DocumentViewer>

    </Grid>

</Window>

 

 

 

Druck-Code beim Laden eines Dokuments

using System;

using System.Collections.Generic;

using System.Linq;

using System.Printing;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

using System.Windows.Xps;

 

namespace Schichtplaner

{

    /// <summary>

    /// Interaction logic for Print_Window.xaml

    /// </summary>

    public partial class Print_Window : Window

    {

        private FixedDocumentSequence _document;

 

        public Print_Window(FixedDocumentSequence document)

        {

            //--------< Initialize with document >------------

            //*open window with document as parameter or argument

 

            //--< set_preview >--

            _document = document;

            //--</ set_preview >--

 

            InitializeComponent();

 

            //< set visible >

            PreviewD.Document = document;

            //</ set visible >

            //--------</ Initialize with document >------------

        }

 

        private void BtnPrint_Click(object sender, RoutedEventArgs e)

        {

            Print_Document();

        }

 

        

        public void Print_Document()

        {

            //----------------< Print_Document() >-----------------------

            //----< Get_Print_Dialog_and_Printer >----

            PrintDialog printDialog = new PrintDialog();

            printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();

            printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;

            //----</ Get_Print_Dialog_and_Printer >----

 

            //*set the default page orientation based on the desired output.

            printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;

            printDialog.PrintTicket.PageScalingFactor = 90;

            printDialog.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);

            //printDialog.PrintableAreaHeight ; //*get

            //printDialog.PrintableAreaWidth;   //get

            //printDialog.PrintDocument.

            printDialog.PrintTicket.PageBorderless = PageBorderless.None;

 

            if (printDialog.ShowDialog() == true)

            {

                //----< print >----   

                // set the print ticket for the document sequence and write it to the printer.

                _document.PrintTicket = printDialog.PrintTicket;

 

                //-< send_document_to_printer >-

                XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);

                writer.WriteAsync(_document, printDialog.PrintTicket);

                //-</ send_document_to_printer >-

                //----</ print >----   

            }

            //----------------</ Print_Document() >-----------------------

        }

 

 

    }

}