简体   繁体   English

计算打印页数的有效方法?

[英]Efficient way to count number of pages to print?

TLDR How can one efficiently determine the amount of pages that have to be printed? TLDR如何有效地确定必须打印的页面数量?

** Just a note: I marked klugerama's answer as the answer to my question. **只是一个注释:我将klugerama的答案标记为我的问题的答案。 I'm still open for other suggestions though! 我仍然愿意接受其他建议!

Currently, I have an application in production which prints invoices. 目前,我在生产中有一个打印发票的应用程序。 The printing itself isn't a problem: customer information, invoice details etc are printed as they should be. 打印本身不是问题:客户信息,发票详细信息等应按原样打印。 However, users are complaining about the time it takes my software to send the print job to the printer. 但是,用户抱怨我的软件将打印作业发送到打印机所花费的时间。

At the moment, I have implemented a rather inefficient way to print. 目前,我已经实施了一种相当低效的打印方式。 In order to know how many pages I have to print, I do a 'dry run' of the print job and cancel it before it gets sent to the printer. 为了知道我需要打印多少页,我会对打印作业进行“干运行”并在将其发送到打印机之前将其取消。 Then I count the number of pages the print job contains and add that information to the print job that actually does get sent to the printer. 然后,我计算打印作业包含的页数,并将该信息添加到实际发送到打印机的打印作业。 To do this, I 'abuse' the PrintDocument.PrintPage method. 为此,我'滥用' PrintDocument.PrintPage方法。 After doing so, I can print "Page 1/2 - Page 2/2" etc. 这样做之后,我可以打印“Page 1/2 - Page 2/2”等。

I'm currently thinking about handling this in a different way. 我目前正在考虑以不同的方式处理这个问题。 I know which margins I have in a page, like the header and the footer. 我知道我在页面中有哪些边距,比如页眉和页脚。 Ergo, the space between header and footer is the available space to print article information (name, price etc). 因此,页眉和页脚之间的空间是打印文章信息(名称,价格等)的可用空间。 If I calculate the height of the string (using Graphics.MeasureString ), I can tell when the articles take up more space on a page that's available for printing. 如果我计算字符串的高度(使用Graphics.MeasureString ),我可以判断文章在可用于打印的页面上占用更多空间。

Previously, printing could take up to a minute before actually getting sent to the printer (taken into account the clients which run the software aren't the fastest around). 以前,在实际发送到打印机之前打印可能需要一分钟(考虑到运行软件的客户端并不是最快的)。 I have implemented this method, and printing now takes about 5-10 seconds. 我已经实现了这种方法,现在打印大约需要5-10秒。 However, I'm wondering if this is the way to go. 但是,我想知道这是否可行。 I can't imagine I'm the only one dealing with this issue; 我无法想象我是唯一一个处理这个问题的人; how do you deal with this? 你怎么处理这个?

I'm also open to completely different approaches. 我也对完全不同的方法持开放态度。

What you are doing sounds like the way to go. 你在做什么听起来像是要走的路。 But... 但...

You could instead output to XML and use XSLT to style the output, which would handle pagination for you. 您可以改为输出到XML并使用XSLT来设置输出样式,这将为您处理分页。 Note that this would likely require a pretty substantial refactoring effort, so depending on your situation it's likely not feasible, but it's an option. 请注意,这可能需要相当大的重构努力,因此根据您的情况,它可能不可行,但它是一种选择。

It would also provide some potential flexibility down the road, as it provides a more modular approach. 它还将提供一些潜在的灵活性,因为它提供了更加模块化的方法。 This would allow you to more easily provide other output options, like HTML & PDF. 这将允许您更轻松地提供其他输出选项,如HTML和PDF。 It may also make "print preview" easier. 它还可以使“打印预览”更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM