skip to Main Content

I work on itextcharp library to export grid view to pdf .
I face issue header column display within data and it must

display above of data
so my issue data rows display above header
exactly .

I need to display header on top then details rows data consistence with header

I need header data display on every page
header meaning column headers

so how to solve this issue .

ON end page event of class pageeventhelper I add header data because i need display header on every page

What I try as below :

[WebMethod]
        public byte[] ExportGridViewToPdf(string[][] gridData, string[] columnNames)
        {
            try
            {

            
                Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                var ms = new MemoryStream();
                var writer = PdfWriter.GetInstance(document, ms);
                var tahomaFontFile = Path.Combine(
 Environment.GetFolderPath(Environment.SpecialFolder.Fonts),
 "Tahoma.ttf");
                var tahomaBaseFont = BaseFont.CreateFont(tahomaFontFile,
     BaseFont.IDENTITY_H,
     BaseFont.EMBEDDED);
                var tahomaFont = new Font(tahomaBaseFont, 8, Font.NORMAL);
                PageEventHelper pageEventHelper = new PageEventHelper(columnNames, tahomaFont, gridData);
                writer.PageEvent = pageEventHelper;
                document.Open();




                var table = new PdfPTable(columnNames.Length)
                {

                    RunDirection = PdfWriter.RUN_DIRECTION_RTL
                };
                table.WidthPercentage = 100;

            


         
                foreach (var row in gridData)
                {
                    var cells = new PdfPCell[columnNames.Length];
                    for (int i = 0; i < columnNames.Length; i++)
                    {
                        cells[i] = new PdfPCell(new Phrase(row[i], tahomaFont));
                        cells[i].FixedHeight = 25f;
                        table.AddCell(cells[i]); // Add each cell individually
                    }
                }
                

               

                document.Add(table);


                document.Close();
                writer.Close();
                HttpContext.Current.Response.ContentType = "application/pdf";
                return ms.ToArray();
            }
            catch (Exception ex)
            {
                
                throw new Exception("Error exporting GridView to PDF: " + ex.Message);
            }
        }
using iText.Kernel.Pdf;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;

namespace EREG.BusinessLayer
{
    public class PageEventHelper : PdfPageEventHelper
    {
        private int pageNumber = 0;
        private int totalPages = 0;
        PdfTemplate footerTemplate;
        BaseFont bf = null;
        PdfContentByte cb;
        private readonly string[] _columnNames;
        private readonly iTextSharp.text.Font _tahomaFont;
        private readonly string[][] _gridData;
        public PageEventHelper(string[] columnNames, iTextSharp.text.Font tahomaFont, string[][] gridData)
        {
            _columnNames = columnNames;
            _tahomaFont = tahomaFont;
            _gridData = gridData;
        }
        
        public override void OnOpenDocument(iTextSharp.text.pdf.PdfWriter writer, Document document)
        {
            try
            {
                bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                cb = writer.DirectContent;
                footerTemplate = cb.CreateTemplate(50, 50);
            }
            catch (DocumentException de)
            {
                //handle exception here
            }
            catch (System.IO.IOException ioe)
            {
                //handle exception here
            }
        }
        public override void OnStartPage(iTextSharp.text.pdf.PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);
            pageNumber++;
        }
        public override void OnCloseDocument(iTextSharp.text.pdf.PdfWriter writer, Document document)
        {
            base.OnCloseDocument(writer, document);
            //totalPages = writer.PageNumber;
            footerTemplate.BeginText();
            footerTemplate.SetFontAndSize(bf, 12);
            footerTemplate.SetTextMatrix(0, 0);
            footerTemplate.ShowText((writer.PageNumber - 1).ToString());
            footerTemplate.EndText();

        }

        public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);

            // Create a new PdfContentByte instance
            PdfContentByte cb = writer.DirectContent;

            // Add page number at the bottom
            string pageNumberText = $"Page {writer.PageNumber} of ";
            float pageNumberWidth = bf.GetWidthPoint(pageNumberText, 12);
            float pageNumberX = (document.PageSize.Width - pageNumberWidth) / 2;
            float pageNumberY = document.BottomMargin;

            cb.BeginText();
            cb.SetFontAndSize(bf, 12);
            cb.SetTextMatrix(pageNumberX, pageNumberY);
            cb.ShowText(pageNumberText);
            cb.EndText();

            // Add the total pages placeholder
            cb.AddTemplate(footerTemplate, pageNumberX + pageNumberWidth, pageNumberY);

            // Create and add the header table
            PdfPTable headerTable = new PdfPTable(_columnNames.Length) { RunDirection = iTextSharp.text.pdf.PdfWriter.RUN_DIRECTION_RTL };

            
            foreach (var columnName in _columnNames)
            {
                var cell = new PdfPCell(new Phrase(columnName, _tahomaFont));
                cell.BackgroundColor = new BaseColor(System.Drawing.Color.LightGray);
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                headerTable.AddCell(cell);
            }

           
            headerTable.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - document.TopMargin - 20, cb);

          


           


          
        }
        
    }
}

image show issue as below

enter image description here

2

Answers


  1. I would suggest that you adopt some kind of reporting system.

    The problem is you attempting to use a GridView, and not only convert the GridView to a PDF (which as you can see can be quite a bit of work).

    However, "more" of a challenge is now you want the GridView heading to appear at the top of each page. And probably later on, someone will ask that you include a page number. Then maybe later on, someone wants a grouping in the report. Then maybe later on, you need a report with totals.

    In other words, the result is you going to be writing boatloads of code, and all that code will be difficult to maintain, difficult to change, difficult to add new simple features like a heading at the top of each page, page numbers etc. And then when all that work is done, you THEN want the results as a PDF.

    Hence, I suggest you don’t hand code the output, don’t try to make the PDF, and also don’t try to convert and change the GridView into some kind of "reporting" system.

    There are great tools for this purpose. In fact, webforms and even vs2022 still supports using what is called RDL reports (RDL = Report definition Language). So, I suggest you ensure that the RDL reporting tools are installed, and then consider using a report.

    So, for a simple report of hotels, then with the report writer, I lay it out like this:

    enter image description here

    Now, the web page with report looks like this:

    enter image description here

    Note VERY close how the report system can export to Excel, Word or as a PDF. And note how there is even a print the PDF button in above. And when I choose PDF, it downloads the PDF for me (again saving a lot of code). And note VERY close on the 2nd page of the above, the heading is repeated.

    So, I recommend you adopt a reporting system. The RDL and report designer is free, and is part of asp.net webforms. While the report writer and designer does have a learning curve, the above report was created without me having to write ANY code!!! Now, much of my previous experience is from desktop and MS-Access (which has the best report writer I have ever used). So, I at first found the RDL designer tools a bit of a challenge, but spend a day on learning this system, and then you have a VERY high-quality reporting system that you can use for years. Better yet is the fantastic printer support, and even better is the automatic output to PDF. Over time, I suspect that you want additional reports, and using the GridiView is not designed for this purpose, hence your existing struggles.

    Your current approach is costing too much time and money, and all that code will be for a "one time" report. Adopting a great reporting system will not only save large amounts of code, but also offer far better export to PDF and Excel options, all without extra coding efforts on your part.

    Login or Signup to reply.
  2. I need to display header on top

    Delete the marked parts in the figure.

    enter image description here

    headerTable.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - document.TopMargin, cb);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search