使用itextsharp创建PDF以获取绑定报告的问题

时间:2017-02-20 05:18:39

标签: c# itext

当我使用itextsharp绑定我的报告以创建PDF时出现问题,当我的报告超过80或100行时,它从第二页开始跳过第一页但少于它从第一页开始工作。这里我的代码是使用问题屏幕短Record bind from second page添加报告 代码:

                table = new PdfPTable(2);
                table.TotalWidth = Size;
                table.LockedWidth = true;
                table.DefaultCell.Border = Rectangle.NO_BORDER;
                table.SetWidths(new float[] { 265f, 265f });



                insideTable1 = new PdfPTable(1);
                insideTable1.TotalWidth = 260f;
                insideTable1.LockedWidth = true;
                insideTable1.DefaultCell.Border = Rectangle.NO_BORDER;
                insideTable1.SetWidths(new float[] { 260f });

                insideTable11 = new PdfPTable(widthArry);
                insideTable11.TotalWidth = 260f;
                insideTable11.LockedWidth = true;
                insideTable11.SetWidths(widthArry);



                for (int a = 0; a < colindexid.Count; a = a + 1)
                {
                    #region addcolumnHeadeName


                    if (colindexid[a].ToString() == Convert.ToString(0))
                    {

                        phrase = new Phrase();
                        phrase.Add(new Chunk(H1, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                        cell.FixedHeight = 15f;
                        insideTable11.AddCell(cell);


                    }
                    else if (colindexid[a].ToString() == Convert.ToString(1))
                    {

                        phrase = new Phrase();
                        phrase.Add(new Chunk(H2, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                        cell.FixedHeight = 15f;
                        insideTable11.AddCell(cell);



                    }
                    else if (colindexid[a].ToString() == Convert.ToString(2))
                    {

                        phrase = new Phrase();
                        phrase.Add(new Chunk(H3, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                        cell.FixedHeight = 15f;
                        insideTable11.AddCell(cell);


                    }
                    else if (colindexid[a].ToString() == Convert.ToString(3))
                    {
                        phrase = new Phrase();
                        phrase.Add(new Chunk(H4, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                        cell.FixedHeight = 15f;
                        insideTable11.AddCell(cell);



                    }
                    #endregion addcolumnHeadeName
                }


                for (int i = 1; i <= 51; i++)
                {
                    cnt++;
                    string Name = "";


                    #region addrow

                    if (colindexid.Contains(Convert.ToInt32(0)))
                    {


                        phrase = new Phrase();
                        phrase.Add(new Chunk(cnt.ToString(), f));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);

                        insideTable11.AddCell(cell);

                    }
                    else
                    {

                    }
                    if (colindexid.Contains(Convert.ToInt32(1)))
                    {

                        phrase = new Phrase();
                        phrase.Add(new Chunk("E1", f));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                        insideTable11.AddCell(cell);
                    }
                    else
                    {

                    }
                    if (colindexid.Contains(Convert.ToInt32(2)))
                    {

                        phrase = new Phrase();
                        //string SName = "";
                        //if (!string.IsNullOrEmpty(Name.ToLower()))
                        //{
                        //    SName = textInfo.ToTitleCase(Name.ToLower());
                        //}
                        phrase.Add(new Chunk("Ravi Shah", f));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                        insideTable11.AddCell(cell);


                    }
                    else
                    {

                    }
                    if (colindexid.Contains(Convert.ToInt32(3)))
                    {
                        //string SC = "";
                        //if (!string.IsNullOrEmpty(itm.ServiceCategory.ToLower()))
                        //{
                        //    SC = textInfo.ToTitleCase(itm.ServiceCategory.ToLower());
                        //}
                        //string ST = "";
                        //if (!string.IsNullOrEmpty(itm.ServiceType.ToLower()))
                        //{
                        //    ST = textInfo.ToTitleCase(itm.ServiceType.ToLower());
                        //}
                        phrase = new Phrase();
                        phrase.Add(new Chunk("Teacher" + "\n", f));
                        phrase.Add(new Chunk("" + "\n", FontFactory.GetFont("Arial Unicode MS,Arial", 5, iTextSharp.text.Color.BLACK)));
                        cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                        cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                        insideTable11.AddCell(cell);


                    }
                    else
                    {

                    }


                    #endregion addrow
                }


                insideTable1.AddCell(insideTable11);
                table.AddCell(insideTable1);



                    insideTable2 = new PdfPTable(1);
                    insideTable2.TotalWidth = 260f;
                    insideTable2.LockedWidth = true;
                    insideTable2.DefaultCell.Border = Rectangle.NO_BORDER;
                    insideTable2.SetWidths(new float[] { 260f });




                    insideTable22 = new PdfPTable(widthArry);
                    insideTable22.TotalWidth = 260f;
                    insideTable22.LockedWidth = true;
                    insideTable22.SetWidths(widthArry);


                    for (int a = 0; a < colindexid.Count; a = a + 1)
                    {
                        #region addcolumnHeadeName


                        if (colindexid[a].ToString() == Convert.ToString(0))
                        {

                            phrase = new Phrase();
                            phrase.Add(new Chunk(H1, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                            cell.FixedHeight = 15f;
                            insideTable22.AddCell(cell);


                        }
                        else if (colindexid[a].ToString() == Convert.ToString(1))
                        {

                            phrase = new Phrase();
                            phrase.Add(new Chunk(H2, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                            cell.FixedHeight = 15f;
                            insideTable22.AddCell(cell);



                        }
                        else if (colindexid[a].ToString() == Convert.ToString(2))
                        {

                            phrase = new Phrase();
                            phrase.Add(new Chunk(H3, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                            cell.FixedHeight = 15f;
                            insideTable22.AddCell(cell);


                        }
                        else if (colindexid[a].ToString() == Convert.ToString(3))
                        {
                            phrase = new Phrase();
                            phrase.Add(new Chunk(H4, FontFactory.GetFont("Arial Unicode MS,Arial", 6, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                            cell.FixedHeight = 15f;
                            insideTable22.AddCell(cell);



                        }

                        #endregion addcolumnHeadeName
                    }

                    for (int i = 1; i <= 50; i++)
                    {
                        cnt++;
                        string Name = "";


                        #region addrow

                        if (colindexid.Contains(Convert.ToInt32(0)))
                        {


                            phrase = new Phrase();
                            phrase.Add(new Chunk(cnt.ToString(), f));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);

                            insideTable22.AddCell(cell);

                        }
                        else
                        {

                        }
                        if (colindexid.Contains(Convert.ToInt32(1)))
                        {

                            phrase = new Phrase();
                            phrase.Add(new Chunk("M1", f));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                            insideTable22.AddCell(cell);
                        }
                        else
                        {

                        }
                        if (colindexid.Contains(Convert.ToInt32(2)))
                        {

                            phrase = new Phrase();
                            //string SName = "";
                            //if (!string.IsNullOrEmpty(Name.ToLower()))
                            //{
                            //    SName = textInfo.ToTitleCase(Name.ToLower());
                            //}
                            phrase.Add(new Chunk("Raj Patel", f));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                            insideTable22.AddCell(cell);


                        }
                        else
                        {

                        }
                        if (colindexid.Contains(Convert.ToInt32(3)))
                        {

                            //string SC = "";
                            //if (!string.IsNullOrEmpty(itm.ServiceCategory.ToLower()))
                            //{
                            //    SC = textInfo.ToTitleCase(itm.ServiceCategory.ToLower());
                            //}
                            //string ST = "";
                            //if (!string.IsNullOrEmpty(itm.ServiceType.ToLower()))
                            //{
                            //    ST = textInfo.ToTitleCase(itm.ServiceType.ToLower());
                            //}
                            phrase = new Phrase();
                            phrase.Add(new Chunk("Sir" + "\n", f));
                            phrase.Add(new Chunk("" + "\n", FontFactory.GetFont("Arial Unicode MS,Arial", 5, iTextSharp.text.Color.BLACK)));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);
                            insideTable22.AddCell(cell);

                        }
                        else
                        {

                        }


                        #endregion addrow

                    }
                    if (result11.Count == result22.Count + 1)
                    {
                        #region addrow

                        if (colindexid.Contains(Convert.ToInt32(0)))
                        {


                            phrase = new Phrase();
                            phrase.Add(new Chunk("", f));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);

                            insideTable22.AddCell(cell);

                        }
                        else
                        {

                        }
                        if (colindexid.Contains(Convert.ToInt32(1)))
                        {

                            phrase = new Phrase();
                            phrase.Add(new Chunk("", f));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);

                            insideTable22.AddCell(cell);
                        }
                        else
                        {

                        }
                        if (colindexid.Contains(Convert.ToInt32(2)))
                        {

                            phrase = new Phrase();
                            phrase.Add(new Chunk("", f));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);

                            insideTable22.AddCell(cell);
                        }
                        else
                        {

                        }
                        if (colindexid.Contains(Convert.ToInt32(3)))
                        {
                            phrase = new Phrase();

                            phrase.Add(new Chunk("", f));
                            cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
                            cell.BorderColor = new iTextSharp.text.Color(229, 229, 229);

                            insideTable22.AddCell(cell);

                        }
                        else
                        {

                        }

                        #endregion addrow
                    }




                    insideTable2.AddCell(insideTable22);
                    table.AddCell(insideTable2);

                document.Add(table);

谢谢..

1 个答案:

答案 0 :(得分:1)

添加以下行:

table.SplitLate = false;

SplitLate的默认值为true,这意味着iTextSharp会尝试向页面添加完整行。如果某行不合适,它将跳到下一页。这解释了您注意到的行为。