| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | ||
| 4: | using iTextSharp.text; | |
| 5: | using iTextSharp.text.pdf; | |
| 6: | ||
| 7: | namespace iTextSharp.tutorial.Chap10 | |
| 8: | { | |
| 9: | /// <summary> | |
| 10: | /// Templates | |
| 11: | /// </summary> | |
| 12: | public class Chap1003 | |
| 13: | { | |
| 14: | ||
| 15: | public Chap1003() | |
| 16: | { | |
| 17: | ||
| 18: | Console.WriteLine("Chapter 10 example 3: Templates"); | |
| 19: | ||
| 20: | // step 1: creation of a document-object | |
| 21: | Document document = new Document(); | |
| 22: | ||
| 23: | try | |
| 24: | { | |
| 25: | ||
| 26: | // step 2: | |
| 27: | // we create a writer that listens to the document | |
| 28: | // and directs a PDF-stream to a file | |
| 29: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1003.pdf", FileMode.Create)); | |
| 30: | ||
| 31: | // step 3: we open the document | |
| 32: | document.Open(); | |
| 33: | ||
| 34: | // step 4: we grab the ContentByte and do some stuff with it | |
| 35: | PdfContentByte cb = writer.DirectContent; | |
| 36: | ||
| 37: | // we create a PdfTemplate | |
| 38: | PdfTemplate template = cb.CreateTemplate(500, 200); | |
| 39: | ||
| 40: | // we add some graphics | |
| 41: | template.MoveTo(0, 200); | |
| 42: | template.LineTo(500, 0); | |
| 43: | template.Stroke(); | |
| 44: | template.SetRGBColorStrokeF(255f, 0f, 0f); | |
| 45: | template.Circle(250f, 100f, 80f); | |
| 46: | template.Stroke(); | |
| 47: | ||
| 48: | // we add some text | |
| 49: | template.BeginText(); | |
| 50: | BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); | |
| 51: | template.SetFontAndSize(bf, 12); | |
| 52: | template.SetTextMatrix(100, 100); | |
| 53: | template.ShowText("Text at the position 100,100 (relative to the template!)"); | |
| 54: | template.EndText(); | |
| 55: | ||
| 56: | // we add the template on different positions | |
| 57: | cb.AddTemplate(template, 0, 0); | |
| 58: | cb.AddTemplate(template, 0, 1, -1, 0, 500, 200); | |
| 59: | cb.AddTemplate(template, .5f, 0, 0, .5f, 100, 400); | |
| 60: | ||
| 61: | // we go to a new page | |
| 62: | document.NewPage(); | |
| 63: | cb.AddTemplate(template, 0, 400); | |
| 64: | cb.AddTemplate(template, 2, 0, 0, 2, -200, 400); | |
| 65: | } | |
| 66: | catch(DocumentException de) | |
| 67: | { | |
| 68: | Console.Error.WriteLine(de.Message); | |
| 69: | } | |
| 70: | catch(IOException ioe) | |
| 71: | { | |
| 72: | Console.Error.WriteLine(ioe.Message); | |
| 73: | } | |
| 74: | ||
| 75: | // step 5: we close the document | |
| 76: | document.Close(); | |
| 77: | } | |
| 78: | } | |
| 79: | } |