.net core 调用打印机打印pdf文件 FreeSpire.PDF

首先,参考了https://stackoverflow.com/questions/63941980/is-it-possible-to-print-documents-from-a-net-core-3-1-windows-service中的说明,

声明:我用的是core 3.1测试的

先下载了FreeSpire.PDF的NuGet包,可以看出来简介上是支持print的:

 然后我的测试直接在controller中:

复制代码
 1 [HttpPost("[action]")]
 2         public async Task<IActionResult> PrintPDF()
 3         {
 4             bool printedOK = true;
 5             string printErrorMessage = "";
 6             try
 7             {
 8                 PdfDocument pdf = new PdfDocument("E:/Project/Testzq.pdf");//pdf file complete path
 9                 pdf.PrintSettings.PrinterName = "NPIAA6432 (HP LaserJet MFP M132snw)";//printer complete name
10                 pdf.PrintSettings.DocumentName = "testDocumentName";//display name
11                 pdf.PrintSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 8, 11);//The size of the paper, in hundredths of an inch.
12                 pdf.PrintSettings.SetPaperMargins(2, 2, 2, 2);
13                 pdf.PrintSettings.SelectSinglePageLayout(Spire.Pdf.Print.PdfSinglePageScalingMode.FitSize, true);
14                 //_logger.LogDebug($"Paper Size - Width:{pdf.PrintSettings.PaperSize.Width} Height:{pdf.PrintSettings.PaperSize.Height} Name:{pdf.PrintSettings.PaperSize.PaperName} Kind:{pdf.PrintSettings.PaperSize.Kind} RawKind:{pdf.PrintSettings.PaperSize.RawKind}");
15                 pdf.Print();//execute print 
16             }
17             catch (Exception ex)
18             {
19                 printErrorMessage = "Printing Error: " + ex.ToString();
20                 printedOK = false;
21             }
22 
23             return null;
24         }
复制代码

 

测试成功!!!!!!!

posted on   张不胖  阅读(1655)  评论(1编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示