Java & J2EE Page 4 - Adding Images With iTextSharp |
Let's put together what we've covered so far in a short example. In this example, we'll create something that looks like this: Note that this image is scaled down. We'll use the ASP Free logo. Download it and place it in a directory in which the example will execute (the output directory of the Visual Studio project-you can add it to the project and set it to be copied to the output directory in the Properties Window): In order to achieve the result we want, we'll need to create three instances of the image. All three will have to be rotated and will need borders. Two will need to be positioned manually and will be located beneath the text. The third will have text wrapped around it and will be positioned automatically. It will also be enlarged. using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; class Images1 { public static void Main() { string text = "This is a paragraph. It is represted" + " by a Paragraph object in the iTextSharp " + "library. Here, we're creating paragraphs with " + "various styles in order to test out iTextSharp." + " This paragraph will take up multiple lines " + "and allow for a more complete example."; Document doc = new Document(PageSize.LETTER, 72, 72, 72, 72); PdfWriter.GetInstance(doc, new FileStream("images1.pdf", FileMode.Create)); doc.Open(); // Get instances of the images string imagePath = Path.Combine( System.Environment.CurrentDirectory, "aspfreelogo.JPG"); Image logoLeft = Image.GetInstance(imagePath); Image logoBottom = Image.GetInstance(imagePath); Image logoRight = Image.GetInstance(imagePath); // Add some text doc.Add(new Paragraph(text)); doc.Add(new Paragraph(text)); // Add the top-left image logoLeft.Alignment = Image.UNDERLYING; logoLeft.Rotation = (float)Math.PI / -3; logoLeft.Border = Image.RIGHT_BORDER; logoLeft.BorderWidth = 1; logoLeft.BorderColor = Color.RED; logoLeft.SetAbsolutePosition(72, 72 * 9); doc.Add(logoLeft); // Add the top-right image logoRight.Alignment = Image.UNDERLYING; logoRight.Rotation = (float)Math.PI / 3; logoRight.Border = Image.LEFT_BORDER; logoRight.BorderWidth = 1; logoRight.BorderColor = Color.BLUE; logoRight.SetAbsolutePosition(72 * 4, 72 * 9); doc.Add(logoRight); // Add the bottom-left image // (using degrees to rotate it!) logoBottom.ScalePercent(150); logoBottom.Alignment = Image.TEXTWRAP; logoBottom.RotationDegrees = 45; logoBottom.Border = Image.RIGHT_BORDER; logoBottom.BorderWidth = 1; logoBottom.BorderColor = Color.GREEN; doc.Add(logoBottom); // Add some more text doc.Add(new Paragraph(text)); doc.Close(); } } Nothing in the above example should be unfamiliar to you. We've covered everything there is, and each section of the code includes a comment. That concludes basic image addition and transformation with the iTextSharp library. Now, you should be able to add images to a PDF document with spacing, borders, and so forth. Images can be placed automatically or manually. The images can also be transformed according to your needs.
blog comments powered by Disqus |
|
|
|
|
|
|
|