Hey Dudes,

I just completed with inserting images in the header and footer of the PDF Documents, the images are shown on every pages without extra pages and without garbage strings on the header and footer Hope this helps You all.

I have attached the codes needed check them out or you can understand them from below as:

1)

private void button1_Click(object sender, EventArgs e)
       {
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10);
           try
           {
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileStream("test.pdf", FileMode.Create));
               document.Open();
               pdfClass.MyHandler myEvents = new pdfClass.MyHandler();
               pdfWriter.PageEvent = myEvents;
               myEvents.onStartPage(pdfWriter, document);

               for (int i = 0; i < 500; i++)
                   document.Add(new Paragraph(i+"  Hello World"));
           }
           catch
           {
               document.Close();
           }
           finally
           {
               document.Close();
           }

       }

2) Inside the onStartPage() of the class MyHandler


public override void onStartPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
       {
iTextSharp.text.Image imgfoot = iTextSharp.text.Image.getInstance("FooterImage.jpg"); iTextSharp.text.Image imghead = iTextSharp.text.Image.getInstance("HeaderImage.jpg");

           imgfoot.setAbsolutePosition(0, 0);
           imghead.setAbsolutePosition(0, 0);

           PdfContentByte cbhead = writer.DirectContent;
           PdfTemplate tp = cbhead.createTemplate(600, 250);
           tp.addImage(imghead);

           PdfContentByte cbfoot = writer.DirectContent;
           PdfTemplate tpl = cbfoot.createTemplate(600, 250);
           tpl.addImage(imgfoot);

           cbhead.addTemplate(tp, 0, 715);
           cbfoot.addTemplate(tpl, 0, 0);

Phrase headPhraseImg = new Phrase(cbhead + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, iTextSharp.text.Font.NORMAL)); Phrase footPhraseImg = new Phrase(cbfoot + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, iTextSharp.text.Font.NORMAL));

           HeaderFooter header = new HeaderFooter(headPhraseImg, true);
           HeaderFooter footer = new HeaderFooter(footPhraseImg, true);


           base.onStartPage(writer, document);
       }

This worked for me hope You all will get benefitted

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! http://search.msn.com/

Attachment: Form1.cs
Description: Binary data

Attachment: MyHandler.cs
Description: Binary data

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to