Is there a way to center an image on a page? The image size will differ from client to client so I can't hardcode it. I've tried putting the imagine into a table but the align property seems to only work for text. It seems the x,y property on the Image overrides the align property. Here's my vb.net code
Dim MyTable As Table = New Table(18, 130, 700, 200)
MyTable.Columns.Add(1)
Dim MyRow1 As Row = MyTable.Rows.Add(1, Font.HelveticaBold, 14, Color.White, Color.White)
MyRow1.Cells.Add(New Image(MapPath("../image/Logo.jpg"), 0, 0, 1.0F))
MyRow1.Align = TextAlign.Center
MyRow1.VAlign = VAlign.Top
CurrentPage.Elements.Add(MyTable)
Thanks
Centering an Image
Re:Centering an Image
Support · Nov 7, 2005Centering an Image on a page is actually very simple. You do not need to use the Table object at all. You will just need to dynamically find the center of the page and set the image to vertical and horizontal alignment at that point. VB.NET code for this will look something like this:
Dim MyDocument As Document = New Document()
Dim MyPage As Page = New Page()
Dim centerX As Integer = (MyPage.Dimensions.Width - MyPage.Dimensions.LeftMargin - MyPage.Dimensions.RightMargin) / 2
Dim centerY As Integer = (MyPage.Dimensions.Height - MyPage.Dimensions.TopMargin - MyPage.Dimensions.BottomMargin) / 2
Dim MyImage As Image = New Image(filePath, centerX, centerY)
MyImage.VAlign = VAlign.Center
MyImage.Align = Align.Center
MyPage.Elements.Add(MyImage)
MyDocument.Pages.Add(MyPage)
Typically the Image object's X and Y coordinates are positioned from the top left corner of the image. Setting the Image's Align and VAlign properties to center then the X and Y location specified will actually be positioned from the center of the image. This simplifies the calculations needed to get the image in the center of the page.
The above code will always work no matter what size your image is and no matter what size you page is. The alignment is calculated completely dynamically.
If you know the page size and margins ahead of time then you can hard code the centerX and centerY values. But the code above will work for any page size.
Thanks,
ceTe Software Support Team
Re: Centering an Image
Forum member · Jan 27, 2006I'm new to DynamicPDF Generator, but I just happened to do this the other day and used a different method to center my image and I thought I would share it with everyone. It is using the page body attributes very similar to the example already given, but it uses less statements. This example centers the image in the middle of the page. I'm not saying this is better - just different.
This is from my C# code:
ImageData myImg = ImageData.GetImage("MyImg.gif");
page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(myImg, (page.Dimensions.Body.Width - myImg.Width) / 2, (page.Dimensions.Body.Height - myImg.Height) / 2, 1.0f));
Hope you guys don't mind me chiming in.
Re: Centering an Image
Forum member · Feb 17, 2006Hi
I also found a short method. First you add the image to the page elements, then you adjust the X coordinate. Here goes:
Dim logo As ceTe.DynamicPDF.PageElements.Image = New Image(logoPath, 0, 708)
logo.X = (MyPage.Dimensions.Width - logo.Width) / 2