1

I am using itextsharp for pdf generation using .net web applications. I have installed the following version of itextsharp using nuget package Install-Package iTextSharp -Version 5.5.10. I have seen the samples and developed code sample to generate the pdf and it is working. However i am not able to understand or get the header footer been added to the pdf. Here is the sample code i am using

public class Header : PdfPageEventHelper
{
    protected Phrase header;

    public void setHeader(Phrase header)
    {
        this.header = header;
    }

    public void onEndPage(PdfWriter writer, Document document)
    {
        PdfContentByte canvas = writer.DirectContent;
        ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, header, 559, 806, 0);
    }
}   


Document pdfReport = null;
MemoryStream msReport = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfReport, msReport);

if (!string.IsNullOrEmpty(HeaderText))
{
    Header objHeaderFooter = new Header();  
    //Here i need to assign the string HeaderText to Header. I dont know how to do it.                 
}

Please help to know how to assign HeaderText using itextSharp. Most of the developers examples are on java but i am using .net c#.

Ben
  • 763
  • 1
  • 5
  • 14
jai ganesh
  • 87
  • 1
  • 9
  • 1
    Duplicate? https://stackoverflow.com/questions/18996323/add-header-and-footer-for-pdf-using-itextsharp – Ben Aug 10 '17 at 06:48
  • solution sees to be complex. I am looking at a simpler approach to add the header and footer to the pdf file using c# – jai ganesh Aug 10 '17 at 07:32
  • "solution is complex" is not an argument for rejecting it – mihail Aug 10 '17 at 08:01
  • Possible duplicate of [Add Header and Footer for PDF using iTextsharp](https://stackoverflow.com/questions/18996323/add-header-and-footer-for-pdf-using-itextsharp) – mihail Aug 10 '17 at 08:04
  • Why are you using the coordinates `559, 806`? Are you sure those are right? You tell us in the comments of the answer that the header doesn't show up, but that would be very normal if `559, 806` is a coordinate outside the visible area of the page. – Bruno Lowagie Aug 10 '17 at 15:10

2 Answers2

2

You want to assign HeaderText to objHeaderFooter in

if (!string.IsNullOrEmpty(HeaderText))
{
    Header objHeaderFooter = new Header();  
    //Here i need to assign the string HeaderText to Header. I dont know how to do it.                 
}

Assuming HeaderText to be a string you can do so using the Header method setHeader:

objHeaderFooter.setHeader(new Phrase(HeaderText));

Furthermore, you have to assign objHeaderFooter to your PdfWriter instance:

pdfWriter.PageEvent = objHeaderFooter;

Thus:

if (!string.IsNullOrEmpty(HeaderText))
{
    Header objHeaderFooter = new Header();  
    objHeaderFooter.setHeader(new Phrase(HeaderText));
    pdfWriter.PageEvent = objHeaderFooter;
}

Furthermore, whenever you override a method in c#, mark it accordingly as override. In particular in your page event listener, use

public override void onEndPage(PdfWriter writer, Document document)

This is easy to forget, especially when porting java examples because in java the corresponding marker @Override is optional.


Below is the sample code provided by the op.

Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename=abc.pdf");
            Response.Charset = "";            
            Response.BinaryWrite(getbinary());
            Response.End();  

  public byte[] getbinary()
        {
            Document pdfReport = null;
            pdfReport = new Document(PageSize.A4, 25, 25, 40, 25);
            MemoryStream msReport = new MemoryStream();
            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfReport, msReport);

            pdfReport.Open();

            if (!string.IsNullOrEmpty("Header Text"))
            {
                Header objHeaderFooter = new Header();
                objHeaderFooter.SetHeader(new Phrase("Header Text"));
                pdfWriter.PageEvent = objHeaderFooter;
            }

            PdfPTable ptData1 = new PdfPTable(1);
            ptData1.SpacingBefore = 8;
            ptData1.DefaultCell.Padding = 1;
            ptData1.WidthPercentage = 100;
            ptData1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptData1.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;

            PdfPCell cell1 = new PdfPCell();
            cell1.BorderWidth = 0.001F;
            cell1.BackgroundColor = new BaseColor(250, 250, 250);
            cell1.BorderColor = new BaseColor(100, 100, 100);
            cell1.Phrase = new Phrase("Sample text");
            ptData1.AddCell(cell1);


            PdfPCell cell = new PdfPCell();
            cell.BorderWidth = 0.001F;
            cell.BackgroundColor = new BaseColor(200, 200, 200);
            cell.BorderColor = new BaseColor(100, 100, 100);

            cell.Phrase = new Phrase("test value");
            ptData1.AddCell(cell);
            pdfReport.Add(ptData1);

            pdfReport.Close();
            return  msReport.ToArray();

        }

public class Header : PdfPageEventHelper
    {
        protected Phrase header;

        public void SetHeader(Phrase header)
        {
            this.header = header;
        }

        public void onEndPage(PdfWriter writer, Document document)
        {
            PdfContentByte canvas = writer.DirectContent;
            ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, header, 559, 806, 0);
        }
    }
mkl
  • 90,588
  • 15
  • 125
  • 265
  • i tried doing it .but still i don't see the header in the pdf. Is there anything missed. or is it browser specific – jai ganesh Aug 10 '17 at 09:04
  • It is not browser specific. Thus, something is missing. Unfortunately your code is no [sscce](http://sscce.org), so I cannot use it to check. – mkl Aug 10 '17 at 09:30
  • a short sample using c# would help me, – jai ganesh Aug 10 '17 at 09:40
  • I'm currently only on a smart phone. I'll look into that later. – mkl Aug 10 '17 at 15:05
  • @jaiganesh I think you misunderstood mkl (and I hope you didn't do that intentionally). Mkl asked you to provide a [SSCCE](http://sscce.org), which means that *you* need to provide an example, so that *we* can take a look at what you are doing wrong. There are literally hundreds of examples on the [official web site](http://developers.itextpdf.com/faq/category/page-events-itext-5). Why don't you try one of those examples? I am very tempted to close your question as it has been answered many times before, and the answer isn't complex at all! – Bruno Lowagie Aug 10 '17 at 15:08
  • Below is the code sample Response.Clear(); Response.Buffer = true; Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=abc.pdf"); Response.Charset = ""; Response.BinaryWrite(getbinary()); Response.End(); – jai ganesh Aug 11 '17 at 10:50
0

May be this could be used as a workaround. Added header as another cell value Below is the code

 if (!string.IsNullOrEmpty(HeaderText))
            {
                PdfPTable Header = new PdfPTable(1);
                Header.SpacingBefore = 8;
                Header.DefaultCell.Padding = 1;
                Header.WidthPercentage = 100;
                Header.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
                Header.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;

                PdfPCell cell1 = new PdfPCell();
                cell1.BorderWidth = 0.001F;
                cell1.BackgroundColor = new BaseColor(250, 250, 250);
                cell1.BorderColor = new BaseColor(100, 100, 100);
                cell1.Phrase = new Phrase(HeaderText, fontBold);
                Header.AddCell(cell1);
                pdfReport.Add(ptDataHeader);
            }
jai ganesh
  • 87
  • 1
  • 9
  • Does your answer relate in any way with your question? You asked for adding headers and footers to a file and now you consider a table once solves that problem? – mkl Aug 16 '17 at 20:23
  • I have unmarked the answer and marked the other one which i have updated earlier. How ever the mistake was in the below line. It should have been as public override void OnnEndPage(PdfWriter writer, Document document) which was working. – jai ganesh Aug 17 '17 at 06:10
  • that indeed is extremely important. As you accepted my answer, I added a remark on that to it. I suppose I hadn't really looked at the `Header` code as the question focussed on *how to assign HeaderText using itextSharp*. – mkl Aug 17 '17 at 07:16