vs2022 提示 不能再 sender, e 申明
时间: 2024-04-09 07:32:51 浏览: 25
非常抱歉,我之前提供的代码有误。以下是更新后的代码示例,使用不同的方法签名来处理PrintPage事件:
```csharp
using System;
using System.Drawing;
using System.Drawing.Printing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace PrintPdfExample
{
class Program
{
static void Main(string[] args)
{
// 加载PDF文档
PdfDocument pdfDoc = new PdfDocument();
pdfDoc.LoadFromFile("spire.pdf");
// 创建一个PrintDocument对象
PrintDocument printDoc = new PrintDocument();
// 设置打印文档
printDoc.DocumentName = "spire.pdf";
printDoc.PrinterSettings.PrintFileName = "spire.pdf";
printDoc.PrinterSettings.PrintToFile = true;
printDoc.PrinterSettings.PrinterName = "Your_Printer_Name";
// 设置PrintPage事件处理程序
printDoc.PrintPage += new PrintPageEventHandler(PrintDoc_PrintPage);
// 打印文档
printDoc.Print();
// 释放资源
pdfDoc.Close();
}
private static void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
// 获取要打印的页面内容
PdfDocument pdfDoc = new PdfDocument();
pdfDoc.LoadFromFile("spire.pdf");
PdfPageBase page = pdfDoc.Pages[0];
// 绘制页面内容
using (var graphics = e.Graphics)
{
page.CreateGraphics().DrawPdfPage(graphics, new RectangleF(0, 0, e.PageBounds.Width, e.PageBounds.Height));
}
// 指示是否还有更多页面要打印
e.HasMorePages = false;
// 释放资源
pdfDoc.Dispose();
}
}
}
```
请将示例代码中的"Your_Printer_Name"替换为您要使用的打印机的名称。这段代码会加载PDF文件并使用PrintDocument类将每个页面打印到指定的打印机上。确保在项目中引用了Spire.Pdf和Spire.Pdf.Graphics命名空间。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)