Sh.Common.PrintForWPF 1.0.0

dotnet add package Sh.Common.PrintForWPF --version 1.0.0
NuGet\Install-Package Sh.Common.PrintForWPF -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sh.Common.PrintForWPF" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sh.Common.PrintForWPF --version 1.0.0
#r "nuget: Sh.Common.PrintForWPF, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sh.Common.PrintForWPF as a Cake Addin
#addin nuget:?package=Sh.Common.PrintForWPF&version=1.0.0

// Install Sh.Common.PrintForWPF as a Cake Tool
#tool nuget:?package=Sh.Common.PrintForWPF&version=1.0.0

使用文档


测试项目 使用.net core 3.1 wpf 测试 使用发发

<Window x:Class="Sh.Common.PrintForWPF.UnitTest.MainWindow"
    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:Sh.Common.PrintForWPF.UnitTest"
    mc:Ignorable="d"
       Title="MainWindow" Height="550" Width="800">
<Window.Resources>
    <ResourceDictionary>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="20"></Setter>
            <Setter Property="FontSize" Value="20"></Setter>
            <Setter Property="Background" Value="AntiqueWhite"></Setter>
        </Style>
        <Style   TargetType="TextBox" >
            <Setter Property="Margin" Value="20"></Setter>
            <Setter Property="FontSize" Value="20"></Setter>
            <Setter Property="Background" Value="AntiqueWhite"></Setter>
        </Style>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100"></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ScrollViewer Grid.Row="0"  Grid.ColumnSpan="2" Grid.Column="0">
        <TextBox TextWrapping="Wrap"    x:Name="lbl_PrintName" >无</TextBox>

    </ScrollViewer>
    <Button x:Name="btn_PrintName" Grid.Row="1"  Click="btn_PrintName_Click">获取默认打印机</Button>
    <Button x:Name="btn_PrintNamees" Grid.Row="1" Grid.Column="2" Click="btn_PrintNamees_Click">所有打印设备</Button>

    <StackPanel  Grid.Row="2" >
        <TextBox x:Name="txt_name" Margin="5" Height="25"></TextBox>
        <Button  Margin="10"  x:Name="btn_PrintSetDefualt" Click="btn_PrintSetDefualt_Click">设置设置默认打印设备</Button>
    </StackPanel>
    <StackPanel  Grid.Row="2"  Grid.Column="1">
        <TextBox x:Name="txt_printQueueName" Margin="5" Height="25"></TextBox>
        <Button Margin="10" x:Name="btn_GetJobs" Click="btn_GetJobs_Click">获取打印Job</Button>
    </StackPanel>
    <StackPanel  Grid.Row="3"  Grid.Column="0">
        <TextBox x:Name="txt_PrintStatusName" Margin="5" Height="25" ></TextBox>
        <Button Margin="10" x:Name="btn_PrintStatus" Click="btn_PrintStatus_Click">获取打印机状态</Button>
    </StackPanel>
    <StackPanel  Grid.Row="3"  Grid.Column="1">
        <TextBox x:Name="txt_path" Margin="5" Height="25" ></TextBox>
        <Button Margin="10" x:Name="btn_Printting" Click="btn_Printting_Click">打印</Button>
    </StackPanel>

</Grid>
</Window>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace Sh.Common.PrintForWPF.UnitTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void btn_PrintName_Click(object sender, RoutedEventArgs e)
    {
        lbl_PrintName.Text = PrintHelper.GetDefaultPrintName();
    }

    private void btn_PrintNamees_Click(object sender, RoutedEventArgs e)
    {
        lbl_PrintName.Text = string.Join(",", PrintHelper.GetQueueses().Select(t => t.Name));
    }

    private void btn_PrintSetDefualt_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txt_name.Text))
        {
            MessageBox.Show("失败!");
            return;
        }
        var result = PrintHelper.SetDefaultPrint(txt_name.Text.Trim());
        if (result.State == PrintStatusResultEnum.Success)
        {
            MessageBox.Show("成功!");
        }
        else
        {
            MessageBox.Show(result.ErrorMessage.Message);
        }

    }
    /// <summary>
    /// 这个测试 可用同浏览器 打印内容 进行查看
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btn_GetJobs_Click(object sender, RoutedEventArgs e)
    {

        var result = PrintHelper.GetPrintQueueJobs(txt_printQueueName.Text.Trim());
        if (result.State == PrintStatusResultEnum.Error)
        {
            MessageBox.Show(result.ErrorMessage.Message); return;
        }
        StringBuilder builder = new StringBuilder();
        result.Jobs.ToList().ForEach(t => builder.AppendLine($"JobIdentifier: {t.JobIdentifier} ,JobName:{t.JobName} ,JobStatus:{t.JobStatus}"));
        lbl_PrintName.Text = builder.ToString();
    }

    private void btn_PrintStatus_Click(object sender, RoutedEventArgs e)
    {
        var result = PrintHelper.GetPrintQueueState(txt_PrintStatusName.Text);
        if (result.State == PrintStatusResultEnum.Error)
        {
            MessageBox.Show(result.ErrorMessage.Message); return;
        }
        lbl_PrintName.Text = result.PrintQueueStatus.ToString();
    }

    private void btn_Printting_Click(object sender, RoutedEventArgs e)
    {
        var result = PrintHelper.AddPrintJob(txt_path.Text, fastCopy: null);
        if (result.State == PrintStatusResultEnum.Error)
        {
            MessageBox.Show(result.ErrorMessage.Message); return;
        }

        lbl_PrintName.Text = result?.Job?.Name + ":" + result?.Job?.JobIdentifier;

    }
}
}

其他的方法 已经添加注释 请自行尝试

* FreeSpirePrintHelper
* PrintHelper 
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 459 5/3/2020

个人的常用工具