| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | ||
| 4: | using iTextSharp.text; | |
| 5: | using iTextSharp.text.pdf; | |
| 6: | ||
| 7: | namespace iTextSharp.tutorial.Chap06 | |
| 8: | { | |
| 9: | /// <summary> | |
| 10: | /// Scaling and Rotating the Image: | |
| 11: | /// Scaling | |
| 12: | /// There are several possibilities to scale an image: | |
| 13: | /// </summary> | |
| 14: | /// <remarks> | |
| 15: | /// Impact on the resolution | |
| 16: | /// If an image is placed without any scaling the the resolution will be 72. | |
| 17: | /// If an image is placed with a scaling of 50% the the resolution will be 144. | |
| 18: | /// With lower scalings the resolution will increase because the pixels are the same but the size will be smaller. | |
| 19: | /// To put a picture with 300dpi use a scaling of 72/300=24%. | |
| 20: | /// For instance: if you have a 5X5 inch image that you scan at 300 dpi, the resultant image is 1500X1500 pixels (5X300 = 1500). | |
| 21: | /// When you place this image in the pdf with a scaling of 24% (72/300 = 0.24), | |
| 22: | /// the image in the pdf will be 5X5 inch with 1500X1500 pixel at 300 dpi. | |
| 23: | /// The image will always be 1500X1500 pixel whatever the size. | |
| 24: | </remarks> | |
| 25: | public class Chap0607 | |
| 26: | { | |
| 27: | ||
| 28: | public Chap0607() | |
| 29: | { | |
| 30: | ||
| 31: | Console.WriteLine("Chapter 6 example 7: Scaling an Image"); | |
| 32: | ||
| 33: | // step 1: creation of a document-object | |
| 34: | Document document = new Document(); | |
| 35: | ||
| 36: | try | |
| 37: | { | |
| 38: | ||
| 39: | // step 2: | |
| 40: | // we create a writer that listens to the document | |
| 41: | // and directs a PDF-stream to a file | |
| 42: | ||
| 43: | PdfWriter.GetInstance(document, new FileStream("Chap0607.pdf", FileMode.Create)); | |
| 44: | ||
| 45: | // step 3: we open the document | |
| 46: | document.Open(); | |
| 47: | ||
| 48: | // step 4: we add content | |
| 49: | Image jpg1 = Image.GetInstance("myKids.jpg"); | |
| 50: | jpg1.ScaleAbsolute(97, 101); | |
| 51: | document.Add(new Paragraph("scaleAbsolute(97, 101)")); | |
| 52: | document.Add(jpg1); | |
| 53: | Image jpg2 = Image.GetInstance("myKids.jpg"); | |
| 54: | jpg2.ScalePercent(50); | |
| 55: | document.Add(new Paragraph("scalePercent(50)")); | |
| 56: | document.Add(jpg2); | |
| 57: | Image jpg3 = Image.GetInstance("myKids.jpg"); | |
| 58: | jpg3.ScaleAbsolute(194, 101); | |
| 59: | document.Add(new Paragraph("scaleAbsolute(194, 101)")); | |
| 60: | document.Add(jpg3); | |
| 61: | Image jpg4 = Image.GetInstance("myKids.jpg"); | |
| 62: | jpg4.ScalePercent(100, 50); | |
| 63: | document.Add(new Paragraph("scalePercent(100, 50)")); | |
| 64: | document.Add(jpg4); | |
| 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: | } |