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

发布时间 2023-07-27 15:47:46作者: 张不胖

首先,参考了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         }

 

测试成功!!!!!!!