Hi everyone

I am trying to place image header and footer on every page of a pdf document for that i wrote the following code With this i am able to place header footer images on the first page only to repeat the process through the pages i am suggested to use onEndPage event. But i couldnot find the straight c# way out of it can someone Help Please

FileStream fos = new FileStream(DocumentName, FileMode.Create);

PdfWriter writer = PdfWriter.getInstance(myDocument, fos);

myDocument.Open();

iTextSharp.text.Image imgfoot = iTextSharp.text.Image.getInstance(footerImage);

iTextSharp.text.Image imghead = iTextSharp.text.Image.getInstance(headerImage);

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, Font.NORMAL));

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

HeaderFooter header = new HeaderFooter(headPhraseImg, true);

HeaderFooter footer = new HeaderFooter(footPhraseImg, true);

 

Thanks in Advance

DAHAL SACHIT



 


From:  "Paulo Soares" <[EMAIL PROTECTED]>
Reply-To:  Post all your questions about iText here <[email protected]>
To:  "Post all your questions about iText here" <[email protected]>
Subject:  Re: [iText-questions] Extra blank page at end of document.
Date:  Mon, 30 Oct 2006 10:39:36 -0000
>You are using Table and that's not supported anymore but some good soul may have provided a fix in the latest release. Use PdfPTable to make sure it will work.
>
>Paulo
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On
> > Behalf Of Musolino Fabien
> > Sent: Monday, October 30, 2006 10:24 AM
> > To: [EMAIL PROTECTED]
> > Subject: [iText-questions] Extra blank page at end of document.
> >
> > Hi all.
> >
> > I’m trying to create a PDF document using iText 1.4.2.
> >
> > I would like to add a special footer at the end of document.
> >
> > I already addes the “Page x on y” stuff and it’s working fine.
> >
> > My special footer also prints but on a blank page.
> >
> > If my document has only one page, everything’s ok. If it has
> > more than one page, the footer “creates” an extra page just for it.
> >
> > I followed the recommendation saying that theses footer
> > things should be placed in the onEndPage event.
> >
> > In this document, I’m trying to create a table
> >
> > Here’s the code of the table :
> >
> >
> >
> >                           //An object creating our standard table
> >
> >                PDFStandardTable tablePoursuites = new
> > PDFStandardTable(100);
> >
> >                tablePoursuites.setPadding(1);
> >
> >                tablePoursuites.setSpaceBetweenCells(0);
> >
> >                tablePoursuites.setCellsFitPage(true);
> >
> >
> > tablePoursuites.addColumn(7,PDFStandardTable.ALIGN_CENTER,"N° doss.");
> >
> >
> > tablePoursuites.addColumn(7,PDFStandardTable.ALIGN_CENTER,"N° série");
> >
> >                tablePoursuites.addColumn(39,"Représentant du
> > créancier\nCréancier");
> >
> >
> > tablePoursuites.addColumn(8,PDFStandardTable.ALIGN_RIGHT,"Créa
> > nce\nRésultat");
> >
> >
> > tablePoursuites.addColumn(8,PDFStandardTable.ALIGN_RIGHT,"Sold
> > e\nPerte");
> >
> >
> > tablePoursuites.addColumn(1,PDFStandardTable.ALIGN_RIGHT,"");
> >
> >                tablePoursuites.addColumn(30,"Etat\nRéférence
> > créancier");
> >
> >                tablePoursuites.setLastHeaderRow(1);
> >
> >                tablePoursuites.prepareTableLines();
> >
> >              //Adds a title to the table
> >
> >
> > tablePoursuites.addColspanedLine(PDFStandardValues.boldItalicF
> > ont, "Poursuites", Element.ALIGN_LEFT);
> >
> >              //Adds the headres
> >
> >
> > tablePoursuites.addHeadersLine(PDFStandardValues.boldFontSmall);
> >
> >                if
> > (virtualSituationBean.getDebtorRequisitionsListSize() == 0){
> >
> >                     //Message if nothing to display
> >
> >
> > tablePoursuites.addColspanedLine(PDFStandardValues.normalFontS
> > mall, "Pas de poursuites.", Element.ALIGN_CENTER);
> >
> >                } else {
> >
> >                     //Adds th values
> >
> >                    for (SimpleVirtualCreditBean current :
> > virtualSituationBean.getDebtorRequisitionsList()) {
> >
> >
> > tablePoursuites.addValuesLine(PDFStandardValues.normalFontSmal
> > l, new String[]{current.getNumeroDossier(),
> > current.getNumeroSerie(), current.getRepresentantCreancier(),
> > current.getCreance(), current.getSoldeRestant(), "",
> > current.getEtat()});
> >
> >
> > tablePoursuites.addValuesLine(PDFStandardValues.normalFontSmal
> > l, new String[]{"", "",  current.getCreancier(),
> > current.getResultat(), current.getPerte(), "",
> > current.getReferenceCreancier()});
> >
> >                    }
> >
> >                     //Adds the total
> >
> >
> > tablePoursuites.addTotalsLine(PDFStandardValues.boldFontSmall,
> >  new String[]{"", "", "",
> > Tools.getBetragAsString(virtualSituationBean.getPoursuitesTota
> > lCreance(),false,true),
> > Tools.getBetragAsString(virtualSituationBean.getPoursuitesTota
> > lSoldeRestant(),false,true), "", ""});
> >
> >
> > tablePoursuites.addTotalsLine(PDFStandardValues.boldFontSmall,
> >  new String[]{"", "", "",
> > Tools.getBetragAsString(virtualSituationBean.getPoursuitesTota
> > lResultat(),false,true),
> > Tools.getBetragAsString(virtualSituationBean.getPoursuitesTota
> > lPerte(),false,true), "", ""});
> >
> >                }
> >
> >             document.add(tablePoursuites);
> >
> >
> >
> >
> >
> > Here’s the code for the events :
> >
> >
> >
> >     public void onOpenDocument(PdfWriter pdfWriter, Document
> > document) {
> >
> >        PdfContentByte cb = pdfWriter.getDirectContent();
> >
> >              cb = pdfWriter.getDirectContent();
> >
> >         tpl = cb.createTemplate(50, 25);
> >
> >         tpl.setBoundingBox(new Rectangle(0, 0, 50, 25));
> >
> >         try {
> >
> >              helv = BaseFont.createFont("Helvetica",
> > BaseFont.WINANSI, false);
> >
> >        } catch (DocumentException e) {
> >
> >              e.printStackTrace();
> >
> >        } catch (IOException e) {
> >
> >              e.printStackTrace();
> >
> >        }
> >
> >     }
> >
> >
> >
> >     public void onEndPage(PdfWriter writer, Document document) {
> >
> >        PdfContentByte cb = writer.getDirectContent();
> >
> >
> >
> >        try {
> >
> >              cb.saveState();
> >
> >              PDFStandardLibrary.addLightHeader(document, cb, helv);
> >
> >              cb.restoreState();
> >
> >        } catch (DocumentException e) {
> >
> >              e.printStackTrace();
> >
> >        }
> >
> >
> >
> >         cb.saveState();
> >
> >         String text = "Page " + writer.getPageNumber() + " sur ";
> >
> >         float textSize = helv.getWidthPoint(text, pageFontSize);
> >
> >         float textBase = document.bottom() - 20;
> >
> >         cb.beginText();
> >
> >         cb.setFontAndSize(helv, pageFontSize);
> >
> >         cb.setTextMatrix(document.left(), textBase);
> >
> >         cb.showText(text);
> >
> >         cb.endText();
> >
> >         cb.addTemplate(tpl, document.left() + textSize, textBase);
> >
> >         cb.restoreState();
> >
> >
> >
> >
> >
> >        BaseFont bf =
> > BaseFont.createFont(BaseFont.HELVETICA_BOLDOBLIQUE,
> > BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
> >
> >        float fontSize =
> > PDFStandardValues.boldItalicFont.getCalculatedSize();
> >
> >         float imgOffset = 0;
> >
> >
> >
> >         if (this.endOfPageImageName != null &&
> > !this.endOfPageImageName.equals("")){
> >
> >              Image img =
> > Image.getInstance(Util.getImageAsByteArray(this.endOfPageImage
> > Name, service.getFileContext(), GvuDebtEnforcementEnv.APP_NAME));
> >
> >              img.scalePercent(this.endOfPageImageScale);
> >
> >              float imgWidth = (img.width() *
> > ((float)this.endOfPageImageScale/(float)100));
> >
> >              float imgHeight = (img.height() *
> > ((float)this.endOfPageImageScale/(float)100));
> >
> >              float x = document.getPageSize().width() -
> > document.rightMargin() - imgWidth;
> >
> >              float y = document.bottomMargin() +
> > (bf.getAscentPoint(this.endOfPageText, fontSize) -
> > bf.getDescentPoint(this.endOfPageText, fontSize));
> >
> >              cb.saveState();
> >
> >              cb.addImage(img, imgWidth, 0, 0, imgHeight, x, y);
> >
> >              cb.restoreState();
> >
> >         }
> >
> >         if (this.endOfPageText != null &&
> > !this.endOfPageText.equals("")){
> >
> >                     cb.saveState();
> >
> >              cb.beginText();
> >
> >                cb.setFontAndSize(bf, fontSize);
> >
> >
> > cb.setColorFill(PDFStandardValues.boldItalicFont.color());
> >
> >
> > cb.showTextAligned(PdfContentByte.ALIGN_RIGHT,this.endOfPageTe
> > xt,document.getPageSize().width()-document.rightMargin(),docum
> > ent.bottomMargin()+imgOffset,0);
> >
> >                 cb.endText();
> >
> >                cb.restoreState();
> >
> >        }
> >
> >     }
> >
> >
> >
> >     public void onCloseDocument(PdfWriter pdfWriter, Document
> > document) {
> >
> >         tpl.beginText();
> >
> >         tpl.setFontAndSize(helv, pageFontSize);
> >
> >         tpl.setTextMatrix(0, 0);
> >
> >         tpl.showText("" + (pdfWriter.getPageNumber() - 1));
> >
> >         tpl.endText();
> >
> >     }
> >
> >
> >
> >
> >
> > Can someone help me ?
> >
> > Thank you.
> >
> >
> >
> > -------------------------------------------------------
> >
> > Fabien Musolino
> >
> > Développeur Web (www.ne.ch <http://www.ne.ch> )
> >
> > Service du Traitement de l'Information
> >
> > Faubourg du Lac 25
> >
> > 2000 Neuchâtel
> >
> > +41 32 889 84 21
> >
> > [EMAIL PROTECTED]
> >
> > -------------------------------------------------------
> >
> >
> >
> >
>
>
>Aviso Legal:
>Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem.
>
>Disclaimer:
>This message is destined exclusively to the intended receiver. It may contain confidential or legally protected information. The incorrect transmission of this message does not mean the loss of its confidentiality. If this message is received by mistake, please send it back to the sender and delete it from your system immediately. It is forbidden to any person who is not the intended receiver to use, distribute or copy any part of this message.
>
>-------------------------------------------------------------------------
>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


Don't just search. Find. MSN Search Check out the new MSN Search!
-------------------------------------------------------------------------
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