在WPF中使用PrintDialog.PrintDocument方法進行打印


使用PrintDialog.PrintDocument方法進行打印需要提前布局流文檔,流文檔不能直接在編輯器中顯示,可以考慮在用戶控件或者窗口中進行布局后,將代碼拷貝進流文檔中。

對流文檔進行數據賦值時,可以考慮在流文檔中預定義TextBlock文本,對其進行命名。之后在渲染時,通過遍歷源數據中的屬性名,在流文檔中查找相應的TextBlock文本,找到后對其進行賦值。

打印參數設置等思路可以參考:TSC TTP-244Pro標簽打印機打印步驟小結

1、流文檔示例

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              FontFamily="宋體">
    <FlowDocument.Resources>
        <Style x:Key="LineStyle" TargetType="{x:Type Line}">
            <Setter Property="Stroke" Value="Black"/>
            <Setter Property="StrokeThickness" Value="1"/>
        </Style>
    </FlowDocument.Resources>

    <Paragraph>
        <Border>
            <Canvas Name="MainCanvas" Width="200" Height="100" Background="White">
                <Line Style="{StaticResource LineStyle}" X1="3" Y1="3" X2="194" Y2="3"/>
                <Line Style="{StaticResource LineStyle}" X1="194" Y1="3" X2="194" Y2="96"/>
                <Line Style="{StaticResource LineStyle}" X1="3" Y1="96" X2="194" Y2="96"/>
                <Line Style="{StaticResource LineStyle}" X1="3" Y1="3" X2="3" Y2="96"/>

                <Grid Canvas.Left="5" Canvas.Top="5" Width="190" Height="90">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*"/>
                        <RowDefinition Height="1*"/>
                        <RowDefinition Height="1*"/>
                        <RowDefinition Height="1*"/>
                        <RowDefinition Height="2"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Text="123456有限公司"
                       TextAlignment="Center" FontSize="14"
                       HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
                    <StackPanel Grid.Row="1" Orientation="Horizontal">
                        <TextBlock Text="商品批號:" FontSize="12" Margin="5,0,0,0"
                           HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
                        <Line Style="{StaticResource LineStyle}" X1="-5" Y1="21" X2="113" Y2="21"/>
                    </StackPanel>
                    <StackPanel Grid.Row="2" Orientation="Horizontal">
                        <TextBlock Text="物料編號 :" FontSize="12" Margin="5,0,0,0"
                           HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
                        <Line Style="{StaticResource LineStyle}" X1="-5" Y1="21" X2="113" Y2="21"/>
                    </StackPanel>
                    <StackPanel Grid.Row="3" Orientation="Horizontal">
                        <TextBlock Text="訂 單 號 :" FontSize="12" Margin="5,0,0,0"
                           HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
                        <Line Style="{StaticResource LineStyle}" X1="-5" Y1="21" X2="113" Y2="21"/>
                    </StackPanel>
                </Grid>

                <!-- 商品批號 -->
                <TextBlock Name="BatchNumber" Canvas.Left="75" Canvas.Top="34" FontSize="12" Text="商品批號"/>
                <!-- 物料編號 -->
                <TextBlock Name="MaterialNumber" Canvas.Left="75" Canvas.Top="56" FontSize="12" Text="物料編號"/>
                <!-- 訂單號 -->
                <TextBlock Name="OrderNumber" Canvas.Left="75" Canvas.Top="78" FontSize="12" Text="訂單號"/>
            </Canvas>
        </Border>
    </Paragraph>
</FlowDocument>

2、打印實體類

public class PrintModel
{
    /// <summary>
    /// 批號
    /// </summary>
    public string BatchNumber { get; set; }

    /// <summary>
    /// 訂單號
    /// </summary>
    public string OrderNumber { get; set; }

    /// <summary>
    /// 物料代碼
    /// </summary>
    public string MaterialNumber { get; set; }
}

3、渲染類

public interface IDocumentRenderer
{
    void Render(FlowDocument doc, object data);
}

public class CommonDocumentRenderer : IDocumentRenderer
{
    public void Render(FlowDocument doc, object data)
    {
        var model = data as PrintModel;
        if (model == null)
        {
            throw new ArgumentException("data is not PrintModel");
        }

        var type = typeof(PrintModel);
        var properties = type.GetProperties();
        foreach (var property in properties)
        {
            //文本賦值
            if (doc.FindName(property.Name) is TextBlock textBlock)
            {
                textBlock.Text = property.GetValue(model)?.ToString();
            }
        }
    }
}

4、打印代碼示例

public void Print(FlowDocument document, object data, string printer, int copyCount)
{
    var renderer = new CommonDocumentRenderer();
    renderer.Render(document, data);
    Print(document, printer, entity.Description, copyCount);
}

/// <summary>
/// 打印
/// </summary>
/// <param name="document">流文檔</param>
/// <param name="printer">打印機名稱</param>
/// <param name="description">打印描述</param>
/// <param name="copyCount">打印個數</param>
public static void Print(FlowDocument document, string printer, string description, int copyCount)
{
    var localPrintServer = new LocalPrintServer();
    var printQueue = localPrintServer.GetPrintQueue(printer);
    if (printQueue.IsInError)
    {
        throw new Exception("打印機處於錯誤狀態");
    }

    var printDialog = new PrintDialog
    {
        PrintQueue = printQueue, //打印隊列
        PrintTicket = {CopyCount = copyCount} //打印個數
    };

    //設置紙張大小
    var pageWidth = (int) Math.Ceiling(printDialog.PrintableAreaWidth); //小標簽:114
    var pageHeight = (int) Math.Ceiling(printDialog.PrintableAreaHeight); //小標簽:227
    printDialog.PrintTicket.PageMediaSize = new PageMediaSize(pageWidth, pageHeight);

    //設置紙張邊距
    var paperSize = GetPaperSize(printer); //小標簽:118*246
    var offsetX = (int) Math.Ceiling((paperSize.Width - pageWidth) / 2f);
    var offsetY = (int) Math.Ceiling((paperSize.Height - pageHeight) / 2f);
    document.PagePadding = new Thickness(offsetX, offsetY, offsetX, offsetY);

    //打印
    var paginator = ((IDocumentPaginatorSource) document).DocumentPaginator;
    printDialog.PrintDocument(paginator, description);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM