开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Core 6.0 中,你可以使用免费开源的库 PdfiumViewer 来打印 PDF 并指定打印机。以下是一个示例代码: 首先,确保已将 PdfiumViewer 包添加到你的项目中。你可以在项目文件(.csproj)中添加以下 NuGet 引用: ```xml <ItemGroup> <PackageReference Include="PdfiumViewer" Version="2.17.0" /> </ItemGroup> ``` 接下来,使用以下代码来打印 PDF 流并指定打印机: ```csharp using PdfiumViewer; using System.Drawing.Printing; using System.IO; public void PrintPdfWithSpecificPrinter(Stream pdfStream, string printerName) { using (PdfDocument pdfDocument = PdfDocument.Load(pdfStream)) { PrintDocument printDocument = new PrintDocument(); printDocument.PrinterSettings.PrinterName = printerName; printDocument.PrintPage += (sender, e) => { using (PdfRenderer pdfRenderer = new PdfRenderer(pdfDocument)) { var pageImage = pdfRenderer.RenderPageToBitmap(e.PageSettings.PrinterSettings.FromPage - 1); e.Graphics.DrawImage(pageImage, e.PageBounds); } }; printDocument.Print(); } } ``` 在上述代码中,我们使用了 PdfiumViewer 库来加载 PDF 流,并使用指定的打印机打印。`PrintPdfWithSpecificPrinter` 方法接受一个 PDF 流对象 `pdfStream` 和一个打印机名称 `printerName`。 调用示例: ```csharp using (FileStream fs = File.OpenRead("path_to_your_pdf_file.pdf")) { PrintPdfWithSpecificPrinter(fs, "your_printer_name"); } ``` 请确保将 `path_to_your_pdf_file.pdf` 替换为实际的 PDF 文件路径,以及将 `your_printer_name` 替换为实际的打印机名称。 希望这个示例对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值