打印c#时,传递给系统调用的数据区域太小

时间:2018-02-27 10:02:30

标签: c# .net rdlc printdocument localreport

我有一个窗口服务,使用PrintDocument类打印带图像的某些文档。有时,我在打印时遇到此错误"传递给系统调用的数据区域太小"并且没有打印报告上的图像。我一直坚持找到原因。这是代码:

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);
        }

这是堆栈跟踪:

2018年2月22日11:56:49,395 AM(1709529445ms)

  

错误[CWS_PrintService.Classes.PrintThread] - 传递给系统调用的数据区域太小 - 线程:6行:0   System.ComponentModel.Win32Exception(0x80004005):传递给系统调用的数据区域太小      在System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document,PrintEventArgs e)      在System.Drawing.Printing.PrintController.Print(PrintDocument文档)      在System.Drawing.Printing.PrintDocument.Print()      在CWS_PrintService.Classes.PrintThread.Print(List`1 printList,Printer_Settings printerSettings)

任何帮助将不胜感激!

0 个答案:

没有答案