参考:https://www.cnblogs.com/guogangj/archive/2013/02/27/2934733.html
模板:JDFlowDocument.xaml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<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>
代码:Dispatcher.Invoke可以防止报错:调用线程必须为 STA,因为许多 UI 组件都需要。
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"); }