我们有一个.NET 4.0 / WPF应用程序需要打印可能很大的文档(超过5000页)。我们目前开发的解决方案在Windows 7上运行得非常好,但在Windows 8.1上运行的完全相同的应用程序在1500-2000页的“内存不足”异常时失败。
在Windows 7上:
在Windows 8.1上,运行完全相同的代码:
无论我们选择哪种打印机,Windows 8.1上的行为都是相同的(通常我们使用Adobe PDF驱动程序或Primo PDF,但我们也使用各种风格的“真正”打印机。)
我们需要在代码中考虑Windows 8.1下WPF打印的已知差异吗?
以下代码示例粗略地封装了我们的进程(尽管每个页面的DrawingVisual是不同的) - 实际上,我们每页创建一个DrawingVisual并将每个都写入VisualsToXpsDocument实例。
在我的测试中,此示例在Windows 7上运行完成,但在Windows 8.1上大约2000页失败:
static void Main(string[] args)
{
LocalPrintServer server = new LocalPrintServer();
PrintQueueCollection queueCollection = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
var printQueue = queueCollection.FirstOrDefault(q => q.Name == "PrimoPDF");
// set the job name so print to file (i.e. pdf) has a default name to use
printQueue.CurrentJobSettings.Description = "Testing 1 2 3";
var validationResult = printQueue.MergeAndValidatePrintTicket(printQueue.UserPrintTicket, printQueue.DefaultPrintTicket);
var printTicket = validationResult.ValidatedPrintTicket;
var writer = PrintQueue.CreateXpsDocumentWriter(printQueue);
var visualsToXpsDocumentCollator = writer.CreateVisualsCollator();
var visual = GetVisual();
visualsToXpsDocumentCollator.BeginBatchWrite();
for (int i = 0; i < 5000; ++i)
{
visualsToXpsDocumentCollator.Write(visual, printTicket);
Console.WriteLine("Printed page " + i);
}
visualsToXpsDocumentCollator.EndBatchWrite();
}
private static DrawingVisual GetVisual()
{
DrawingVisual visual = new DrawingVisual();
using (DrawingContext dc = visual.RenderOpen())
{
// Create formatted text--in a particular font at a particular size
FormattedText ft = new FormattedText(
@"Lorem ipsum dolor sit amet, mei at elitr legimus apeirian, ea sit bonorum alienum neglegentur, docendi minimum adolescens mei te. Ex alii euismod argumentum mel, voluptua ponderum euripidis cum id, alii efficiendi appellantur per et. Prompta nusquam ea mea. Putent labores id vim, eos ullum vivendum contentiones cu. In eos eius facilisis comprehensam. Percipit lucilius repudiare vis in, id per singulis omittantur. Qui vocent vivendo ad. Mollis vidisse habemus pri ne. Est voluptaria referrentur an, vivendum indoctum vel an. Mea vitae referrentur cu. Ridens quidam fuisset et nam, lorem vocibus ut duo, natum prodesset in vix. Vix no sonet consequuntur, per inermis omittam referrentur in. Posse voluptatum has et. Quando lucilius eam in, ex his sint consulatu ullamcorper. Id mel utroque postulant. No has regione facilisi deserunt. Te quidam postulant mea. Usu in primis semper. Utamur probatus ad est. Quo at quis eripuit, sed assum putent id. Te probatus omittantur necessitatibus quo, at vel probo facer efficiendi, tota denique interesset vix et. Ei sit hinc argumentum, saperet dolorem habemus sit no. Ea essent aliquip postulant est, ad oratio evertitur duo. Scripta scriptorem ea est. Nec ut duis complectitur. Ut eos vide doming, nam id iudicabit dissentias, eos duis putent no. Tempor pertinacia referrentur eum cu, meis harum homero eum id. An eius repudiare eum, quo assum lucilius ad. Consulatu molestiae ius ei, te elit assum erroribus usu, an his choro quodsi. In nam unum fabellas, mei ad deseruisse moderatius intellegam. Id utroque ancillae nec, mea sale mollis perfecto te. Eu vis dicant delenit convenire, magna mandamus urbanitas ius ea. Ne dolor prompta mentitum sit. Id veritus consequat sit. Sententiae incorrupte et has, quod affert voluptua pri ut. Vel enim meis ancillae et, est te vidisse malorum. Nec facilis commune ut, ut est impetus convenire. An cum tollit suscipit accommodare, ius eu labore placerat, mollis contentiones ei eam. Novum mazim id cum, dolores molestiae quo ei. Ex eos tollit consequat, sed ei epicuri perfecto. Eum at nihil clita albucius, propriae prodesset eu vel, id pri eruditi debitis appareat.",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily("Century"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
24, Brushes.Black);
ft.MaxTextWidth = 750;
// Draw the text at a location
dc.DrawText(ft, new Point(10.0, 10.0));
}
return visual;
}
对于我们可能想要查找或更改的内容,我们将不胜感激。