2015-04-08 47 views
0

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

问题 ,当我在我的功能,此功能生成和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); 
      } 
     } 


    } 
} 

感谢您的评论和建议

+0

您已经发布了414行代码,但大部分与您的具体问题无关。例如,'rightL2.Border = 1;'与多个页面无关,可以从问题中删除。另外,您要将26个单元格添加到表格中。除非这些问题与问题有关,否则这些问题也可以解决。您应该能够将问题简化为25至50行,这可以使我们更容易诊断。 –

回答

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(); 
+0

我做了这个,但pdf不生成 –

+0

我已经更新了我的答案了一下。 – PeteG

+0

我做了这个更改,但这是生成两个pdf如果我通过两个发票没有 –