| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | ||
| 4: | using iTextSharp.text; | |
| 5: | using iTextSharp.text.rtf; | |
| 6: | ||
| 7: | namespace iTextSharp.tutorial.Chap08 | |
| 8: | { | |
| 9: | /// <summary> | |
| 10: | /// This example creates a RTF document with two chapters and different headers | |
| 11: | /// for both chapters. The same works for footers. Just replace setHeader with setFooter. | |
| 12: | /// @author <a href="mailto:mhall@myrealbox.com">Mark.Hall@myrealbox.com</a> | |
| 13: | /// </summary> | |
| 14: | public class Chap0802 | |
| 15: | { | |
| 16: | ||
| 17: | public Chap0802() | |
| 18: | { | |
| 19: | ||
| 20: | Console.WriteLine("Chapter 8 example 2: Headers in RTF"); | |
| 21: | ||
| 22: | // step 1: creation of a document-object | |
| 23: | Document document = new Document(); | |
| 24: | ||
| 25: | try | |
| 26: | { | |
| 27: | ||
| 28: | // step 2: | |
| 29: | // we create a writer that listens to the document | |
| 30: | // and directs a PDF-stream to a file | |
| 31: | RtfWriter.GetInstance(document, new FileStream("Chap0802.rtf", FileMode.Create)); | |
| 32: | ||
| 33: | // step 3: we open the document | |
| 34: | document.Open(); | |
| 35: | ||
| 36: | // step 4: we create two chapters and add the same content to both. | |
| 37: | Paragraph par = new Paragraph("This is some sample content."); | |
| 38: | Chapter chap1 = new Chapter("Chapter 1", 1); | |
| 39: | chap1.Add(par); | |
| 40: | Chapter chap2 = new Chapter("Chapter 2", 2); | |
| 41: | chap2.Add(par); | |
| 42: | ||
| 43: | // step 5: we create the header for the first chapter, set the header and | |
| 44: | // then add the first chapter. | |
| 45: | HeaderFooter hf1 = new HeaderFooter(new Phrase("This is chapter 1"), false); | |
| 46: | document.Header = hf1; | |
| 47: | document.Add(chap1); | |
| 48: | ||
| 49: | // step 6: we create a second header, set this one and then add the second | |
| 50: | // chapter. | |
| 51: | HeaderFooter hf2 = new HeaderFooter(new Phrase("This is chapter 2"), false); | |
| 52: | document.Header = hf2; | |
| 53: | document.Add(chap2); | |
| 54: | } | |
| 55: | catch(DocumentException de) | |
| 56: | { | |
| 57: | Console.Error.WriteLine(de.Message); | |
| 58: | } | |
| 59: | catch(IOException ioe) | |
| 60: | { | |
| 61: | Console.Error.WriteLine(ioe.Message); | |
| 62: | } | |
| 63: | ||
| 64: | // step 7: we close the document | |
| 65: | document.Close(); | |
| 66: | } | |
| 67: | } | |
| 68: | } |