有没有比这更好的方法从 PrintDocument 获取页数?
这是我想到的最好的方法:
public static int GetPageCount( PrintDocument printDocument )
{
printDocument.PrinterSettings.PrintFileName = Path.GetTempFileName();
printDocument.PrinterSettings.PrintToFile = true;
int count = 0;
printDocument.PrintController = new StandardPrintController();
printDocument.PrintPage += (sender, e) => count++;
printDocument.Print();
File.Delete( printDocument.PrinterSettings.PrintFileName );
return count;
}
有更好的方法吗? (这实际上很慢)
This is the best I've come up with:
public static int GetPageCount( PrintDocument printDocument )
{
printDocument.PrinterSettings.PrintFileName = Path.GetTempFileName();
printDocument.PrinterSettings.PrintToFile = true;
int count = 0;
printDocument.PrintController = new StandardPrintController();
printDocument.PrintPage += (sender, e) => count++;
printDocument.Print();
File.Delete( printDocument.PrinterSettings.PrintFileName );
return count;
}
Is there a better way to do this? (This is actually quite slow)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以最终的解决方案是:
So the final solution would be:
将 PrintController 声明为
Printing.PreviewPrintController
。这样,您只能打印到内存,而不是文件。
我在 VB.NET 项目中使用了它,它运行得非常好!
Declare the PrintController as a
Printing.PreviewPrintController
.This way, you're only printing to memory, not to a file.
I use this in a VB.NET project, and it works perfectly!
检查 - http://msdn.microsoft.com /en-us/library/system.drawing.printing.printdocument.querypagesettings.aspx
有一个可以处理的 PrintDocument.QueryPageSettings 事件。如果处理,它将在每个 PrintDocument.PrintPage 事件之前调用。所以你可以在那里放一个计数器来计算页数。这样您就可以避免两次通过(一次将文档打印到文件以计算页数,第二次用于实际作业打印)。
上面的 URL 也有一些计数器的示例代码。
希望这有帮助
Check - http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.querypagesettings.aspx
There is a PrintDocument.QueryPageSettings Event that could be handled. If handled, it is called before each PrintDocument.PrintPage event. So you can put a counter there to count the pages. This way you could avoid a two pass (one pass to print the doc to file for counting the pages and second pass for the actual job printing).
The URL above has some example code for a counter also.
Hope this helps