
文档打印 是桌面应用、后台服务以及服务器端系统中非常常见的需求。在实际开发和业务场景中,开发者往往需要在无用户交互的情况下完成打印操作,例如:静默打印文件、将打印任务发送到指定打印机,或通过代码精细控制打印行为。
本文将介绍如何使用 Spire.Printing,在 Windows、Linux 和 macOS 平台上,通过 C# 实现 PDF 与 Office 文档的自动化打印。你将了解如何构建可打印的文档流、以代码方式选择打印机,并配置高级打印参数,从而在现代 .NET 应用中实现稳定、可控的跨平台打印方案。
目录
安装 Spire.Printing
Spire.Printing 以 NuGet 包的形式发布,可通过标准方式添加到项目中:
Install-Package Spire.Printing
平台兼容性说明
Spire.Printing 是一款面向现代 .NET 应用的 跨平台打印库。当与支持 .NET Standard 的 Spire.Office 系列组件配合使用时,可在 无需依赖 Microsoft Office Interop 的情况下,在 Windows、Linux 和 macOS 上打印 Word、Excel、PowerPoint、PDF 等文档。
目前支持的 .NET 运行时包括:
- .NET 5.0
- .NET 6.0
- .NET 9.0
- .NET 10.0
支持的平台环境包括:
- Windows(x64、x86)
- Linux(x64、ARM)
- macOS(x64、ARM)
核心打印流程与打印设置
Spire.Printing 的核心设计思想是:将“可直接打印的文档流”发送到打印机。在不同操作系统下,可打印流的格式略有不同:Windows 下使用 XPS 文档流;Linux / macOS 下通常使用 PDF 文档流。
在实际项目中,Spire.Printing 通常与 Spire.Office for .NETStandard 配合使用,形成统一的跨平台打印流程。
基本打印流程
整体步骤如下:
- 从文档创建一个 IPrintDocumentStream 实例
- 创建 PrintDocument 对象
- 通过 PrintSettings 配置打印参数
- 将打印任务发送到打印机
示例代码
using Spire.Printing;
IPrintDocumentStream documentStream;
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(
System.Runtime.InteropServices.OSPlatform.Windows))
{
// Windows 平台使用 XPS
documentStream = new XpsPrintDocument("test.xps");
}
else
{
// Linux / macOS 使用 PDF
documentStream = new PdfPrintDocument("test.pdf");
}
// 创建 PrintDocument
PrintDocument printDocument = new PrintDocument(documentStream);
// 设置纸张大小
printDocument.PrintSettings.PaperSize = PaperSize.A4;
// 设置打印份数
printDocument.PrintSettings.Copies = 2;
// 选择打印页码范围
printDocument.PrintSettings.SelectPageRange(2, 5);
// 双面打印
if (printDocument.PrintSettings.CanDuplex)
{
printDocument.PrintSettings.Duplex = Duplex.Vertical;
}
// 是否逐份打印
printDocument.PrintSettings.Collate = true;
// 指定打印机(不设置则使用默认打印机)
printDocument.PrintSettings.PrinterName = "Your Printer Name";
// 输出为文件
printDocument.PrintSettings.PrintToFile("toXps.xps");
// 记录打印日志
printDocument.PrintSettings.PrintLogger =
new DefaultPrintLogger("log.txt");
// 执行打印
printDocument.Print();
// 释放资源
printDocument.Dispose();
这种基于“文档流”的设计,使打印流程在不同平台上保持一致,同时所有打印行为都可以通过 PrintSettings API 进行精细控制。
打印 Word、Excel、PowerPoint、PDF 等文档
在实际项目中,Spire.Printing 通常与对应的 Spire.Office 文档组件配合使用,包括 Spire.Doc、Spire.XLS、Spire.Presentation、Spire.PDF。这些组件负责加载原始文档,并将其保存为 PDF 或 XPS 的文档流,再交由 Spire.Printing 发送至打印机。

在 C# 中打印 Word 文档
安装库
Install-Package Spire.Printing
Install-Package Spire.Docfor.NETStandard
示例代码
using Spire.Doc;
using Spire.Printing;
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
using (Document document = new Document())
{
document.LoadFromFile("test.docx");
var fileFormat = !isWindows
? Spire.Doc.FileFormat.PDF
: Spire.Doc.FileFormat.XPS;
MemoryStream stream = new MemoryStream();
document.SaveToStream(stream, fileFormat);
IPrintDocumentStream docStream = !isWindows
? new PdfPrintDocument(stream)
: new XpsPrintDocument(stream);
PrintDocument printDoc = new PrintDocument(docStream);
printDoc.PrintSettings.SelectPageRange(1, 1);
printDoc.Print();
printDoc.Dispose();
}
在 C# 中打印 Excel 文件
安装库
Install-Package Spire.Printing
Install-Package Spire.XLSfor.NETStandard
示例代码
using Spire.Xls;
using Spire.Printing;
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
using (Workbook workbook = new Workbook())
{
workbook.LoadFromFile("test.xlsx");
var fileFormat = !isWindows
? Spire.Xls.FileFormat.PDF
: Spire.Xls.FileFormat.XPS;
MemoryStream stream = new MemoryStream();
workbook.SaveToStream(stream, fileFormat);
IPrintDocumentStream xlsStream = !isWindows
? new PdfPrintDocument(stream)
: new XpsPrintDocument(stream);
PrintDocument printXls = new PrintDocument(xlsStream);
printXls.PrintSettings.SelectPageRange(1, 1);
printXls.Print();
printXls.Dispose();
}
在 C# 中打印 PDF 文件
安装库
Install-Package Spire.Printing
Install-Package Spire.PDFfor.NETStandard
示例代码
using Spire.Pdf;
using Spire.Printing;
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
using (PdfDocument pdfDocument = new PdfDocument())
{
pdfDocument.LoadFromFile("test.pdf");
var fileFormat = !isWindows
? Spire.Pdf.FileFormat.PDF
: Spire.Pdf.FileFormat.XPS;
MemoryStream stream = new MemoryStream();
pdfDocument.SaveToStream(stream, fileFormat);
IPrintDocumentStream pdfStream = !isWindows
? new PdfPrintDocument(stream)
: new XpsPrintDocument(stream);
PrintDocument printPdf = new PrintDocument(pdfStream);
printPdf.PrintSettings.SelectPageRange(1, 1);
printPdf.Print();
printPdf.Dispose();
}
在 C# 中打印 PowerPoint 演示文稿
安装库
Install-Package Spire.Printing
Install-Package Spire.Presentationfor.NETStandard
示例代码
using Spire.Presentation;
using Spire.Printing;
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
using (Presentation presentation = new Presentation())
{
presentation.LoadFromFile("test.pptx");
var fileFormat = !isWindows
? Spire.Presentation.FileFormat.PDF
: Spire.Presentation.FileFormat.XPS;
MemoryStream stream = new MemoryStream();
presentation.SaveToFile(stream, fileFormat);
IPrintDocumentStream pptStream = !isWindows
? new PdfPrintDocument(stream)
: new XpsPrintDocument(stream);
PrintDocument printPpt = new PrintDocument(pptStream);
printPpt.PrintSettings.SelectPageRange(1, 1);
printPpt.Print();
printPpt.Dispose();
}
高级打印设置与操作
在自动化打印或跨平台打印场景中,Spire.Printing 通过 PrintSettings 提供了对打印机、纸张和页面输出的更精细控制,非常适合无人值守服务和批量打印任务。
枚举并选择打印机
IEnumerable<string> printers = printDocument.PrintSettings.Printers;
// 根据业务逻辑选择打印机
string selectedPrinter = printers.First();
printDocument.PrintSettings.PrinterName = selectedPrinter;
该方式适用于多打印机环境,或需要明确指定打印目标的场景。
选择打印机支持的纸张大小
IEnumerable<PaperSize> paperSizes =
printDocument.PrintSettings.PaperSizes;
PaperSize selectedSize = paperSizes.First();
printDocument.PrintSettings.PaperSize = selectedSize;
通过这种方式可确保所选纸张尺寸与目标打印机兼容。
选择指定页进行打印
// 连续页码
printDocument.PrintSettings.SelectPageRange(2, 5);
// 指定页码
int[] pages = { 1, 3, 5, 7 };
printDocument.PrintSettings.SelectSomePages(pages);
注意:同一次打印任务中只能使用其中一种方式。
这些高级设置使得打印输出具备高度可控性,特别适合自动化处理、多文档批量打印以及对打印结果一致性要求较高的业务场景。
授权说明
在未授权的情况下,Spire.Printing 仅允许打印前 10 页内容。 通过为 Spire.Office for .NET 或对应的文档组件(如 Spire.Doc、Spire.XLS、Spire.PDF、Spire.Presentation)配置许可证,可移除该限制。
Spire.Pdf.License.LicenseProvider.SetLicenseKey(key);
Spire.Doc.License.LicenseProvider.SetLicenseKey(key);
Spire.Xls.License.LicenseProvider.SetLicenseKey(key);
Spire.Presentation.License.LicenseProvider.SetLicenseKey(key);
有关许可证配置的详细说明,请参考:授权应用指南。
总结
Spire.Printing 为 C# 应用提供了一套灵活、可靠的专业打印解决方案。它支持在 Windows、Linux 和 macOS 上,以文档流方式打印 PDF、Word、Excel 和 PowerPoint 文件,并可与 Spire.Office for .NET(尤其是 .NET Standard 版本)无缝配合使用。
在掌握基础打印流程后,开发者可以根据实际业务需求,灵活应用 打印机选择、纸张设置、页码控制等高级功能,轻松构建稳定的自动化打印系统。
在评估或短期开发阶段,也可以申请临时许可证,以在开发过程中解除试用限制。







