| 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: | /// Text at absolute positions | |
| 11: | /// </summary> | |
| 12: | public class Chap1002 | |
| 13: | { | |
| 14: | ||
| 15: | public Chap1002() | |
| 16: | { | |
| 17: | ||
| 18: | Console.WriteLine("Chapter 10 example 2: Text at absolute positions"); | |
| 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("Chap1002.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: | // first we draw some lines to be able to visualize the text alignment functions | |
| 38: | cb.SetLineWidth(0f); | |
| 39: | cb.MoveTo(250, 500); | |
| 40: | cb.LineTo(250, 800); | |
| 41: | cb.MoveTo(50, 700); | |
| 42: | cb.LineTo(400, 700); | |
| 43: | cb.MoveTo(50, 650); | |
| 44: | cb.LineTo(400, 650); | |
| 45: | cb.MoveTo(50, 600); | |
| 46: | cb.LineTo(400, 600); | |
| 47: | cb.Stroke(); | |
| 48: | ||
| 49: | // we tell the ContentByte we're ready to draw text | |
| 50: | cb.BeginText(); | |
| 51: | ||
| 52: | BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); | |
| 53: | cb.SetFontAndSize(bf, 12); | |
| 54: | String text = "Sample text for alignment"; | |
| 55: | // we show some text starting on some absolute position with a given alignment | |
| 56: | cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, 700, 0); | |
| 57: | cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, 650, 0); | |
| 58: | cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, 600, 0); | |
| 59: | ||
| 60: | // we draw some text on a certain position | |
| 61: | cb.SetTextMatrix(100, 400); | |
| 62: | cb.ShowText("Text at position 100,400."); | |
| 63: | ||
| 64: | // we draw some rotated text on a certain position | |
| 65: | cb.SetTextMatrix(0, 1, -1, 0, 100, 300); | |
| 66: | cb.ShowText("Text at position 100,300, rotated 90 degrees."); | |
| 67: | ||
| 68: | // we draw some mirrored, rotated text on a certain position | |
| 69: | cb.SetTextMatrix(0, 1, 1, 0, 200, 200); | |
| 70: | cb.ShowText("Text at position 200,200, mirrored and rotated 90 degrees."); | |
| 71: | ||
| 72: | // we tell the contentByte, we've finished drawing text | |
| 73: | cb.EndText(); | |
| 74: | } | |
| 75: | catch(DocumentException de) | |
| 76: | { | |
| 77: | Console.Error.WriteLine(de.Message); | |
| 78: | } | |
| 79: | catch(IOException ioe) | |
| 80: | { | |
| 81: | Console.Error.WriteLine(ioe.Message); | |
| 82: | } | |
| 83: | ||
| 84: | // step 5: we close the document | |
| 85: | document.Close(); | |
| 86: | } | |
| 87: | } | |
| 88: | } |