没有发出OnEndPrint的startdocprinter调用

时间:2016-08-24 16:27:22

标签: c# printing

我有一个正确打印的C#Windows应用程序。我通过移动一些东西做了一些改变。我在开发环境中进行了一些单元测试。编译并安装在win 7 pc上。我现在从编译版本中收到以下错误。我做了一些谷歌搜索,所有我能找到的是一些XP问题。在这里搜索并发现一些VBA问题,这些都不适用于我。如果需要,我可以发布一些代码。有点难过了。

************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): A StartDocPrinter call was not issued
   at System.Drawing.Printing.StandardPrintController.OnEndPrint(PrintDocument document, PrintEventArgs e)
   at System.Windows.Forms.PrintControllerWithStatusDialog.OnEndPrint(PrintDocument document, PrintEventArgs e)
   at System.Drawing.Printing.PrintController.Print(PrintDocument document)
   at System.Drawing.Printing.PrintDocument.Print()
   at TPG.Printing.PrintPickTicket(String OrdNum, Boolean blnReprint, Boolean blnSwapOrder)
   at TPG.frmPrintTicket.btnReprint_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我的代码

public void PrintPickTicket(string OrdNum, bool blnReprint, bool blnSwapOrder)
{
    UserBusiness userBiz = new UserBusiness();
    OrderBusiness OrderBus = new OrderBusiness();
    UserPrinterSetting setting = userBiz.GetUserPrinterSettings(SessionHelper.UserData.UserID, MachineName);
    PrintDocument pd = new PrintDocument();

    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    pd.PrinterSettings.PrinterName = setting.DocumentPrinter;  
    pd.DefaultPageSettings.Landscape = true;
    blnDetailRecs = false;
    blnNotesRecs = false;
    blnSwapRecs = false;
    blnIsReprint = blnReprint;
    blnIsSwapOrder = blnSwapOrder;

    //
    //Get the data needed to print a pick ticket
    //

    //Pick Ticket Header, Merchant Info, 
    dsOrders = OrderBus.GetPickTicketInfo(OrdNum);
    dsMerchDetails = OrderBus.GetMerchantInfo(OrdNum);
    dsOrderDetails = OrderBus.GetPickDetailsInfo(OrdNum);
    dsComments = OrderBus.GetLineItemComments(OrdNum);
    dsShipComments = OrderBus.GetShippingComments(OrdNum);
    dsSwap = OrderBus.GetSwapOrder(OrdNum.TrimEnd() + 'S');

    if (dsSwap.Tables[0].Rows.Count != 0)
    {
        blnIsSwapOrder = true;
    }

    //Initialize printing vars
    sopNumber = OrdNum.TrimEnd();
    sStatus = "Detail";
    pgCnt = 1;
    rCnt = 0;
    rSCnt = 0;
    rCCnt = 0;

    //Print the doggone thing already
    pd.Print();

    //Clean up your crap dammit
    dsShipComments.Dispose();

    if (blnIsSwapOrder)
    {
        dsSwap.Dispose();
    }

    dsOrderDetails.Dispose();
    dsMerchDetails.Dispose();
    dsOrders.Dispose();
    dsComments.Dispose();
    dsShipComments.Dispose();
}

private static string GetNumbers(string input)
{
    return new string(input.Where(c => char.IsDigit(c)).ToArray());
}

private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    Brush blkBrush = Brushes.Black;
    Brush gryBrush = Brushes.LightGray;
    Pen gryPen = new Pen(Color.DarkGray);
    Font hdrFont = new Font("Arial Black", 10, System.Drawing.GraphicsUnit.Point);
    Font hdrsmFont = new Font("Arial Black", 7, System.Drawing.GraphicsUnit.Point);
    Font dtlFont = new Font("Arial", 10, System.Drawing.GraphicsUnit.Point);
    Font ttlFont = new Font("Arial Black", 12, System.Drawing.GraphicsUnit.Point);
    Font bcFont = new Font("Free 3 of 9 Extended", 24, GraphicsUnit.Point);
    Font bcSmallFont = new Font("Free 3 of 9 Extended", 16, GraphicsUnit.Point);
    int LinesPerPage = 20;
    int intLineCnt = 0;
    string lineComment = "";
    string printComment = "";
    string rmaNumber = "";

    g.PageUnit = GraphicsUnit.Millimeter;

    System.DateTime now = System.DateTime.Now;
    string theDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    Bitmap lgoImage = new Bitmap(theDirectory + @"\Images\phxLetr.png");

    //
    //
    // Print Page Heading
    //
    //
    g.DrawImage(lgoImage, 5, 5);
    g.DrawString("Page ", dtlFont, blkBrush, 194, 7);
    g.DrawString(pgCnt.ToString(), dtlFont, blkBrush, 206, 7);
    g.DrawString("Enter Date:", dtlFont, blkBrush, 190, 11);
    g.DrawString("Print Date:", dtlFont, blkBrush, 190, 15);
    g.DrawString(dsOrders.Tables[0].Rows[0]["EntDate"].ToString(), dtlFont, blkBrush, 208, 11);
    g.DrawString(now.ToString("g"), dtlFont, blkBrush, 208, 15);

    if (sStatus == "Detail" || sStatus == "Swap")
    {
        g.DrawString("PICK TICKET", ttlFont, blkBrush, 47, 7);

        if (blnIsSwapOrder)
        {
            g.DrawString("*SWAP*", ttlFont, blkBrush, 85, 7);
        }

        g.DrawString("Picked By:    _____________", hdrFont, blkBrush, 60, 15);
        g.DrawString("Inspect By:   _____________", hdrFont, blkBrush, 60, 25);
        g.DrawString("Observe By:  _____________", hdrFont, blkBrush, 120, 15);
        g.DrawString("Deploy By:    _____________", hdrFont, blkBrush, 120, 25);
    }
    else if (sStatus == "Notes")
    {
        g.DrawString("DEPLOYMENT INFO", ttlFont, blkBrush, 47, 7);
    }

    if (sStatus != "Swap")
    {
        g.DrawString('*' + sopNumber + '*', bcFont, blkBrush, 207, 20);
        g.DrawString(sopNumber, dtlFont, blkBrush, 215, 27);
    }
    else
    {
        g.DrawString('*' + sopNumber + 'S' + '*', bcFont, blkBrush, 207, 20);
        g.DrawString(sopNumber + "S", dtlFont, blkBrush, 215, 27);
    }

    if (blnIsReprint)
    {
        g.DrawString("****  REPRINT  ****", dtlFont, blkBrush, 215, 7);
    }

    //
    //Bill To Box
    //
    g.DrawRectangle(gryPen, 3, 43, 85, 23);  //Bill To box
    g.DrawString("Bill To:", hdrFont, blkBrush, 3, 38);
    g.DrawString(dsOrders.Tables[0].Rows[0]["CustomerID"].ToString().TrimEnd(), dtlFont, blkBrush, 20, 39);
    g.DrawString(dsOrders.Tables[0].Rows[0]["CustomerName"].ToString().Substring(0, 31), dtlFont, blkBrush, 5, 45);
    g.DrawString(dsOrders.Tables[0].Rows[0]["CustomerAddress1"].ToString(), dtlFont, blkBrush, 5, 50);
    g.DrawString(dsOrders.Tables[0].Rows[0]["CustomerAddress2"].ToString(), dtlFont, blkBrush, 5, 55);
    g.DrawString(dsOrders.Tables[0].Rows[0]["CustomerCity"].ToString().TrimEnd() + ", " + 
                dsOrders.Tables[0].Rows[0]["CustomerState"].ToString().TrimEnd() + " " + 
                dsOrders.Tables[0].Rows[0]["CustomerZip"].ToString().TrimEnd(), dtlFont, blkBrush, 5, 60);

    //
    //Ship To Box
    //
    g.DrawRectangle(gryPen, 92, 43, 85, 23);  //Ship To box
    g.DrawString("Ship To:", hdrFont, blkBrush, 92, 38);

    if (sStatus == "Swap")
    {
        g.DrawString(dsOrders.Tables[0].Rows[0]["SwapShipToName"].ToString().Substring(0, 31), dtlFont, blkBrush, 94, 45);
        g.DrawString(dsOrders.Tables[0].Rows[0]["SwapShippingAddress1"].ToString(), dtlFont, blkBrush, 94, 50);
        g.DrawString(dsOrders.Tables[0].Rows[0]["SwapShippingAddress2"].ToString(), dtlFont, blkBrush, 94, 55);
        g.DrawString(dsOrders.Tables[0].Rows[0]["SwapShippingCity"].ToString().TrimEnd() + ", " +
                    dsOrders.Tables[0].Rows[0]["SwapShippingState"].ToString().TrimEnd() + " " +
                    dsOrders.Tables[0].Rows[0]["SwapShippingZip"].ToString().TrimEnd(), dtlFont, blkBrush, 94, 60);
    }
    else
    {
        g.DrawString(dsOrders.Tables[0].Rows[0]["ShipToName"].ToString().Substring(0, 31), dtlFont, blkBrush, 94, 45);

        if (!blnIsSwapOrder)
        {
            g.DrawString(dsOrders.Tables[0].Rows[0]["ShippingAddress1"].ToString(), dtlFont, blkBrush, 94, 50);
            g.DrawString(dsOrders.Tables[0].Rows[0]["ShippingAddress2"].ToString(), dtlFont, blkBrush, 94, 55);
            g.DrawString(dsOrders.Tables[0].Rows[0]["ShippingCity"].ToString().TrimEnd() + ", " +
                        dsOrders.Tables[0].Rows[0]["ShippingState"].ToString().TrimEnd() + " " +
                        dsOrders.Tables[0].Rows[0]["ShippingZip"].ToString().TrimEnd(), dtlFont, blkBrush, 94, 60);
        }
    }

    //
    //Merch Info box
    //
    g.DrawRectangle(gryPen, 185, 33, 83, 33);  
    g.DrawString("M", hdrsmFont, blkBrush, 181, 35);
    g.DrawString("E", hdrsmFont, blkBrush, 181, 38);
    g.DrawString("R", hdrsmFont, blkBrush, 181, 41);
    g.DrawString("C", hdrsmFont, blkBrush, 181, 44);
    g.DrawString("H", hdrsmFont, blkBrush, 181, 47);
    g.DrawString("I", hdrsmFont, blkBrush, 181, 53);
    g.DrawString("N", hdrsmFont, blkBrush, 181, 56);
    g.DrawString("F", hdrsmFont, blkBrush, 181, 59);
    g.DrawString("O", hdrsmFont, blkBrush, 181, 62);

    if (dsMerchDetails.Tables[0].Rows.Count > 0)
    {
        g.DrawString(dsMerchDetails.Tables[0].Rows[0]["MID"].ToString(), dtlFont, blkBrush, 187, 35);
        g.DrawString(dsMerchDetails.Tables[0].Rows[0]["CustName"].ToString().Substring(0, 30), dtlFont, blkBrush, 187, 40);
        g.DrawString(dsMerchDetails.Tables[0].Rows[0]["Address1"].ToString(), dtlFont, blkBrush, 187, 45);
        g.DrawString(dsMerchDetails.Tables[0].Rows[0]["Address2"].ToString(), dtlFont, blkBrush, 187, 50);
        g.DrawString(dsMerchDetails.Tables[0].Rows[0]["City"].ToString().TrimEnd() + ", " +
                    dsMerchDetails.Tables[0].Rows[0]["State"].ToString().TrimEnd() + " " +
                    dsMerchDetails.Tables[0].Rows[0]["Zip"].ToString().TrimEnd(), dtlFont, blkBrush, 187, 55);
        g.DrawString(dsMerchDetails.Tables[0].Rows[0]["Phone"].ToString(), dtlFont, blkBrush, 187, 60);
    }

    //
    //Purch Order, Ship Mthd, etc.  box
    //
    g.DrawRectangle(gryPen, 3, 68, 265, 18); 
    g.DrawString("Purchase Order", hdrFont,blkBrush, 5, 70);
    g.DrawString("Shipping Method", hdrFont, blkBrush, 46, 70);
    g.DrawString("Shipper Number", hdrFont, blkBrush, 90, 70);
    g.DrawString("Ship Notes: ", hdrFont, blkBrush, 133, 70);
    g.DrawString(dsOrders.Tables[0].Rows[0]["CStponbr"].ToString().TrimEnd(), dtlFont, blkBrush, 5, 75);
    g.DrawString(dsOrders.Tables[0].Rows[0]["ShippingMethod"].ToString(), dtlFont, blkBrush, 47, 75); 
    g.DrawString(dsOrders.Tables[0].Rows[0]["ShipperNumber"].ToString().TrimEnd(), dtlFont, blkBrush, 94, 75);

    //if (ShipNum.Length > 23)
    //{
    //    ShipNum = ShipNum.Substring(0, 24);
    //}

    if (dsShipComments.Tables[0].Rows.Count > 0) 
    {
        g.DrawString(dsShipComments.Tables[0].Rows[0]["Comment_1"].ToString(), dtlFont, blkBrush, 161, 70);
        g.DrawString(dsShipComments.Tables[0].Rows[0]["Comment_2"].ToString(), dtlFont, blkBrush, 161, 74);
        g.DrawString(dsShipComments.Tables[0].Rows[0]["Comment_3"].ToString(), dtlFont, blkBrush, 161, 78);
        g.DrawString(dsShipComments.Tables[0].Rows[0]["Comment_4"].ToString(), dtlFont, blkBrush, 161, 82);
    }

    //
    //Detail Header and Data
    //
    if (sStatus == "Detail" || sStatus == "Swap")
    {                
        g.DrawString("Item Num", hdrFont, blkBrush, 3, 90);
        g.DrawString("Desc", hdrFont, blkBrush, 70, 90);
        g.DrawString("Inv", hdrFont, blkBrush, 190, 86); 
        g.DrawString("Bin", hdrFont, blkBrush, 190, 90);
        g.DrawString("Pick", hdrFont, blkBrush, 219, 86);
        g.DrawString("Qty", hdrFont, blkBrush, 219, 90);
        g.DrawString("UOM", hdrFont, blkBrush, 234, 90);
        g.DrawString("QC", hdrFont, blkBrush, 248, 90);
    }
    else if (sStatus == "Notes")
    {
        g.DrawString("Qty", hdrFont, blkBrush, 3, 90);
        g.DrawString("Item Num", hdrFont, blkBrush, 13, 90);
        g.DrawString("Deployment Info", hdrFont, blkBrush, 80, 90);
    }

    //
    //Details Data
    //
    int ypos = 95;
    int count = 0;
    Boolean blnShadow = false;

    if (sStatus == "Detail")
    {
        while (count < LinesPerPage && rCnt < dsOrderDetails.Tables[0].Rows.Count)
        {
            if (blnShadow)
            {
                g.FillRectangle(gryBrush, 3, ypos, 265, 5);
            }

            g.DrawString(dsOrderDetails.Tables[0].Rows[rCnt]["ITEMNMBR"].ToString(), dtlFont, blkBrush, 3, ypos);
            g.DrawString(dsOrderDetails.Tables[0].Rows[rCnt]["ITEMDESC"].ToString().Substring(0, 60), dtlFont, blkBrush, 70, ypos);
            g.DrawString(dsOrderDetails.Tables[0].Rows[rCnt]["LOCNCODE"].ToString(), dtlFont, blkBrush, 190, ypos);
            g.DrawString(string.Format("{0:0}", (dsOrderDetails.Tables[0].Rows[rCnt]["QUANTITY"])), dtlFont, blkBrush, 218, ypos);
            g.DrawString(dsOrderDetails.Tables[0].Rows[rCnt]["UOFM"].ToString(), dtlFont, blkBrush, 234, ypos);                   
            ypos = ypos + 5;
            rCnt ++;
            intLineCnt ++;
            count ++;
            blnShadow = !blnShadow;
        }

        if (rCnt <= dsOrderDetails.Tables[0].Rows.Count && intLineCnt >= LinesPerPage) 
        {
            e.HasMorePages = true;
            pgCnt ++;
            intLineCnt = 0;
            ypos = 95;
            blnDetailRecs = true;
        }
        else
        {
            e.HasMorePages = true;
            pgCnt ++;
            blnDetailRecs = false;
        }
    }

    //
    //Deployment page details
    //
    if (sStatus == "Notes")
    {
        ypos = 95;
        count = 0;
        blnShadow = false;

        if (dsComments.Tables[0].Rows.Count == 0)
        {
            g.DrawString("****  NO INFORMATION  ****", dtlFont, blkBrush, 70, ypos);
        }

        while (count < LinesPerPage && rCCnt < dsComments.Tables[0].Rows.Count)
        {
            if (blnShadow)
            {
                g.FillRectangle(gryBrush, 3, ypos, 265, 5);
            }

            lineComment = dsComments.Tables[0].Rows[rCCnt]["cmtText"].ToString().TrimEnd();
            g.DrawString(string.Format("{0:0}", (dsComments.Tables[0].Rows[rCCnt]["QUANTITY"])), dtlFont, blkBrush, 3, ypos);
            g.DrawString(dsComments.Tables[0].Rows[rCCnt]["ITEMNMBR"].ToString(), dtlFont, blkBrush, 13, ypos);

            if (dsComments.Tables[0].Rows[rCCnt]["ITEMNMBR"].ToString().StartsWith("CALL TAG"))
            {
                rmaNumber = GetNumbers(dsComments.Tables[0].Rows[rCCnt]["cmtText"].ToString().TrimEnd());
                rmaNumber = "RMA" + rmaNumber.PadLeft(12, '0');
                g.DrawString(rmaNumber, dtlFont, blkBrush, 80, ypos);
                g.DrawString("*" + rmaNumber + "*", bcSmallFont, blkBrush, 130, ypos);
                ypos = ypos + 5;
                blnShadow = !blnShadow;
                intLineCnt++;
                count++;
            }
            else
            {
                //if the comment is really long then wrap that rascal
                if (Math.Ceiling(Convert.ToDouble(lineComment.Length) / 90) > 1)
                {
                    if (count + (Math.Ceiling(Convert.ToDouble(lineComment.Length) / 90)) > LinesPerPage)
                    {
                        //Force new page before printing
                        count = LinesPerPage;
                    }
                    else
                    {
                        while (lineComment.Length > 0)
                        {
                            if (lineComment.Length > 90)
                            {
                                printComment = lineComment.Substring(1, 90);
                                lineComment = lineComment.Substring(91);
                            }
                            else
                            {
                                printComment = lineComment;
                                lineComment = "";
                            }

                            if (blnShadow)
                            {
                                g.FillRectangle(gryBrush, 3, ypos, 265, 5);
                            }

                            g.DrawString(printComment, dtlFont, blkBrush, 80, ypos);
                            ypos = ypos + 5;
                            blnShadow = !blnShadow;
                            intLineCnt++;
                            count++;
                        }
                    }
                }
                else
                {
                    g.DrawString(lineComment, dtlFont, blkBrush, 80, ypos);
                    intLineCnt++;
                    count++;
                    ypos = ypos + 5;
                    blnShadow = !blnShadow;
                }
            }

            rCCnt = rCCnt + 1;                    
        }

        if (rCCnt <= dsComments.Tables[0].Rows.Count && intLineCnt >= LinesPerPage)
        {
            e.HasMorePages = true;
            pgCnt++;
            intLineCnt = 0;
            ypos = 95;
            blnNotesRecs = true;
        }
        else
        {
            if (blnIsSwapOrder)
            {
                e.HasMorePages = true;
                blnNotesRecs = false;
                pgCnt++;
            }
            else
            {
                e.HasMorePages = false;
                blnNotesRecs = false;
            }
        }
    }

    //
    //Swap page details
    //
    if (sStatus == "Swap")
    {
        ypos = 95;
        count = 0;
        blnShadow = false;

        while (count < LinesPerPage && rSCnt < dsSwap.Tables[0].Rows.Count)
        {
            if (blnShadow)
            {
                g.FillRectangle(gryBrush, 3, ypos, 265, 5);
            }

            g.DrawString(dsSwap.Tables[0].Rows[rSCnt]["ITEMNMBR"].ToString(), dtlFont, blkBrush, 3, ypos);

            if (dsSwap.Tables[0].Rows[rSCnt]["ITEMNMBR"].ToString().StartsWith("CALL TAG"))
            {
                rmaNumber = GetNumbers(dsSwap.Tables[0].Rows[rSCnt]["ITEMDESC"].ToString().TrimEnd());
                rmaNumber = "RMA" + rmaNumber.PadLeft(12, '0');
                g.DrawString(rmaNumber, dtlFont, blkBrush, 70, ypos);
                g.DrawString("*" + rmaNumber + "*", bcSmallFont, blkBrush, 120, ypos);
            }
            else
            {
                g.DrawString(dsSwap.Tables[0].Rows[rSCnt]["ITEMDESC"].ToString(), dtlFont, blkBrush, 70, ypos);
            }

            g.DrawString(dsSwap.Tables[0].Rows[rSCnt]["LOCNCODE"].ToString(), dtlFont, blkBrush, 190, ypos);
            g.DrawString(string.Format("{0:0}", dsSwap.Tables[0].Rows[rSCnt]["QUANTITY"]), dtlFont, blkBrush, 218, ypos); 
            g.DrawString(dsSwap.Tables[0].Rows[rSCnt]["UOFM"].ToString(), dtlFont, blkBrush, 234, ypos);                    
            ypos = ypos + 5;
            rSCnt++;
            intLineCnt++;
            count++;
            blnShadow = !blnShadow;
        }

        if (rSCnt <= dsSwap.Tables[0].Rows.Count && intLineCnt >= LinesPerPage)
        {
            e.HasMorePages = true;
            pgCnt++;
            intLineCnt = 0;
            ypos = 95;
            blnSwapRecs = true;
        }
        else
        {
            e.HasMorePages = false;
            pgCnt++;
            blnSwapRecs = false;
        }
    }

    switch (sStatus)
    {
        case "Detail":
            if (!blnDetailRecs)
                {sStatus = "Notes";}
                break;
        case "Notes":
            if (!blnNotesRecs)
                {sStatus = "Swap";}
                break;
        case "Swap":
            if (!blnSwapRecs)
                {sStatus = "Detail";}
                break;
    }

1 个答案:

答案 0 :(得分:1)

我发现了问题。我以管理员身份运行了应用程序,但我遇到了一个不同的错误。该错误表示打印位图有问题。它无法找到位图。显然我们的安装程序不包括相关的图像文件夹。