0

i know this question has been asked. but i can't make it work with the examples. (except one which is the code i'm using right now)

here is the snippet where i create a pdf using itextsharp. the code works for adding header and footer( but i want the header and footer text to be dynamic example from c# textbox)

string imagelocation = @"C:\Users\Desktop\1.jpg";
            string outputpdflocation = @"C:\Users\Desktop\headerfooter.pdf";
            using (MemoryStream ms = new MemoryStream())
            {
                Document doc = new Document(PageSize.A4, 10, 10, 42, 35);
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(outputpdflocation, FileMode.Create));
                doc.AddTitle("Document Title");



                doc.Open();
                writer.PageEvent = new MyHeaderFooterEvent();

                iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(imagelocation);
                image1.Alignment = iTextSharp.text.Image.ALIGN_CENTER;


                image1.ScaleToFit(700, 900);

                image1.SetAbsolutePosition((PageSize.A4.Width - image1.ScaledWidth) / 2, (PageSize.A4.Height - image1.ScaledHeight) / 2);
                doc.Add(image1);
                doc.Close();

            }

here is my code for adding header and footer(but not dynamic) i want the footer and header to be from a c# textbox

class MyHeaderFooterEvent : PdfPageEventHelper
        {
            iTextSharp.text.Font FONT = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 18, iTextSharp.text.Font.BOLD);

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            PdfContentByte canvas = writer.DirectContent;
            ColumnText.ShowTextAligned(
              canvas, Element.ALIGN_LEFT,
              new Phrase("Header", FONT), PageSize.A4.Width / 2, 810, 0
            );

            ColumnText.ShowTextAligned(
              canvas, Element.ALIGN_LEFT,
              new Phrase("Footer", FONT), 10, 10, 0
            );
        }
    }
codequery18
  • 151
  • 1
  • 3
  • 10

0 Answers0