C# 客户端PDF文件打印方法大全

news/2025/3/16 1:06:38/

前言

最近开发客户端的时候,用户提出了要增加打印PDF文件的需求,于是我各种翻官网和CSDN,然而好像还没有人总结得特别全,于是自己记录一下这几天的研究吧。

目前发现的有以下四种方式调用打印服务:

  1. 使用创建新进程的方式打印
  2. 使用WPF命名空间下的打印接口打印
  3. 使用WinForm命名空间下的打印接口打印
  4. 使用第三方PDF组件打印

使用创建新进程的方式打印

参考代码

using System.Diagnostics;namespace Process打印
{class Program{static void Main(string[] args){Process process = new Process();bool canPrint = false;string[] verbs = null;process.StartInfo.FileName = @"D:\test1.pdf";verbs = process.StartInfo.Verbs;for (int i = 0; i < verbs.Length; i++){if (verbs[i] == "Print"){canPrint = true;}}if (canPrint){process.StartInfo.UseShellExecute = true;process.StartInfo.CreateNoWindow = true;process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;process.StartInfo.Verb = "Print";process.Start();}}}
}

缺点说明:除了使用"Printto"操作可以指定打印机外,不能指定其他参数*(实际上可以程序传入一些参数,但没有更多资料无法继续研究);不能弹出打印机设置界面;以PDF文件为例,需要设置“对”默认打开程序,如Adobe Acrobat 或者Adobe Reader 是可以调用打印服务的,但如果是其他阅读器或者浏览器则会报错没有对应进程与之关联。

StartInfo的类说明文档:https://learn.microsoft.com/zh-cn/dotnet/api/system.diagnostics.processstartinfo?view=net-7.0

使用WPF命名空间下的打印接口打印

参考代码

<Window x:Class="WPFPrintTest.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:WPFPrintTest"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Button Content="点击展示打印设置窗口" HorizontalAlignment="Left" Height="56" Margin="21,24,0,0" VerticalAlignment="Top" Width="145" Click="InvokePrint"/><ComboBox Name ="PrinterList" HorizontalAlignment="Left" Height="25" Margin="276,175,0,0" VerticalAlignment="Top" Width="158" SelectionChanged="PrinterList_SelectionChanged"/><Button Content="点击使用上方设置进行打印" HorizontalAlignment="Left" Margin="276,216,0,0" VerticalAlignment="Top" Width="158" Height="56" Click="Button_Click"/></Grid>
</Window>
using System;
using System.Collections;
using System.IO;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;namespace WPFPrintTest
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{object selectedItem = null;static PrintServer printServer = new PrintServer();PrintQueueCollection printqueen = printServer.GetPrintQueues();public MainWindow(){InitializeComponent();ShowAllPrinter();}public void ShowAllPrinter(){ArrayList currentPrinterList = new ArrayList();foreach (PrintQueue pq in printqueen){currentPrinterList.Add(pq.Name);}PrinterList.ItemsSource = currentPrinterList;PrintQueue defaultPrinter = LocalPrintServer.GetDefaultPrintQueue();PrinterList.Text = defaultPrinter.Name;}private void InvokePrint(object sender, RoutedEventArgs e){// Create the print dialog object and set optionsPrintDialog pDialog = new PrintDialog();pDialog.PageRangeSelection = PageRangeSelection.AllPages;pDialog.UserPageRangeEnabled = true;// Display the dialog. This returns true if the user presses the Print button.Nullable<Boolean> print = pDialog.ShowDialog();if (print == true){XpsDocument xpsDocument = new XpsDocument(@"d:\测试XPS文档.xps", FileAccess.ReadWrite);FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Test print job");}}private void PrinterList_SelectionChanged(object sender, SelectionChangedEventArgs e){selectedItem = PrinterList.SelectedItem;}private void Button_Click(object sender, RoutedEventArgs e){PrintQueue printQueue = printServer.GetPrintQueue(selectedItem.ToString());PrintTicket printTicket = printQueue.UserPrintTicket;printTicket.CopyCount = 2;PrintCapabilities printCapabilities = printQueue.GetPrintCapabilities();System.Printing.ValidationResult result = printQueue.MergeAndValidatePrintTicket(printQueue.UserPrintTicket, printTicket);PrintSystemJobInfo Info = printQueue.AddJob("TestJob");var stream = Info.JobStream;var data = File.ReadAllBytes(@"d:\测试XPS文档.xps");stream.Write(data, 0, data.Length);stream.Close();}}
}

此名称空间下的打印自带打印设置界面
缺点说明:只接受XPS文件类型(或者文本文件流);

参考链接:https://learn.microsoft.com/zh-cn/dotnet/api/system.printing?view=windowsdesktop-7.0

使用WinForm命名空间下的打印接口打印

参考代码:

using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;namespace WinForm的打印
{class Program{private  Font printFont;private  StreamReader streamToPrint;private  PrintDocument pd = new PrintDocument();static private Image newImage ;// Create point for upper-left corner of image.static private PointF ulCorner ;static void Main(string[] args){Program pg = new Program();pg.printFont = new Font("Arial", 10);//完全的静默打印//支持设置参数的静默打印Console.WriteLine("输入1进入完全静默打印;输入2进入设置参数的静默打印");string inputCommand = Console.ReadLine();if (inputCommand == "1"){ PurePrintSilense(pg); }else if (inputCommand == "2"){ PrintSilenceWithCommandLine(pg); }else{ Console.WriteLine("输入错误!按任意键退出程序");Console.ReadLine(); }}private static void PrintSilenceWithCommandLine(Program pg){Console.WriteLine("请输入要打印的文件全路径");string printFilePath = Console.ReadLine();pg.streamToPrint = new StreamReader(printFilePath);Console.WriteLine("以下当前电脑所有打印机名称,请输入将要设置打印的机器序号:");ArrayList currentPrinterList = pg.PopulateInstalledPrintersCombo();for (int i = 0; i < currentPrinterList.Count; i++){int num = i + 1;Console.WriteLine("打印机序号是:"+ num +",打印机名称是:"+ currentPrinterList[i]);}int selecePrinter = Convert.ToInt32(Console.ReadLine());pg.comboInstalledPrinters_SelectionChanged(pg, currentPrinterList[selecePrinter-1].ToString(),pg.pd);Console.WriteLine("请输入要打印的份数");int printCopies = Convert.ToInt32(Console.ReadLine());pg.setprintCopies(pg, printCopies,pg.pd);pg.print(pg,pg.pd);}private void print(Program pg, PrintDocument pd){try{try{pd.PrintPage += new PrintPageEventHandler(pg.pd_PrintPage);//去掉正在打印第几页的页面pd.PrintController = new System.Drawing.Printing.StandardPrintController();pd.Print();}finally{pg.streamToPrint.Close();}}catch (Exception ex){Console.WriteLine(ex.Message);}}private void setprintCopies(Program pg, int printCopies ,PrintDocument pd){pd.PrinterSettings.Copies = (short)printCopies;//throw new NotImplementedException();}private ArrayList PopulateInstalledPrintersCombo(){// Add list of installed printers found to the combo box.// The pkInstalledPrinters string will be used to provide the display string.String pkInstalledPrinters;ArrayList currentPrinterList = new ArrayList();for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++){pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];currentPrinterList.Add(PrinterSettings.InstalledPrinters[i]);}return currentPrinterList;}private void comboInstalledPrinters_SelectionChanged(Program pg ,string printerName, PrintDocument pd){pd.PrinterSettings.PrinterName = printerName;}private static void PurePrintSilense(Program pg){//throw new NotImplementedException();Console.WriteLine("请输入要打印的文件全路径");string printFilePath = Console.ReadLine();//newImage = Image.FromFile(printFilePath);// Create point for upper-left corner of image.//ulCorner = new PointF(100.0F, 100.0F);pg.streamToPrint = new StreamReader(printFilePath);using (FileStream stream = new FileStream(printFilePath, FileMode.Open))pg.streamToPrint = new StreamReader(stream);pg.print(pg,pg.pd);}private void pd_PrintPage(object sender, PrintPageEventArgs ev){float linesPerPage = 0;float yPos = 0;int count = 0;float leftMargin = ev.MarginBounds.Left;float topMargin = ev.MarginBounds.Top;string line = null;// Calculate the number of lines per page.linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);// Print each line of the file.while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null)){yPos = topMargin + (count *printFont.GetHeight(ev.Graphics));ev.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black,leftMargin, yPos, new StringFormat());count++;}ev.Graphics.DrawImage(newImage, ulCorner);// If more lines exist, print another page.if (line != null)ev.HasMorePages = true;elseev.HasMorePages = false;}}
}

缺点说明:只能打印纯文本文件或者image文件;
参考链接:https://learn.microsoft.com/zh-cn/dotnet/api/system.drawing.printing?view=dotnet-plat-ext-7.0

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G

使用第三方PDF组件打印

参考代码(以使用spire.pdf为例)

using Spire.Pdf;
using System.Drawing.Printing;namespace 引入其他组件的打印
{class Program{static void Main(string[] args){//pdftron.PDFNet.Initialize();//测试spire.pdfPdfDocument pdf = new PdfDocument();pdf.LoadFromFile(@"d:\测试.pdf");pdf.PrintSettings.PrinterName = "Microsoft XPS Document Writer";pdf.PrintSettings.PrintToFile(@"d:\测试.xps");pdf.Print();}}
}

可以看出前面三种方法在真正的实际应用中都有点难受,所以我查资料的时候看到很多人都会使用第三方组件去实现打印,但是组件不开源(自己找破解版不算),需要收费且价格不算低,下面给出我找的几个组件信息。
试用描述


http://www.ppmy.cn/news/789095.html

相关文章

HTML打印pdf

将html打印出来 npm npm i print-js 在打印的页面中引入 import print from print-js 点击调用函数 Batchprinting() {const style page { } media print { table {margin-bottom: 15px;};print({printable: pdfDomALL,type: html,style: style,scanStyles: false})} …

扫描型PDF进行反色打印

对于扫描出来的图片型pdf&#xff0c;白花花的白底黑字很难受的&#xff0c;怎么办呢&#xff1f; Acrobat 11.0.10 ①设置“打印机“为Adobe PDF ②点击"高级" ③复合灰度 ④勾选”负片" ⑤ 缺点是反色打印后&#xff0c;ocr文字层就不见了 而且acrobat不…

js实现简单pdf打印功能

js实现pdf打印功能的主要思想是&#xff0c;需要一个pdf模板图片作为背景图&#xff0c;然后采用拼内容的方式&#xff0c;将文字拼接到页面中&#xff0c;打印页面内容即可&#xff0c;需要注意的是pdf 背景尺寸。 一、Html中的打印按钮&#xff0c;可以是a标签也可以是其他标…

SpringBlade、若依框架和人人开源框架对比

SpringBlade、若依框架和人人开源框架都是基于Java的开源框架&#xff0c;用于快速构建企业级应用程序。下面是它们之间的简要对比&#xff1a; 1、SpringBlade&#xff1a; SpringBlade是一个基于 Spring Cloud 和 Spring Boot 的微服务快速开发框架。 它提供了一套完整的开…

C#打印PDF

C#打印PDF文件的方式有如下几种&#xff1a; 第一种&#xff1a;新建打印进程&#xff0c;利用C#封装的打印方法直接打印。 缺点&#xff1a;会启动Adobe reader 修复&#xff1a; 第二种&#xff1a;引用第三方dll进行打印 O2S.Components.PDFView4NET.dll O2S.Components.P…

jspdf打印、pdf打印

先安装 然后在你的组件直接调用this.getPdf() 就可以打印 具体安装什么 请看代码 import就是要安装的 安装好之后千万别忘了再main.js也引入哦 import html2Canvas from html2canvas import JsPDF from jspdfconst A4pageH 841.89 const A4pageW 595.28export default {inst…

故障:PDF 文件打印失败

故障现象 客户在安装一台新的复合机后&#xff0c;能够打印 Word 等 Office 文件&#xff0c;但不能打印 PDF 文件 。 故障处理 在 Adobe Acrobat Reader 打印界面&#xff0c;不要勾选【按照 PDF 页面大小选择纸张来源】。

java之pdf打印

前言 网上搜了一堆如何把打印的纸张横向&#xff0c;发现颇为花费时间&#xff0c;特再次记录一下 解决办法 Document pdfDoc new Document(PageSize.A4.rotate());说明&#xff1a;加上 rotate()函数即可 教程 知识点&#xff1a; 1、新建 完整pdf文档 依赖&#xff1…