使用itextsharp在一个文档中生成多个Invoice

时间:2015-04-08 09:22:46

标签: .net c#-4.0 itextsharp

我想使用给定代码生成多个发票和单个发票。当用户在此函数GenInvoice(int clientId, string InvoiceNum, bool SinglePage) SinglePage= true中传递值时,代码将在分页后在同一文档中打印多个发票。

问题 当我在我的函数中发送多个发票号时,此函数生成并且singlepage为true,然后此函数不会在同一文档中生成多个pdf。我将值传递给函数 GenInvoice(1362, "85561,85231",true);

我的代码低于

public void GenInvoice(int clientId, string InvoiceNum, bool SinglePage)
    {


        string[] arg = InvoiceNum.Split(',');
        InOnePdf = SinglePage;
        InvNumber = InvoiceNum;
        if (arg.Length != 0)
        {

            foreach (string s in arg)
            {
                TotalCount = i + 1;
                if (TotalCount == 1)
                {
                    doc = new Document(PageSize.A4, 10, 10, 50, 50);
                    fs = null;
                    table = new PdfPTable(8);
                    //string pdfFilePath = "";
                }

                try
                {
                    SqlConnection connWin = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
                    if (connWin.State == ConnectionState.Closed)
                    {
                        connWin.Open();
                    }

                    string Command = "SELECT COMPANY,ADDRLN1,ADDRLN2,ADDRLN3 FROM config";
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(Command, connWin);
                    DataSet dataSet = new DataSet();
                    dataAdapter.Fill(dataSet);

                    /* Getting Client Information*/

                    string ClientCommand = "SELECT BCLIENT,BCITY,BADDR, BSTPROV + '' +  BZIPPOSTAL AS BSTZIP , 'Attn: ' + CONTACT AS Contact  From client WHERE CLIENTID = " + clientId;

                    SqlDataAdapter dataClientInfo = new SqlDataAdapter(ClientCommand, connWin);
                    DataSet _dstClientInfo = new DataSet();
                    dataClientInfo.Fill(_dstClientInfo);

                    /* END client Information */

                    string InvoiceTotal = "SELECT O.INVNUM,C.CLIENTNUM , CONVERT(varchar(10), O.INVDATE,101)AS INVDATE  ,O.TOTAL  from orde_ O INNER JOIN client C ON C.CLIENTID = O.CLIENTID" +
                                            " INNER JOIN Billing B ON B.INVNUM = O.INVNUM " +
                                             "WHERE O.CLIENTID =" + clientId + "AND O.INVNUM  IN  (" + "'" + InvNumber + "')";
                    //IN''85561','87918''
                    SqlDataAdapter dataInvTotal = new SqlDataAdapter(InvoiceTotal, connWin);

                    DataSet _dstInvTotal = new DataSet();
                    dataInvTotal.Fill(_dstInvTotal);

                    string InvDesc = "SELECT O.ORDERNUM,O.TOTAL,Se.DES_,CONVERT(varchar(10),O.ORDDATE,101)AS ORDDATE, DROPOFF from orde_ O INNER JOIN SERVICE Se ON Se.SERVICEID = O.SERVICEID " +
                                       " WHERE O.CLIENTID =" + clientId + " AND O.INVNUM  IN  (" + "'" + InvNumber + "')";

                    SqlDataAdapter dataInvDesc = new SqlDataAdapter(InvDesc, connWin);

                    DataSet dsDetail = new DataSet();
                    dataInvDesc.Fill(dsDetail);




                    #region variabl
                    string _ClientNum = "";
                    string _invdate = "";
                    decimal _total = 0;
                    string _invNum = "";

                    foreach (DataTable tabledata in _dstInvTotal.Tables)
                    {
                        foreach (DataRow dr in tabledata.Rows)
                        {
                            decimal _totalL = 0;
                            Decimal.TryParse(dr["TOTAL"].ToString(), out _totalL);
                            _total = _total + _totalL;
                            _invdate = dr["INVDATE"].ToString();
                            _ClientNum = dr["CLIENTNUM"].ToString();
                            _invNum = dr["INVNUM"].ToString();
                        }
                    }

                    #endregion




                    string ApplicationDirectroy = ConfigurationSettings.AppSettings["FolderPath"].ToString();
                    string appDirectory = ApplicationDirectroy;
                    //appDirectory = appDirectory + @"\Temp";
                    if (ApplicationDirectroy != string.Empty)
                    {

                        if (!Directory.Exists(ApplicationDirectroy))
                        {
                            Directory.CreateDirectory(ApplicationDirectroy);
                        }

                    }
                    else
                    {
                        Console.WriteLine("Please specify the Folder Path And Folder Name Where PDF Generate  ");
                        return;

                    }

                    Random rr = new Random();
                    string filename = rr.Next(1111, 9999).ToString();
                    pdfFilePath = appDirectory + @"\" + filename + ".pdf";
                    PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
                    doc.Open();

                    if (dataSet.Tables[0].Rows.Count > 0)
                    {


                        PdfPCell _CompanyName = new PdfPCell(new Phrase(dataSet.Tables[0].Rows[0]["COMPANY"].ToString(), FontFactory.GetFont(FontFactory.COURIER_BOLDOBLIQUE, 12)));
                        _CompanyName.Colspan = 4;
                        _CompanyName.Border = 0;

                        table.AddCell(_CompanyName);


                        PdfPCell InvoiceHeader = new PdfPCell(new Phrase("INVOICE  ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 16)));
                        InvoiceHeader.Colspan = 4;
                        InvoiceHeader.Border = 0;
                        table.AddCell(InvoiceHeader);

                        PdfPTable pob = new PdfPTable(1);

                        PdfPCell pob1 = new PdfPCell(new Phrase(dataSet.Tables[0].Rows[0]["ADDRLN1"].ToString(), FontFactory.GetFont(FontFactory.COURIER, 10)));
                        pob1.Colspan = 1;
                        pob1.Border = 0;
                        pob.AddCell(pob1);

                        PdfPCell pob2 = new PdfPCell(new Phrase(dataSet.Tables[0].Rows[0]["ADDRLN2"].ToString(), FontFactory.GetFont(FontFactory.COURIER, 10)));
                        pob2.Colspan = 1;
                        pob2.Border = 0;
                        pob.AddCell(pob2);
                        PdfPCell pob3 = new PdfPCell(new Phrase(dataSet.Tables[0].Rows[0]["ADDRLN3"].ToString(), FontFactory.GetFont(FontFactory.COURIER, 10)));
                        pob3.Colspan = 1;
                        pob3.Border = 0;
                        pob.AddCell(pob3);

                        PdfPCell pob4 = new PdfPCell(pob);
                        pob4.Colspan = 4;
                        pob4.Border = 0;
                        table.AddCell(pob4);
                    }




                    PdfPTable right = new PdfPTable(5);

                    PdfPCell right1 = new PdfPCell(new Phrase("Invoice Number  ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    right1.Colspan = 3;
                    right1.Border = 0;
                    right1.BorderWidthLeft = 1;
                    right1.BorderWidthRight = 1;
                    right1.BorderWidthTop = 1;
                    right1.BorderWidthBottom = 1;
                    //right1.BackgroundColor
                    right.AddCell(right1);

                    PdfPCell right2 = new PdfPCell(new Phrase("Page  ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    right2.Colspan = 2;
                    right2.Border = 1;
                    right2.Border = 0;
                    right2.BorderWidthLeft = 0;
                    right2.BorderWidthRight = 1;
                    right2.BorderWidthTop = 1;
                    right2.BorderWidthBottom = 1;
                    right.AddCell(right2);

                    PdfPCell rightL1 = new PdfPCell(new Phrase(_invNum, FontFactory.GetFont(FontFactory.COURIER, 10)));
                    rightL1.Colspan = 3;
                    rightL1.Border = 1;
                    rightL1.BorderWidthLeft = 1;
                    rightL1.BorderWidthRight = 1;
                    rightL1.BorderWidthTop = 0;
                    rightL1.BorderWidthBottom = 1;
                    right.AddCell(rightL1);

                    PdfPCell rightL2 = new PdfPCell(new Phrase(" 1  ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    rightL2.Colspan = 2;
                    rightL2.Border = 1;
                    rightL2.BorderWidthLeft = 0;
                    rightL2.BorderWidthRight = 1;
                    rightL2.BorderWidthTop = 0;
                    rightL2.BorderWidthBottom = 1;
                    right.AddCell(rightL2);

                    PdfPCell rightL3 = new PdfPCell(new Phrase("CLIENT NUMBER  ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    rightL3.Colspan = 3;
                    rightL3.Border = 0;
                    rightL3.BorderWidthLeft = 1;
                    rightL3.BorderWidthRight = 1;
                    rightL3.BorderWidthTop = 0;
                    rightL3.BorderWidthBottom = 1;
                    right.AddCell(rightL3);

                    PdfPCell rightL4 = new PdfPCell(new Phrase("TERMS  ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    rightL4.Colspan = 2;
                    rightL4.Border = 0;
                    rightL4.BorderWidthLeft = 0;
                    rightL4.BorderWidthRight = 1;
                    rightL4.BorderWidthTop = 0;
                    rightL4.BorderWidthBottom = 1;
                    right.AddCell(rightL4);

                    PdfPCell rightL5 = new PdfPCell(new Phrase(_ClientNum, FontFactory.GetFont(FontFactory.COURIER, 10)));
                    rightL5.Colspan = 3;
                    rightL5.Border = 0;
                    rightL5.BorderWidthLeft = 1;
                    rightL5.BorderWidthRight = 1;
                    rightL5.BorderWidthTop = 0;
                    rightL5.BorderWidthBottom = 0;
                    right.AddCell(rightL5);

                    PdfPCell rightL6 = new PdfPCell(new Phrase("NO RECEIPT  ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    rightL6.Colspan = 2;
                    rightL6.Border = 0;
                    rightL6.BorderWidthLeft = 0;
                    rightL6.BorderWidthRight = 1;
                    rightL6.BorderWidthTop = 0;
                    rightL6.BorderWidthBottom = 0;
                    right.AddCell(rightL6);

                    PdfPCell rightL7 = new PdfPCell(new Phrase("INVOICE DATE ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    rightL7.Colspan = 5;
                    rightL7.Border = 0;
                    rightL7.BorderWidthLeft = 1;
                    rightL7.BorderWidthRight = 1;
                    rightL7.BorderWidthTop = 1;
                    rightL7.BorderWidthBottom = 1;
                    right.AddCell(rightL7);
                    PdfPCell rightL8 = new PdfPCell(new Phrase(_invdate, FontFactory.GetFont(FontFactory.COURIER, 10)));
                    rightL8.Colspan = 5;
                    rightL8.Border = 0;
                    rightL8.BorderWidthLeft = 1;
                    rightL8.BorderWidthRight = 1;
                    rightL8.BorderWidthTop = 0;
                    rightL8.BorderWidthBottom = 1;
                    right.AddCell(rightL8);


                    PdfPCell rightL9 = new PdfPCell(new Phrase("INVOICE TOTAL ", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    rightL9.Colspan = 5;
                    rightL9.Border = 0;
                    rightL9.BorderWidthLeft = 1;
                    rightL9.BorderWidthRight = 1;
                    rightL9.BorderWidthTop = 0;
                    rightL9.BorderWidthBottom = 1;
                    right.AddCell(rightL9);
                    PdfPCell rightL10 = new PdfPCell(new Phrase("$" + _total, FontFactory.GetFont(FontFactory.COURIER, 10)));
                    rightL10.Colspan = 5;
                    rightL10.Border = 0;
                    rightL10.BorderWidthLeft = 1;
                    rightL10.BorderWidthRight = 1;
                    rightL10.BorderWidthTop = 0;
                    rightL10.BorderWidthBottom = 1;
                    right.AddCell(rightL10);




                    PdfPCell pob5 = new PdfPCell(right);
                    pob5.Colspan = 4;
                    pob5.Border = 0;
                    table.AddCell(pob5);


                    PdfPCell _Middetail = new PdfPCell(new Phrase("Invoice Submitted To", FontFactory.GetFont(FontFactory.COURIER, 8)));
                    _Middetail.Colspan = 4;
                    _Middetail.Border = 0;

                    table.AddCell(_Middetail);


                    PdfPCell MidRight = new PdfPCell(new Phrase("", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRight.Colspan = 4;
                    MidRight.Border = 0;
                    table.AddCell(MidRight);



                    PdfPCell MidRight1 = new PdfPCell(new Phrase("", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRight1.Colspan = 4;
                    MidRight1.Border = 0;
                    table.AddCell(MidRight1);

                    PdfPCell pdfSpace = new PdfPCell(new Phrase("", FontFactory.GetFont(FontFactory.COURIER, 10)));
                    pdfSpace.Colspan = 8;
                    pdfSpace.Border = 0;
                    pdfSpace.BorderWidthTop = 0;
                    pdfSpace.BorderWidthBottom = 0;
                    table.AddCell(pdfSpace);

                    PdfPCell MidRighCentre = new PdfPCell(new Phrase("Order", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRighCentre.Colspan = 1;
                    MidRighCentre.Border = 0;
                    MidRighCentre.BorderWidthLeft = 1;
                    MidRighCentre.BorderWidthRight = 1;
                    MidRighCentre.BorderWidthTop = 1;
                    MidRighCentre.BorderWidthBottom = 1;
                    table.AddCell(MidRighCentre);

                    PdfPCell MidRighCentreH = new PdfPCell(new Phrase("Order #", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRighCentreH.Colspan = 1;
                    MidRighCentreH.Border = 0;
                    MidRighCentreH.BorderWidthLeft = 0;
                    MidRighCentreH.BorderWidthRight = 1;
                    MidRighCentreH.BorderWidthTop = 1;
                    MidRighCentreH.BorderWidthBottom = 1;
                    table.AddCell(MidRighCentreH);

                    PdfPCell MidRighCentreService = new PdfPCell(new Phrase("Service", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRighCentreService.Colspan = 1;
                    MidRighCentreService.Border = 0;
                    MidRighCentreService.BorderWidthLeft = 0;
                    MidRighCentreService.BorderWidthRight = 1;
                    MidRighCentreService.BorderWidthTop = 1;
                    MidRighCentreService.BorderWidthBottom = 1;
                    table.AddCell(MidRighCentreService);

                    PdfPCell MidRighCentreDesc = new PdfPCell(new Phrase("Description", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRighCentreDesc.Colspan = 4;
                    MidRighCentreDesc.Border = 0;
                    MidRighCentreDesc.BorderWidthLeft = 0;
                    MidRighCentreDesc.BorderWidthRight = 1;
                    MidRighCentreDesc.BorderWidthTop = 1;
                    MidRighCentreDesc.BorderWidthBottom = 1;
                    table.AddCell(MidRighCentreDesc);

                    PdfPCell MidRighCentreTotal = new PdfPCell(new Phrase("Total", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRighCentreTotal.Colspan = 1;
                    MidRighCentreTotal.Border = 0;
                    MidRighCentreTotal.BorderWidthLeft = 0;
                    MidRighCentreTotal.BorderWidthRight = 1;
                    MidRighCentreTotal.BorderWidthTop = 1;
                    MidRighCentreTotal.BorderWidthBottom = 1;
                    table.AddCell(MidRighCentreTotal);




                    PdfPCell MidRighCentreDesc10 = new PdfPCell(new Phrase("Billing Group Sub Total  :", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRighCentreDesc10.Colspan = 7;
                    MidRighCentreDesc10.Border = 0;
                    MidRighCentreDesc10.HorizontalAlignment = Element.ALIGN_RIGHT;

                    table.AddCell(MidRighCentreDesc10);

                    PdfPCell MidRighCentreTotal11 = new PdfPCell(new Phrase("$" + _total, FontFactory.GetFont(FontFactory.COURIER, 10)));
                    MidRighCentreTotal11.Colspan = 1;
                    MidRighCentreTotal11.Border = 0;
                    MidRighCentreTotal11.BorderWidthTop = 1;
                    MidRighCentreTotal11.BorderWidthBottom = 1;
                    table.AddCell(MidRighCentreTotal11);

                    PdfPCell MidRighCentreInvTotal = new PdfPCell(new Phrase("Invoice Total  :", FontFactory.GetFont(FontFactory.COURIER_BOLD, 10)));
                    MidRighCentreInvTotal.Colspan = 7;
                    MidRighCentreInvTotal.Border = 0;
                    MidRighCentreInvTotal.HorizontalAlignment = Element.ALIGN_RIGHT;
                    table.AddCell(MidRighCentreInvTotal);

                    PdfPCell MidRighINVTotal11 = new PdfPCell(new Phrase("$" + _total, FontFactory.GetFont(FontFactory.COURIER, 10)));
                    MidRighINVTotal11.Colspan = 1;
                    MidRighINVTotal11.Border = 0;
                    MidRighINVTotal11.BorderWidthTop = 0;
                    MidRighINVTotal11.BorderWidthBottom = 0;
                    table.AddCell(MidRighINVTotal11);


                    PdfPCell pdfFooterTempI = new PdfPCell(new Phrase("", FontFactory.GetFont(FontFactory.COURIER, 10)));
                    pdfFooterTempI.Colspan = 8;
                    pdfFooterTempI.Border = 0;
                    pdfFooterTempI.BorderWidthTop = 0;
                    pdfFooterTempI.BorderWidthBottom = 0;
                    table.AddCell(pdfFooterTempI);

                    PdfPCell pdfFooterTempLast = new PdfPCell(new Phrase(" \n \t \t PAYMENT IS DUE ON RECEIPT. LAST PAYMENT WILL BE ASSESSED  \n \t  A LATE FEE. PLEASE INCLUDE INV # WITH PAYMENT. THANK YOU. \n \t \t \t \t \t \t \t \t  FOR CHOOSING ABSOLUTE.  ", FontFactory.GetFont(FontFactory.COURIER_BOLDOBLIQUE, 11)));
                    pdfFooterTempLast.Colspan = 8;
                    pdfFooterTempLast.Border = 0;
                    pdfFooterTempLast.BorderWidthTop = 0;
                    pdfFooterTempLast.BorderWidthBottom = 0;
                    //pdfFooterTempLast.HorizontalAlignment ;
                    table.AddCell(pdfFooterTempLast);
                    doc.Add(table);
                    doc.NewPage();
                    doc.Close();

                    //if (TotalCount == arg.Length)
                    //{
                    //    doc.Close();

                    //}



                }
                catch (Exception)
                {
                    doc.Close();
                }
                // System.Diagnostics.Process.Start(pdfFilePath);
            }
        }


    }
}

感谢您的意见和建议

1 个答案:

答案 0 :(得分:2)

在iTextSharp中,您只有一个文档和多个页面。所以删除doc.Close();在Newpage()之后;把它放在循环外面。

doc.NewPage();
doc.Close(); // remove this: it will stop any further writing

您也只在循环的第一次迭代中创建一个新表。我不确定为什么。我以为你想要为每张发票买一张新桌子。

table = new PdfPTable(8); // move outside of the if (TotalCount == 1)

同时删除Try / Catch以允许显示任何错误。

// this stuff should be outside of the loop too
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
doc.Open();