WPF打印京東電子面單(能夠異步)

參考:https://www.cnblogs.com/guogangj/archive/2013/02/27/2934733.htmlhtml

模板:JDFlowDocument.xamlapp

<FlowDocument PageWidth="450" PagePadding="0 0 0 0" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" FontFamily="微軟雅黑" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" TextOptions.TextFormattingMode="Display">
    <Paragraph>
        <Grid>
            <WrapPanel Width="380" Margin="-35 -40 0 0">
                <StackPanel Width="380">
                    <StackPanel Width="48" Height="50" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0 0 10 0">
                        <Label>
                            <TextBlock FontSize="16" TextWrapping="Wrap">標準快遞</TextBlock>
                        </Label>
                    </StackPanel>
                </StackPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 5 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>
                <!--大頭筆-->
                <StackPanel Width="380" Height="55" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0 -10 0 0">
                    <Label Content="{Binding BigShotName}" FontWeight="Bold" FontSize="45"></Label>
                </StackPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 10 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>
                <!--集包地名稱-->
                <WrapPanel Width="380" Height="35" Margin="0 3 0 0" VerticalAlignment="Center">
                    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Left" Width="35">
                        <Border HorizontalAlignment="Center" VerticalAlignment="Center" Width="35" Height="35" CornerRadius="0" BorderBrush="Black" BorderThickness="3">
                            <TextBlock Text="" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Center">
                        <Label Content="{Binding GatherCenterName}" FontSize="35" FontWeight="Bold"/>
                    </StackPanel>
                </WrapPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 3 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>
                <!--收件、寄件-->
                <WrapPanel Width="380" Margin="0 -5 0 0">
                    <!--收件-->
                    <WrapPanel Width="380" Height="60">
                        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Width="30" Height="40">
                            <Label>
                                <TextBlock TextWrapping="Wrap" Text="" FontSize="20" FontWeight="Bold"></TextBlock>
                            </Label>
                        </StackPanel>
                        <StackPanel>
                            <WrapPanel>
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding ToFullName}"  FontSize="12" FontWeight="Bold"></TextBlock>
                                </Label>
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding ToMobile}"  FontSize="12" FontWeight="Bold"></TextBlock>
                                </Label>
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding ToTelephone}"  FontSize="12" FontWeight="Bold"></TextBlock>
                                </Label>
                            </WrapPanel>
                            <WrapPanel Width="340" Margin="0 -10 0 0">
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding ToFullAddress}"  FontSize="12" FontWeight="Bold"></TextBlock>
                                </Label>
                            </WrapPanel>
                        </StackPanel>
                    </WrapPanel>
                    <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 -5 0 0">
                        <Border.BorderBrush>
                            <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Color="Black" Offset="0"/>
                                    <GradientStop Color="Black" Offset="0.2"/>
                                    <GradientStop Color="Transparent" Offset="0.4"/>
                                    <GradientStop Color="Transparent" Offset="0.6"/>
                                    <GradientStop Color="Black" Offset="0.8"/>
                                    <GradientStop Color="Black" Offset="1"/>
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Border.BorderBrush>
                    </Border>
                    <!--寄件-->
                    <WrapPanel Width="380" Margin="0 -5 0 0">
                        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Width="30" Height="30">
                            <Label>
                                <TextBlock TextWrapping="Wrap" Text="" FontSize="20" FontWeight="Bold"></TextBlock>
                            </Label>
                        </StackPanel>
                        <StackPanel>
                            <WrapPanel>
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding FromFullName}" FontSize="10" ></TextBlock>
                                </Label>
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding FromMobile}" FontSize="10" ></TextBlock>
                                </Label>
                            </WrapPanel>
                            <StackPanel Width="340" HorizontalAlignment="Left" Margin="0 -8 0 0">
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding FromFullAddress}" FontSize="10" ></TextBlock>
                                </Label>
                            </StackPanel>
                        </StackPanel>
                    </WrapPanel>
                </WrapPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 0 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>
                <!--面單號 條形碼-->
                <StackPanel Width="380" Height="65" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0 5 0 0">
                    <StackPanel Width="380" Height="65" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Image Source="{Binding WaybillCodeBarCodeUrl1}" />
                    </StackPanel>
                </StackPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 5 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>
                <!--簽收人-->
                <WrapPanel Width="380" Height="60">
                    <StackPanel Width="80">
                        <Label Height="23" Margin="0 -5 0 0">
                            <TextBlock TextWrapping="Wrap" Text="{Binding Time1}" FontSize="8"></TextBlock>
                        </Label>
                        <Label Height="23" Margin="0 -10 0 0">
                            <TextBlock TextWrapping="Wrap" Text="{Binding Time2}" FontSize="10"></TextBlock>
                        </Label>
                        <Label Height="23" Margin="0 -10 0 0">
                            <TextBlock TextWrapping="Wrap" Text="{Binding Numbers}" FontSize="10"></TextBlock>
                        </Label>
                    </StackPanel>
                    <StackPanel Width="220">
                        <Label>
                            <TextBlock TextWrapping="Wrap" Text="{Binding SignDes}" FontSize="8"></TextBlock>
                        </Label>
                    </StackPanel>
                    <!--快遞公司二維碼 承運商編碼-->
                    <StackPanel Width="60" Height="60" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <Image Width="60" Height="60" Source="{Binding ProviderCodeBarCodeUrl}" />
                        </StackPanel>
                    </StackPanel>
                </WrapPanel>

                <!--撕單分隔線-->

                <!--面單號 條形碼-->
                <StackPanel Width="380" Margin="0 50 0 0">
                    <StackPanel Width="280" Height="50" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0 0 10 0">
                        <Image Source="{Binding WaybillCodeBarCodeUrl2}" />
                    </StackPanel>
                </StackPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 0 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>
                <!--收件、寄件-->
                <WrapPanel Width="380" Margin="0 -5 0 0">
                    <StackPanel Width="300">
                        <!--收件-->
                        <WrapPanel Width="300" Height="40">
                            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Width="30" Height="40">
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="" FontSize="20" FontWeight="Bold"></TextBlock>
                                </Label>
                            </StackPanel>
                            <StackPanel Width="270" Height="40">
                                <WrapPanel >
                                    <Label>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding ToFullName}"  FontSize="8" ></TextBlock>
                                    </Label>
                                    <Label>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding ToMobile2}"  FontSize="8"></TextBlock>
                                    </Label>
                                    <Label>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding ToTelephone}"  FontSize="8"></TextBlock>
                                    </Label>
                                </WrapPanel>
                                <WrapPanel Width="280" Margin="0 -8 0 0">
                                    <Label>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding ToFullAddress}"  FontSize="8"></TextBlock>
                                    </Label>
                                </WrapPanel>
                            </StackPanel>
                        </WrapPanel>
                        <Border Width="300" Height="1" CornerRadius="0" BorderThickness="1">
                            <Border.BorderBrush>
                                <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStop Color="Black" Offset="0"/>
                                        <GradientStop Color="Black" Offset="0.2"/>
                                        <GradientStop Color="Transparent" Offset="0.4"/>
                                        <GradientStop Color="Transparent" Offset="0.6"/>
                                        <GradientStop Color="Black" Offset="0.8"/>
                                        <GradientStop Color="Black" Offset="1"/>
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Border.BorderBrush>
                        </Border>
                        <!--寄件-->
                        <WrapPanel Width="300" Margin="0 -5 0 0">
                            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Width="30" Height="40">
                                <Label>
                                    <TextBlock TextWrapping="Wrap" Text="" FontSize="20" FontWeight="Bold"></TextBlock>
                                </Label>
                            </StackPanel>
                            <StackPanel Width="270" Height="40">
                                <WrapPanel>
                                    <Label>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding FromFullName}" FontSize="8" ></TextBlock>
                                    </Label>
                                    <Label>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding FromMobile}" FontSize="8" ></TextBlock>
                                    </Label>
                                </WrapPanel>
                                <StackPanel Width="280" Margin="0 -8 0 0">
                                    <Label>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding FromFullAddress}" FontSize="8" ></TextBlock>
                                    </Label>
                                </StackPanel>
                            </StackPanel>
                        </WrapPanel>
                    </StackPanel>
                    <Border Width="1" Height="65" CornerRadius="0" BorderThickness="1" Margin="0 0 0 0">
                        <Border.BorderBrush>
                            <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Color="Black" Offset="0"/>
                                    <GradientStop Color="Black" Offset="1"/>
                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Border.BorderBrush>
                    </Border>
                    <!--電子面單碼-->
                    <StackPanel Width="60" Height="60" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <Image Width="60" Height="60" Source="{Binding WaybillCodeQRCodeUrl}" />
                        </StackPanel>
                    </StackPanel>
                </WrapPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 -5 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>

                <StackPanel Width="380" Height="90" HorizontalAlignment="Left">
                    <TextBlock TextWrapping="Wrap" Text="{Binding CustomArea}" HorizontalAlignment="Left" FontSize="10"></TextBlock>
                </StackPanel>
                <StackPanel Width="380" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                    <Label Content="已驗視" FontSize="12" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0 0 10 0"></Label>
                </StackPanel>
                <Border Width="380" Height="1" CornerRadius="0" BorderThickness="1" Margin="0 5 0 0">
                    <Border.BorderBrush>
                        <LinearGradientBrush SpreadMethod="Repeat" StartPoint="0,5" EndPoint="5,0" MappingMode="Absolute">
                            <LinearGradientBrush.GradientStops>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="Black" Offset="0.2"/>
                                <GradientStop Color="Transparent" Offset="0.4"/>
                                <GradientStop Color="Transparent" Offset="0.6"/>
                                <GradientStop Color="Black" Offset="0.8"/>
                                <GradientStop Color="Black" Offset="1"/>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                </Border>
            </WrapPanel>
        </Grid>
    </Paragraph>
</FlowDocument>
View Code

代碼:Dispatcher.Invoke能夠防止報錯:調用線程必須爲 STA,由於許多 UI 組件都須要。ide

public void PrintIt()
{
WaybillData waybill = new WaybillData();//TODO:本身構造數據
System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action<WaybillData>(PrintAction), waybill);
}
public void PrintAction(WaybillData t)
        {
            PrintDialog pdlg = new PrintDialog();
            FlowDocument doc = PrintPreviewWindow.LoadDocumentAndRender("JDFlowDocument.xaml", t);
            Dispatcher.Invoke(new DoPrintMethod(DoPrint), DispatcherPriority.ApplicationIdle, pdlg, ((IDocumentPaginatorSource)doc).DocumentPaginator);
        }
 private delegate void DoPrintMethod(PrintDialog pdlg, DocumentPaginator paginator);
        private void DoPrint(PrintDialog pdlg, DocumentPaginator paginator)
        {
            pdlg.PrintDocument(paginator, "JDFlowDocument");
        }
相關文章
相關標籤/搜索