C# WPF仅打印一页

C# WPF仅打印一页,c#,wpf,xaml,C#,Wpf,Xaml,首先,我不能在两面打印我的页面,因为我只是打印视觉效果,而没有使用flowdocument,所以我将所有页面都放在一个段落和一个flowdocument中。打印的一页看起来比以前好,但不会打印多页/面。所以在我的示例代码中,它只打印到第8条,不打印任何其他页面或侧面 我在一个side项目中尝试了一个简单的flowdocument示例,它似乎能够打印多页和双面,因此打印机不会受到怀疑 编辑:如果有人知道为什么白色背景的网格没有居中,并且被推到左侧,也会很有帮助 我的看法是: <UserCon

首先,我不能在两面打印我的页面,因为我只是打印视觉效果,而没有使用flowdocument,所以我将所有页面都放在一个段落和一个flowdocument中。打印的一页看起来比以前好,但不会打印多页/面。所以在我的示例代码中,它只打印到第8条,不打印任何其他页面或侧面

我在一个side项目中尝试了一个简单的flowdocument示例,它似乎能够打印多页和双面,因此打印机不会受到怀疑

编辑:如果有人知道为什么白色背景的网格没有居中,并且被推到左侧,也会很有帮助

我的看法是:

<UserControl x:Class="Eblacorp_internal.Views.PAFMPReportView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:viewmodel="clr-namespace:Eblacorp_internal.ViewModel"
             mc:Ignorable="d" 
             d:DesignHeight="1950" d:DesignWidth="800">

    <UserControl.DataContext>
        <viewmodel:PAFMReportViewModel/>
    </UserControl.DataContext>

    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50*"/>
        </Grid.RowDefinitions>

        <StackPanel  Grid.Row="0">
            <Grid x:Name="ButtonGrid" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>

                </Grid.ColumnDefinitions>
                <Button Content="Print" x:Name="PrintReportButton" Command="{Binding printButton}"></Button>


            </Grid>

        </StackPanel>

        <FlowDocumentScrollViewer Grid.Row="1">

                <FlowDocument x:Name="PAFMFlowDocument" FontSize="11">
                    <Paragraph>
                        <StackPanel>
                            <Grid Margin="10" x:Name="PAFMGridPage1" Background="White">
                <StackPanel Margin="5">
                    <Image Source="/Eblacorp_internal;component/assets/kuwaitLogo.jpg" MaxWidth="30" MaxHeight="40"></Image>
                    <TextBlock HorizontalAlignment="Center" Text="الهيئة العـــــــــامة للقـــوى العــامـلة"></TextBlock>
                    <TextBlock HorizontalAlignment="Center" Text="The Public Authority For Manpower"></TextBlock>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" BorderThickness="1.1" BorderBrush="Black">
                            <StackPanel Margin="5" Grid.Column="0">
                                <TextBlock HorizontalAlignment="Center" TextDecorations="Underline" Text="Sample Form an Employment Contract in the Civil Sector"></TextBlock>
                                <TextBlock Margin="30 10 20 0" Text="State o Kuwait"></TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>Public Authority of Manpower/</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <TextBlock FontFamily="Bold">Labour Department</TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>On</TextBlock>
                                    <TextBox BorderThickness="0" Foreground="Red" Margin="5 0 5 0" MinWidth="80">Enter Text Here</TextBox>
                                    <TextBlock>corresponing to</TextBlock>
                                    <DatePickerTextBox BorderThickness="0" Foreground="Red"></DatePickerTextBox>
                                    <TextBlock>the present</TextBlock>
                                </StackPanel>
                                <TextBlock>contract was concluded by and between:</TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>1. Company/</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <TextBlock>2. Represented in signature in the present contract by:</TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>Name:</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>Civil Card:</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <TextBlock>(FirstParty)</TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>3. Name:</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>4. Nationality:</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>Civil Card:</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>Residence:</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <TextBlock>(Second Party)</TextBlock>
                                <!--PREAMBLE-->
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Preamble</TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>The first party owns the facility entitled</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>working in the field of</TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                </StackPanel>
                                <TextBlock>whereas it wished to conclude a contract with the second party to work</TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>or it in the profession of </TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                    <TextBlock> whereas the parties</TextBlock>
                                </StackPanel>
                                <TextBlock>acknowledged their capacity to conclude this contract; they agreed </TextBlock>
                                <TextBlock>upon the following:</TextBlock>
                                <!--Article One-->
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article One</TextBlock>
                                <TextBlock>The preamble above shall constitute an integral part of the present</TextBlock>
                                <TextBlock>contract.</TextBlock>
                                <!--Article Two "Nature of the Work-->
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Two</TextBlock>
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Nature of the Work</TextBlock>
                                <TextBlock>The first party concluded a contract with the second party to work for </TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>it in the profession of </TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                    <TextBlock>in the State of Kuwait.</TextBlock>
                                </StackPanel>
                                <!--Article Three-->
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Three</TextBlock>
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Probation Period"</TextBlock>
                                    <TextBlock>The second party shall be subject to a probation period for a term 
                                        <LineBreak/>not exceeding 100 work days. Each party shall have the right to
                                     <LineBreak/> terminate the contract during the said term without notification.</TextBlock>
                                <!--Article Four-->
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Four</TextBlock>
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Lease Value"</TextBlock>
                                <TextBlock>For executing the present contract, the second party shall receive the</TextBlock>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock> wage of</TextBlock>
                                        <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                        <TextBlock>/~ dinars to be paid at the end of every</TextBlock>
                                    </StackPanel>
                                    <TextBlock> month. The first party may not decrease the wage during the
                                    <LineBreak/>term of the contract. It may not transfer the partly to daily wage
                                    <LineBreak/>without his approval.</TextBlock>
                                <!--Article Five-->
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Five</TextBlock>
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Contract Term"</TextBlock>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock>The contract shall come into force on </TextBlock>
                                        <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                        <TextBlock>The second</TextBlock>
                                    </StackPanel>
                                    <TextBlock> party  shall  execute his work during the entire execution term thereof.</TextBlock>
                                 <!--Article Six-->
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Six</TextBlock>
                                <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Contract Term"</TextBlock>
                                <TextBlock>The present contract has a definite term. It shall come into force on</TextBlock>
                                <StackPanel Orientation="Horizontal">
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                    <TextBlock>for a term of </TextBlock>
                                    <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                    <TextBlock>the contract maybe </TextBlock>
                                </StackPanel>
                                <TextBlock>renewed with the approval of the parties for similar terms not 
                                <LineBreak/>exceeding five years. The present contract has an indefinite term and 
                                <LineBreak/>it shall  come  into force on
                                </TextBlock>
                                <TextBox BorderThickness="0" TextDecorations="UnderLine" MinWidth="80" Width="auto" Text="--INSERT HERE--" Foreground="Red"></TextBox>
                                <TextBlock>*Considering the contract as having a definite or indefinite term
                                <LineBreak/>shall be subject to the will of the two parties.</TextBlock>


                                </StackPanel>



                            <!--Arabic Side-->
                        </Border>
                            <Border Grid.Column="1" BorderThickness="1.1" BorderBrush="Black">
                            <StackPanel Grid.Column="1">
                                    <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">نموذج عقد عمل استرشادي</TextBlock>
                                    <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">في القطاع الأهلي</TextBlock>
                                </StackPanel>
                            </Border>

                        </Grid>

                </StackPanel>


            </Grid>
            <Grid x:Name="PAFMGridPage2" Margin="10" Background="White">
                    <StackPanel Margin="5">

                        <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Margin="0" Grid.Column="0">
                            <Border BorderThickness="1.1" BorderBrush="Black">
                                <StackPanel Margin="5">
                                    <!--Article Seven-->
                                    <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Seven</TextBlock>
                                    <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Annual Leave"</TextBlock>
                                    <TextBlock>The second party shall have the right to a paid annual leave with 
                                <LineBreak/>a term of 30 days. It shall   not be due on the first year save after the
                                <LineBreak/>expiration of nine months to be calculated from the date of the 
                                <LineBreak/>contract into force.</TextBlock>
                                        <!--Article Eight-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Eight</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Number of Work Hours"</TextBlock>
                                        <TextBlock>The first party may not require that the second party work for a term 
                                <LineBreak/>exceeding eight daily work hours with rest periods not less than one 
                                <LineBreak/>except for the cases set forth in the law.</TextBlock>
                                        <!--Article Nine-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Nine</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Ticket Value"</TextBlock>
                                        <TextBlock>The first party shall bear the expenses of the return of the second party 
                                <LineBreak/>to his country the expiration of the work relationship and his final 
                                <LineBreak/> departure from the country.</TextBlock>
                                        <!--Article Ten-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Ten</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Insurance against Injuries and Work Maladies"</TextBlock>
                                        <TextBlock>The first party shall insure the second party against injuries and work
                                <LineBreak/>maladies. It shall commit to the health insurance value in accordance
                                <LineBreak/>with the law No. (1) of the year 1999.</TextBlock>
                                        <!--Article Eleven-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Eleven</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"End of Service Benefit"</TextBlock>
                                        <TextBlock>The second party shall be due the end of service benefit as set forth
                                <LineBreak/> the regulating laws</TextBlock>
                                        <!--Article Twelve-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Twelve</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Applicable Law"</TextBlock>
                                        <TextBlock>The provisions of the Labour code in the civil sector No. 6 of 2010 and 
                                <LineBreak/>the decisions executing same shall apply for all matters not provided 
                                <LineBreak/>for in the present contract. Shall be considered null every condition 
                                <LineBreak/>agreed upon in violation of the provisions of the law, unless the same 
                                <LineBreak/> has a better benefit for the worker.</TextBlock>
                                        <!--Article Thirteen-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Thirteen</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Special Conditions"</TextBlock>
                                        <TextBlock>1-……………………N/A………………….
                                <LineBreak/>2-……………………N/A………………….
                                <LineBreak/>3-……………………N/A………………….</TextBlock>
                                        <!--Article Fourteen-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Fourteen</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Special Conditions"</TextBlock>
                                        <TextBlock>The court of first instance and its Labour departments, in accordance 
                                <LineBreak/>with the provisions of the law No. 46 of the year 1987, shall be
                                <LineBreak/>competent to peruse any conflicts resulting from the execution or 
                                <LineBreak/> interpretation of the present contract.</TextBlock>
                                        <!--Article Fifteen-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Fifteen</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Contract Language"</TextBlock>
                                        <TextBlock>The  present  contract  was  made  in  Arabic and English. The  Arabic  texts   
                                <LineBreak/>shall in the case of any conflict between them.</TextBlock>
                                        <!--Article Sixteen-->
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Sixteen</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Contract Copies"</TextBlock>
                                        <TextBlock>The present contract was made in three copies, one for each party to
                                <LineBreak/>work in accordance therewith. The third copy shall be deposited at 
                                <LineBreak/>the Public Authority for Manpower.</TextBlock>
                                    </StackPanel>
                            </Border>
                        </StackPanel>









                            <!--Arabic Side-->
                        <StackPanel Margin="0" Grid.Column="1">
                                <Border BorderThickness="1.1" BorderBrush="Black">
                                    <StackPanel Margin="5">
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 5 0 0">Article Seven</TextBlock>
                                        <TextBlock FontFamily="Bold" TextDecorations="underline" HorizontalAlignment="Center" Margin="0 0 0 0">"Annual Leave"</TextBlock>
                                        <TextBlock>The second party shall have the right to a paid annual leave with 
                                <LineBreak/>a term of 30 days. It shall   not be due on the first year save after the
                                <LineBreak/>expiration of nine months to be calculated from the date of the 
                                <LineBreak/>contract into force.</TextBlock>
                                    </StackPanel>
                                </Border>




                            </StackPanel>



                    </Grid>

                    </StackPanel>




                </Grid>
        </StackPanel>
                    </Paragraph>
                </FlowDocument>
            </FlowDocumentScrollViewer>

    </Grid>
</UserControl>

问题在于把所有的东西都放在一个地方

<段落>

元素。只要我再加一个

<段落>

元素,所以我想要的每一页都在其中一页之间,它打印得很好,并且在需要的区域打破了页面。希望这对任何人都有帮助

using Eblacorp_internal.Commands;
using Eblacorp_internal.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Eblacorp_internal.ViewModel
{
    class PAFMReportViewModel
    {
        //print button
        public RelayCommand printButton { set; private get; }

        public void printCommand(object obj)
        {
            PAFMPReportView PAFMReportView = new PAFMPReportView();

            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() != true) return;

            PAFMReportView.PAFMFlowDocument.PageHeight = pd.PrintableAreaHeight;
            PAFMReportView.PAFMFlowDocument.PageWidth = pd.PrintableAreaWidth;
            PAFMReportView.PAFMFlowDocument.ColumnWidth = pd.PrintableAreaWidth;
            PAFMReportView.PAFMFlowDocument.ColumnGap = 0;

            IDocumentPaginatorSource idocument = PAFMReportView.PAFMFlowDocument as IDocumentPaginatorSource;

            pd.PrintDocument(idocument.DocumentPaginator, "Printing Flow Document...");
        }

        public PAFMReportViewModel()
        {
            printButton = new RelayCommand(printCommand);
        }
    }
}