I have a window service the prints certain documents with image using the PrintDocument class. Occasionally, I'm encountering this error upon printing "The data area passed to a system call is too small" and the image on the report was not printed. I'm stuck on finding whats the cause. Here's the code:
public string Print(List<PrintPageInfo> printList, Printer_Settings printerSettings)
{
string result = string.Empty;
try
{
List<PrintPageInfo> sortedPrintList = printList.OrderBy(p => p.SequenceOrder).ThenBy(x => x.SubSequenceOrder).ToList();
lock (_printLocker)
{
foreach (var item in sortedPrintList)
{
streams = item.Streams;
if (streams == null || streams.Count == 0)
throw new Exception("No stream to print.");
using (var printDoc = new PrintDocument())
{
printDoc.PrinterSettings.PrinterName = printerSettings.PrinterName;
//if (printDoc.PrinterSettings.IsValid) throw new Exception("Printer name is invalid.");
PrintController printController = new StandardPrintController();
printDoc.PrintController = printController;
printDoc.DefaultPageSettings.Color = true;
printDoc.DefaultPageSettings.Landscape = false;
printDoc.DocumentName = item.PageName;
Margins margins = new Margins(0, 0, 0, 0);
printDoc.DefaultPageSettings.Margins = margins;
if (!printDoc.PrinterSettings.IsValid) throw new Exception("Cannot find the default printer.");
foreach (PaperSize papers in printDoc.PrinterSettings.PaperSizes)
{
if (papers.PaperName == "A4")
{
printDoc.DefaultPageSettings.PaperSize = papers;
break;
}
}
if (item.PageName == "MCCheck")
{
var hasCheckTray = false;
foreach (PaperSource paperSource in printDoc.PrinterSettings.PaperSources)
{
if (paperSource.SourceName == printerSettings.CheckVoucherTray)
{
printDoc.DefaultPageSettings.PaperSource = paperSource;
hasCheckTray = true;
break;
}
}
if (!hasCheckTray) throw new Exception(string.Format("Unable to find printer tray for check voucher."));
}
else if (item.PageName == "Invoice")
{
var hasInvoiceTray = false;
foreach (PaperSource paperSource in printDoc.PrinterSettings.PaperSources)
{
if (paperSource.SourceName == printerSettings.ExcessInvoiceTray)
{
printDoc.DefaultPageSettings.PaperSource = paperSource;
hasInvoiceTray = true;
break;
}
}
if (!hasInvoiceTray) throw new Exception(string.Format("Unable to find printer tray for invoice."));
}
else if (item.PageName == "BIR2307")
{
var hasBIRTray = false;
foreach (PaperSource paperSource in printDoc.PrinterSettings.PaperSources)
{
if (paperSource.SourceName == printerSettings.BIR2307Tray)
{
printDoc.DefaultPageSettings.PaperSource = paperSource;
hasBIRTray = true;
break;
}
}
if (!hasBIRTray) throw new Exception(string.Format("Unable to find printer tray for BIR2307."));
}
else if (item.PageName == "SignOff")
{
var hasSignOffTray = false;
foreach (PaperSource paperSource in printDoc.PrinterSettings.PaperSources)
{
if (paperSource.SourceName == printerSettings.SignOffSheetTray)
{
printDoc.DefaultPageSettings.PaperSource = paperSource;
hasSignOffTray = true;
break;
}
}
if (!hasSignOffTray) throw new Exception(string.Format("Unable to find printer tray for sign off sheet."));
}
if (item.PageName == "BIR2307")
printDoc.PrinterSettings.Copies = (short)4;
//else if (item.PageName == "SignOff")
// printDoc.PrinterSettings.Copies = (short)2;
else
printDoc.PrinterSettings.Copies = (short)1;
//currentPageIndex = streams.Count - 1;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
currentPageIndex = 0;
printDoc.Print();
Thread.Sleep(700);
}
}
}
}
catch (Exception ex)
{
Logger.Error(ex.Message, ex);
return ex.Message;
}
finally
{
Dispose();
}
return result;
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(streams[currentPageIndex]);
// Adjust rectangular area with printer margins.
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
// Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect);
// Prepare for the next page. Make sure we haven't hit the end.
//currentPageIndex--;
//ev.HasMorePages = currentPageIndex >= 0; //(m_currentPageIndex < m_streams.Count);
currentPageIndex++;
ev.HasMorePages = (currentPageIndex < streams.Count);
}
Here's the stacktrace:
February 22, 2018 11:56:49,395 AM (1709529445ms)
ERROR [CWS_PrintService.Classes.PrintThread] - The data area passed to a system call is too small - Thread: 6 line: 0 System.ComponentModel.Win32Exception (0x80004005): The data area passed to a system call is too small at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at CWS_PrintService.Classes.PrintThread.Print(List`1 printList, Printer_Settings printerSettings)
Any help would be appreciated!