Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 927a1532fb070c51449654bb68b5bde2 > files > 453

itext-manual-2.0.8-0.0.2mdv2008.1.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>iText Tutorial: Extended header/footer support</title><meta name="Description" content="Using the extended header / footer support available in the RTF package."><meta name="Keywords" content="PDF, JAVA, iText, RTF, examples, Lowagie, Bruno"><link rel="stylesheet" href="./../../../style.css" type="text/css"></head><body><a name="top" class="logo" href="http://www.lowagie.com/iText"><img src="http://www.lowagie.com/iText/images/logo.gif" border="0" alt="iText"></a><h1>Tutorial: iText by Example</h1><h2>Extended header/footer support</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../../index.html#rtf_extensions_hf">
							Table of Contents
						</a><div align="Center" class="small">Best viewed with:<br><script type="text/javascript"><!--
google_ad_client = "pub-0340380473790570";
google_ad_width = 180;
google_ad_height = 60;
google_ad_format = "180x60_as_rimg";
google_cpa_choice = "CAAQyaj8zwEaCIwcWMzeycafKMu293M";
//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div><div class="sidetitle">Sections:</div><ul><li><a href="#extendedhf">Extended header/footer support</a></li><li><a href="#rtfhf">Creating more complex headers and footers</a></li><li><a href="#rtfhfgroup">Creating different headers and footers on differnet pages</a></li><li><a href="#chapterhf">Different headers per chapter</a></li></ul><br><br><div class="sidetitle">Examples:</div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ExtendedHeaderFooter.java">ExtendedHeaderFooter</a><br><div class="description">Generates an RTF document with more complex headers and footers</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ExtendedHeaderFooter.rtf">ExtendedHeaderFooter.rtf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/MultipleHeaderFooter.java">MultipleHeaderFooter</a><br><div class="description">Generates an RTF document with different headers/footers on different pages</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/MultipleHeaderFooter.rtf">MultipleHeaderFooter.rtf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ChapterHeaderFooter.java">ChapterHeaderFooter</a><br><div class="description">Generates an RTF document with different headers/footers per chapter</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ChapterHeaderFooter.rtf">ChapterHeaderFooter.rtf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../../rtf/extensions/hf/build.xml">
					build.xml
				</a></li></ul></div></div><div id="main"><a name="extendedhf"></a><div class="title">Extended header/footer support:</div><div xmlns="http://www.w3.org/1999/xhtml" xmlns:site="http://www.lowagie.com/iText/site">
		  Due to the limitations of the RTF format it is not possible to create more
		  complex headers and footers using page events as can be done when generating
		  PDF documents. To make it possible to create more complex headers and footers
		  than with the basic <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/HeaderFooter.html">HeaderFooter</a>
		  the RTF package provides three extensions:
		  
		  <ul xmlns="http://www.w3.org/1999/xhtml">
		    <li><a href="#rtfhf">Create more complex headers/footer</a> by placing any
		      <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Element.html">Element</a> in the
		      header or footer.</li>
		    <li xmlns="http://www.w3.org/1999/xhtml"><a href="#rtfhfgroup">Create different headers/footers</a> on the first,
		      left or right pages.</li>
		    <li><a href="#chapterhf">Create different headers/footers per chapter</a>.</li>
		  </ul>
		  
		  Of course these techniques can be combined to create different complex headers
		  on different pages in different chapters :-).
		</div><a class="top" href="#top">Go to top of the page</a><a xmlns="" name="rtfhf"></a><div class="title">Creating more complex headers and footers:</div><div xmlns="http://www.w3.org/1999/xhtml">
		  The first step is to create more complex headers and footers using the
		  <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/rtf/headerfooter/RtfHeaderFooter.html">RtfHeaderFooter</a>.
		  The RtfHeaderFooter class provides two public constructors that take either
		  a single <a href="http://itext.ugent.be/library/api/com/lowagie/text/Element.html">Element</a> or an
		  array of Elements. Using these constructors you can create a header with more
		  than one <a href="http://itext.ugent.be/library/api/com/lowagie/text/Paragraph.html">Paragraph</a>
		  
		  <pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">
// Create the Paragraphs that will be used in the header.
Paragraph date = new Paragraph("01.01.2010");
date.setAlignment(Paragraph.ALIGN_RIGHT);
Paragraph address = new Paragraph("TheFirm\nTheRoad 24, TheCity\n" +
    "+00 99 11 22 33 44");

// Create the RtfHeaderFooter with an array containing the Paragraphs to add
RtfHeaderFooter header = new RtfHeaderFooter(new Element[]{date, address});
            
// Set the header
document.setHeader(header);
		  </pre>
		  
		  another frequently used possibility is to use a <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Table.html">Table</a>
		  to create a header or footer with multiple columns
		  
		  <pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">
// Create the table that will be used as the footer
Table footer = new Table(2);
footer.setBorder(0);
footer.setDefaultCellBorder(0);
footer.setWidth(100);
footer.addCell(new Cell("(c) Mark Hall"));
Paragraph pageNumber = new Paragraph("Page ");
            
// The RtfPageNumber is an RTF specific element that adds a page number field
pageNumber.add(new RtfPageNumber());
pageNumber.setAlignment(Paragraph.ALIGN_RIGHT);
footer.addCell(new Cell(pageNumber));
            
// Create the RtfHeaderFooter and set it as the footer to use
document.setFooter(new RtfHeaderFooter(footer));
		  </pre>
		  
		  <div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ExtendedHeaderFooter.java">
						com.lowagie.examples.rtf.extensions.hf.ExtendedHeaderFooter</a><br>Generates an RTF document with more complex headers and footers: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ExtendedHeaderFooter.rtf">ExtendedHeaderFooter.rtf</a><br></div>
		</div><a class="top" href="#top">Go to top of the page</a><a name="rtfhfgroup"></a><div class="title">Creating different headers and footers on differnet pages:</div><div xmlns="http://www.w3.org/1999/xhtml">
		  In addition to creating more complex headers and footers it is also possible to
		  define multiple headers or footers that are displayed on different pages.
		  This approach uses the <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/rtf/headerfooter/RtfHeaderFooterGroup.html">RtfHeaderFooterGroup</a>
		  and allows for the following positions of headers or footers
		  
		  <ul xmlns="http://www.w3.org/1999/xhtml">
		    <li>RtfHeaderFooter.DISPLAY_ALL_PAGES</li>
		    <li>RtfHeaderFooter.DISPLAY_FIRST_PAGE</li>
		    <li>RtfHeaderFooter.DISPLAY_LEFT_PAGES</li>
		    <li>RtfHeaderFooter.DISPLAY_RIGHT_PAGES</li>
		  </ul>
		  
		  DISPLAY_ALL_PAGES can only be used by itself, DISPLAY_FIRST_PAGE, DISPLAY_LEFT_PAGES and DISPLAY_RIGHT_PAGES
		  can be combined to create different headers or footers on the respective pages.
		  
		  <pre class="commandline">
// Create the RtfHeaderGroup for the footer and set the footers
// at the desired positions
RtfHeaderFooterGroup footer = new RtfHeaderFooterGroup();
footer.setHeaderFooter(new RtfHeaderFooter(titleFooter), RtfHeaderFooter.DISPLAY_FIRST_PAGE);
footer.setHeaderFooter(new RtfHeaderFooter(leftFooter), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
footer.setHeaderFooter(new RtfHeaderFooter(rightFooter), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
            
// Set the document footer
document.setFooter(footer);
		  </pre>

          It is important to note that if you are creating a footer with different
          footers on different pages and you want to have the header on all pages
          except the first page, then you have to set this manually
          
          <pre class="commandline">
// Create the RtfHeaderFooterGroup for the header.
// To display the same header on both pages, but not the
// title page set them to left and right pages explicitly.
RtfHeaderFooterGroup header = new RtfHeaderFooterGroup();
header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
          </pre>
          
          This is not necessary if you want it to appear on all pages, then you can use
          DISPLAY_ALL_PAGES.<br><br>
          
          As the example shows you can combine the <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/rtf/headerfooter/RtfHeaderFooterGroup.html">RtfHeaderFooterGroup</a>
          with the <a href="http://itext.ugent.be/library/api/com/lowagie/text/rtf/headerfooter/RtfHeaderFooter.html">RtfHeaderFooter</a> to
          create complex headers or footers on different pages.
          
		  <div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/MultipleHeaderFooter.java">
						com.lowagie.examples.rtf.extensions.hf.MultipleHeaderFooter</a><br>Generates an RTF document with different headers/footers on different pages: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/MultipleHeaderFooter.rtf">MultipleHeaderFooter.rtf</a><br></div>
		</div><a class="top" href="#top">Go to top of the page</a><a name="chapterhf"></a><div class="title">Different headers per chapter:</div><div xmlns="http://www.w3.org/1999/xhtml">
		  The third method for displaying different headers or footers is to use 
		  <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chapter.html">Chapter</a>s and set
		  different headers or footers per <a href="http://itext.ugent.be/library/api/com/lowagie/text/Chapter.html">Chapter</a>.<br xmlns="http://www.w3.org/1999/xhtml"><br>
		  
		  The headers and footers for the first <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chapter.html">Chapter</a>
		  have to be set before the document is opened.
		  
		  <pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">
// Create the header identifying the current chapter. The first
// chapter has to be set before the document is opened.
Paragraph header = new Paragraph("Chapter 1");
header.setAlignment(Element.ALIGN_CENTER);
document.setHeader(new RtfHeaderFooter(header));
            
// If the footer (or header) is to be the same for all Chapters
// then it has to be set before the document is opened and is
// then automatically set for all Chapters.
document.setFooter(new HeaderFooter(new Phrase("This is page "), new Phrase(".")));
		  </pre>
		  
		  Then after adding each chapter optionally create and set the new headers or footers
		  for the next <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chapter.html">Chapter</a>.
		  
		  <pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">
// After adding the first chapter set the header for the second chapter.
header = new Paragraph("Chapter 2");
header.setAlignment(Element.ALIGN_CENTER);
document.setHeader(new RtfHeaderFooter(header));

Chapter chapter2 = new Chapter("Chapter 2", 2);
chapter2.add(new Paragraph("This is the content of chapter 2."));
document.add(chapter2);
		  </pre>
		  
		  This process is then repeated for each additional <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chapter.html">Chapter</a>
		  to add. If you don't set a header or footer before adding a <a href="http://itext.ugent.be/library/api/com/lowagie/text/Chapter.html">Chapter</a>
		  then the header and footer from the previous <a href="http://itext.ugent.be/library/api/com/lowagie/text/Chapter.html">Chapter</a>
		  are used.

		  <div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ChapterHeaderFooter.java">
						com.lowagie.examples.rtf.extensions.hf.ChapterHeaderFooter</a><br>Generates an RTF document with different headers/footers per chapter: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/rtf/extensions/hf/ChapterHeaderFooter.rtf">ChapterHeaderFooter.rtf</a><br></div>
		</div><a class="top" href="#top">Go to top of the page</a><div id="footer" xmlns="http://www.w3.org/1999/xhtml">
			Page Updated: 2006/10/05 21:25:54
			Copyright &copy; 1999-2005
			Mark Hall<br><a href="http://www.lowagie.com/iText/">iText</a> is a Free Java-Pdf library by Bruno Lowagie and Paulo Soares.
		</div></div></div><div class="commercial"><br><script type="text/javascript"><!--
google_ad_client = "pub-0340380473790570";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel ="";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "1B09BD";
google_color_url = "100670";
google_color_text = "707070";
//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><br><br><div class="subtitle">Amazon books:</div><script type="text/javascript"><!--
document.write('<iframe src="http://rcm.amazon.com/e/cm?t=itisacatalofwebp&o=1&p=8&l=as1&asins=0596004753&fc1=000000&lc1=0000ff&bc1=&lt1=_blank&IS2=1&bg1=ffffff&f=ifr" width="120" height="240" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" align="Center"></iframe>');
//--></script><a xmlns="" href="http://www.amazon.co.uk/exec/obidos/ASIN/0596004753/catloogjecom-21" class="amazonlinks">
			amazon.co.uk-link
		</a><br><br><script type="text/javascript"><!--
document.write('<iframe src="http://rcm.amazon.com/e/cm?t=itisacatalofwebp&o=1&p=10&l=st1&mode=books&search=JAVA&=1&fc1=&lc1=&lt1=&bg1=&f=ifr" width="120" height="460" border="0" frameborder="0" style="border:none;" scrolling="no" marginwidth="0" marginheight="0"></iframe>');
//--></script><br><script type="text/javascript"><!--
document.write('<iframe src="http://rcm.amazon.com/e/cm?t=itisacatalofwebp&o=1&p=10&l=st1&mode=books&search=RTF&=1&fc1=&lc1=&lt1=&bg1=&f=ifr" width="120" height="460" border="0" frameborder="0" style="border:none;" scrolling="no" marginwidth="0" marginheight="0"></iframe>');
//--></script><br></div></body></html>