Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.rtf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap08
9:   {
10:       /// <summary>
11:       /// This example creates a RTF document with more complex headers and footers
12:       /// using the RtfHeaderFooters class.
13:       /// @author <a href="mailto:mhall@myrealbox.com">Mark.Hall@myrealbox.com</a>
14:       /// </summary>
15:       public class Chap0803 
16:       {
17:  
18:           public  Chap0803() 
19:           {
20:  
21:               Console.WriteLine("Chapter 8 example 3: RTF with the RtfHeaderFooters class");
22:  
23:               /* Create a new document */
24:               Document document new Document(PageSize.A4);
25:               try 
26:               {
27:                   /* Create a RtfWriter and a PdfWriter for the document */
28:                   RtfWriter rtf RtfWriter.GetInstance(documentnew FileStream("Chap0803.rtf"FileMode.Create));
29:  
30:                   /* We specify that the RTF file has a Title Page */                
31:                   rtf.SetHasTitlePage(true);
32:  
33:                   /* We create headers and footers for the RTF file */
34:                   RtfHeaderFooters header new RtfHeaderFooters();
35:                   RtfHeaderFooters footer new RtfHeaderFooters();
36:  
37:                   /* We add a header that will only appear on the first page */
38:                   header.Set(RtfHeaderFooters.FIRST_PAGEnew HeaderFooter(new Phrase("This header is only on the first page"), false));
39:                   /* We add a header that will only appear on left-side pages */
40:                   header.Set(RtfHeaderFooters.LEFT_PAGESnew HeaderFooter(new Phrase("This header is only on left pages"), false));
41:                   /* We add a header that will only appear on right-side pages */
42:                   header.Set(RtfHeaderFooters.RIGHT_PAGESnew HeaderFooter(new Phrase("This header is only on right pages. "), false));
43:                   /* We add a footer that will appear on all pages except the first (because of the title page)
44:                      Because the header has different left and right page footers, we have to add the footer to
45:                      both the left and right pages. */
46:                   footer.Set(RtfHeaderFooters.LEFT_PAGESnew HeaderFooter(new Phrase("This footer is on all pages except the first. Page: "), true));
47:                   footer.Set(RtfHeaderFooters.RIGHT_PAGESnew HeaderFooter(new Phrase("This footer is on all pages except the first. Page: "), true));
48:  
49:                   /* Open the document */
50:                   document.Open();
51:  
52:  
53:                   /* We add the header and footer */
54:                   document.Header header;
55:                   document.Footer footer;
56:  
57:  
58:                   /* We add some content */
59:                   Chapter chapter new Chapter(new Paragraph("Advanced RTF headers and footers"new Font(Font.HELVETICA16Font.BOLD)), 1);
60:  
61:                   chapter.Add(new Paragraph("This document demonstrates the use of advanced RTF headers and footers."));
62:  
63:                   for(int 0300i++) 
64:                   {
65:                       chapter.Add(new Paragraph("Line " i));
66:                   }
67:                   document.Add(chapter);
68:               }
69:               catch(DocumentException de
70:               {
71:                   Console.Error.WriteLine(de.Message); }
72:               catch(IOException ioe
73:               {
74:                   Console.Error.WriteLine(ioe.Message); }
75:  
76:               /* Close the document */
77:               document.Close();
78:           }
79:       }
80:   }