Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

I am currently using ItextSharp to create a PDF file. I am working on generating a header along with a table below. The table works without any problems but when the header runs it creates a duplicate of 4 headers. I have been experimenting and working on solving this problem but so far I have had no luck after multiple hours.

C#
  class PrintForAccess    {
        Document printAccess2CareDoc;
        const int LINES_PER_ACCESS2CARE_DOC = 5;
        int pageCount = 0;
        PdfWriter writer;
        public void PrintAccessPDF(IEnumerable<ride> records, DateTime ridedate, int driverid, bool groupByDriver, string username, Stream outputstream, int tenantid, bool splitByDriver = false)
        {
printAccess2CareDoc = new iTextSharp.text.Document(PageSize.LETTER.Rotate(), .2f, .2f, .2f, .2f);
            writer = PdfWriter.GetInstance(printAccess2CareDoc, outputstream);
            PdfAcroForm acroForm = writer.AcroForm;
     printAccess2CareDoc.Open();
            PdfPTable table = printAccess2CareInitTable(defaultFont);
            PdfPTable headTable = printHeadTableAccess2Care(headerFont);          
            int detailLine = 0;
            driver previousDriver = null;
            bool firstRec = true;
            foreach(ride record in records)
            {
                if(groupByDriver && record.driver != previousDriver || firstRec)
                {                    
                    previousDriver = record.driver;              
                    detailLine = 0;
                    printAccess2CareNewPage(ref table, ref headTable, defaultFont, headerFont, firstRec);
                }
                printAccess2CareHeader(headTable, record, ridedate);
                printAccess2CareDetail(table, defaultFont, writer, record, ridedate);
                if((++detailLine) == LINES_PER_ACCESS2CARE_DOC)
                {
                    detailLine = 0;
                    printAccess2CareNewPage(ref table, ref headTable, defaultFont, headerFont, false);
                }
                firstRec = false;
            }
            printAccess2CareFooter(defaultFont);
            printAccess2CareDoc.Add(table);
            if (!splitByDriver)
                printAccess2CareDoc.Close();
            else
                printAccess2CareDoc.CloseDocument();
            return;
        }
        private void printAccess2CareNewPage(ref PdfPTable table, ref PdfPTable headTable, iTextSharp.text.Font defaultFont, iTextSharp.text.Font headerFont, bool suppressFooter)
        {
            if(!suppressFooter)
            {
                printAccess2CareDoc.Add(headTable);
                printAccess2CareDoc.Add(table);
                printAccess2CareFooter(defaultFont);
                printAccess2CareDoc.NewPage();
            }
            headTable = printHeadTableAccess2Care(headerFont);
            table = printAccess2CareInitTable(defaultFont);
        }
 private PdfPTable printHeadTableAccess2Care(iTextSharp.text.Font headfont)        {            
     const int COLUMNS = 3;
            PdfPTable table = new PdfPTable(COLUMNS);
            table.DefaultCell.Phrase = new Phrase("", headfont);
            table.DefaultCell.Border = Rectangle.NO_BORDER;
            table.TotalWidth = 760f;
            table.LockedWidth = true;
            float[] widths = new float[] { 250f, 250f, 260f };
            table.SetWidths(widths);
            table.SpacingBefore = 1f;
            return table;
        }
private void printAccess2CareHeader(PdfPTable table, ride ride, DateTime rideDate)
        {
            BaseFont times = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            float titleFontSize = 10;
            var boldHeaderFont = new iTextSharp.text.Font(times, titleFontSize, iTextSharp.text.Font.BOLD);
            var headerFont = new iTextSharp.text.Font(times, titleFontSize);
            var largeTitleFont = new iTextSharp.text.Font(times, 15, iTextSharp.text.Font.BOLD);

            PdfPTable headerTable1 = new PdfPTable(2);// (6);
            PdfPCell c1r1head = new PdfPCell(new Phrase("Provider: ", boldHeaderFont));
            c1r1head.PaddingTop = 5f;
            c1r1head.BorderWidth = 0f;
            c1r1head.PaddingLeft = -30f;
            PdfPCell c2r1head = new PdfPCell(new Phrase(/*strTenant*/"", headerFont));
            c2r1head.PaddingTop = 5f;
            c2r1head.PaddingLeft = -60f;
            c2r1head.BorderWidth = 0f;
            PdfPCell c3r1head = new PdfPCell(new Phrase("Date Of Service: ", boldHeaderFont));
            c3r1head.PaddingTop = 5f;
            c3r1head.PaddingLeft = -30f;
            c3r1head.BorderWidth = 0f;
            PdfPCell c4r1head = new PdfPCell(new Phrase(/*strDateToday*/"", headerFont));
            c4r1head.PaddingTop = 5f;
            c4r1head.PaddingLeft = -50f;
            c4r1head.BorderWidth = 0f;
            PdfPCell c5r1head = new PdfPCell(new Phrase("Check In____:____AM PM", headerFont));
            c5r1head.PaddingTop = 5f;
            c5r1head.PaddingLeft = -30f;
            c5r1head.BorderWidth = 0f;
            PdfPCell c6r1head = new PdfPCell(new Phrase("Check Out____:____AM PM", headerFont));
            c6r1head.PaddingLeft = -10f;
            c6r1head.PaddingRight = -20f;
            c6r1head.PaddingTop = 5f;
            c6r1head.BorderWidth = 0f;
            //row 2
            PdfPTable headerTable2 = new PdfPTable(6);
            PdfPCell c1r2head = new PdfPCell(new Phrase("Vehicle/VIN: ", boldHeaderFont));
            c1r2head.PaddingTop = 4f;
            c1r2head.PaddingLeft = -30f;
            c1r2head.BorderWidth = 0f;
            PdfPCell c2r2head = new PdfPCell(new Phrase("vehicle# " /*+ vinLast4digits*/, headerFont));
            c2r2head.PaddingLeft = -60f;
            c2r2head.BorderWidth = 0f;
            PdfPCell c3r2head = new PdfPCell(new Phrase("Driver Name: ", boldHeaderFont));
            c3r2head.PaddingLeft = -30f;
            c3r2head.BorderWidth = 0f;
            PdfPCell c4r2head = new PdfPCell(new Phrase(/*record.driver.person.fullname*/"", headerFont));
            c4r2head.PaddingLeft = -50f;
            c4r2head.BorderWidth = 0f;
            //PdfPCell c5r2head = new PdfPCell(new Phrase("", boldHeaderFont));
            //row 3
            PdfPTable headerTable3 = new PdfPTable(6);
            PdfPCell c1r3head = new PdfPCell(new Phrase("Driver Signature: ", boldHeaderFont));
            //c1r3head.PaddingTop = 10f;
            c1r3head.PaddingLeft = -30f;
            c1r3head.BorderWidth = 0f;
            PdfPCell c2r3head = new PdfPCell(new Phrase(""));
            c2r3head.BorderWidth = 0f;
            PdfPCell c4r3head = new PdfPCell(new Phrase("Access2Care", largeTitleFont));
            c4r3head.Colspan = 2;
            c4r3head.Border = 0;
            c4r3head.PaddingTop = 2f;
            PdfPCell c5r3head = new PdfPCell(new Phrase("Check In____:____AM PM", headerFont));
            //c5r3head.PaddingRight = 20f;
            c5r3head.PaddingTop = 5f;
            c5r3head.PaddingLeft = -30f;
            c5r3head.BorderWidth = 0f;
            PdfPCell c6r3head = new PdfPCell(new Phrase("Check Out____:____AM PM", headerFont));
            c6r3head.PaddingRight = -20f;
            c6r3head.PaddingTop = 5f;
            c6r3head.PaddingLeft = -10f;
            c6r3head.BorderWidth = 0f;

            headerTable1.AddCell(c4r3head);
            headerTable1.AddCell(c1r2head);
            headerTable1.AddCell(c2r2head);
            headerTable1.AddCell(c1r1head);
            headerTable1.AddCell(c2r1head);

            headerTable2.AddCell(c3r1head);
            headerTable2.AddCell(c4r1head);
            headerTable2.AddCell(c3r2head);
            headerTable2.AddCell(c4r2head);
            headerTable2.AddCell(c1r3head);
            headerTable2.AddCell(c2r3head);

            headerTable3.AddCell(c5r1head);
            headerTable3.AddCell(c6r1head);
            headerTable3.AddCell(c2r3head);
            headerTable3.AddCell(c2r3head);
            headerTable3.AddCell(c5r3head);
            headerTable3.AddCell(c6r3head);
            
            table.AddCell(headerTable1);            
            table.AddCell(headerTable2);
            table.AddCell(headerTable3);
        }
Posted
Comments
Richard Deeming 14-Oct-15 11:33am    
You're calling printAccess2CareHeader for every record. Therefore, it's hardly surprising that you're getting a header for every record.

Change your code to only call that method when you want to display a header.
TheBigBearNow 14-Oct-15 11:43am    
I am only getting 4 Headers not one for every record. I had one of the method calls commented out but then i was getting 0 headers

1 solution

Here is the answer to my question
I finally solved it!!
C#
foreach(ride record in records)
           {
               if(groupByDriver && record.driver != previousDriver || firstRec)
               {
                   if(!firstRec)
                   {
                       printAccessFivelengths(ref table, defaultFont, detailLine, headTable, headerFont);
                   }
                   previousDriver = record.driver;

                   if (record.driver != null && (driverid != 0 || groupByDriver))
                   {
                       //pageHeaderWithDriver.Add(new Phrase(titlec1 + record.driver.person.fullname, headerFont));
                       c4r2head = new PdfPCell(new Phrase(record.driver.person.fullname, headerFont));
                       headerTable.AddCell(c4r2head);
                   }
                   detailLine = 0;
                   printAccessHeader(headTable, record, ridedate, username);
                   printAccessNewPage(ref table, ref headTable, defaultFont, headerFont, firstRec);

                   table = printAccessInitTable(defaultFont);
                   headTable = printAccessHeaderInit(headerFont);
               }

               printAccessDetail(table, defaultFont, writer, record, ridedate);

               if((++detailLine) == LINES_PER_ACCESS_DOC)
               {
                   detailLine = 0;
                   //print new page
                   printAccessHeader(headTable, record, ridedate, username);
                   printAccessNewPage(ref table, ref headTable, defaultFont, headerFont, false);
               }
               firstRec = false;
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900