I'm having trouble centering elements while in landscape mode. If I change to portrait everything works fine. Here's what I'm doing.
Dim MyPage As ceTe.DynamicPDF.Page = New ceTe.DynamicPDF.Page(PageSize.Letter, PageOrientation.Landscape, 54.0F)
Dim MyLabel As Label = New Label("test me", 0, 0, 512, 14, Font.Helvetica, 14, TextAlign.Center, RgbColor.Blue)
MyPage.Elements.Add(MyLabel)
etc....
This code results in the element being pushed over from the left but not truly centered.
Center Elements in Landscape Mode
Re: Center Elements in Landscape Mode
Forum member · Mar 7, 2008Well I found a work arround... I just take the diference of the width of the page in landscape (792) and the width in portrait (612) and divide by 2. Use this value as the x coordinate rather than 0. Seems to work.
Private Const landscapeCenterBugFixXCord As Single = (792 - 612) / 2
Dim MyLabel As Label = New Label(Text, landscapeCenterBugFixXCord, 0, 512, 14, Font.Helvetica, 14, TextAlign.Center, RgbColor.Blue)
Re: Center Elements in Landscape Mode
Support · Mar 7, 2008Actually your issue is quite simple. In your original code you are using a value of 512 for the Width of the Label. This works perfectly when your Page is in Portrait mode (because the Width of a Portrait page is 612 subtract margins of 50 on each side and everything is perfectly centered).
However when you change the Page to Landscape, the Width of the Page is now 792. You however did not make the appropriate adjustment to the Width of your Label. In order for the Label to always be centered in the Page you will want the Label width to equal the Page Width minus the left and right margins. In your case changing the Label Width from 512 to 692 will correct this.
To see this concept just place a Rectangle object around your Label object by using the following code:
Dim MyDocument As Document = New Document()
Dim MyPage As Page = New Page(PageSize.Letter, PageOrientation.Landscape, 54.0F)
MyDocument.Pages.Add(MyPage)
Dim MyLabel As Label = New Label("test me", 0, 0, 512, 14, Font.Helvetica, 14, TextAlign.Center, RgbColor.Blue)
Dim MyRectangle As Rectangle = New Rectangle(0, 0, 512, 14)
MyPage.Elements.Add(MyLabel)
MyPage.Elements.Add(MyRectangle)
MyDocument.DrawToWeb()
You notice that the Label (and the Rectangle) is just the wrong size.
By my calculation, the alternative code you provided does not exactly fix the issue. Again if you just use a Rectangle around the Label you can see this:
Dim MyDocument As Document = New Document()
Dim MyPage As Page = New Page(PageSize.Letter, PageOrientation.Landscape, 54.0F)
MyDocument.Pages.Add(MyPage)
Const landscapeCenterBugFixXCord As Single = (792 - 612) / 2
Dim MyLabel As Label = New Label("test me", landscapeCenterBugFixXCord, 0, 692, 14, Font.Helvetica, 14, TextAlign.Center, RgbColor.Blue)
Dim MyRectangle As Rectangle = New Rectangle(landscapeCenterBugFixXCord, 0, 692, 14)
MyPage.Elements.Add(MyLabel)
MyPage.Elements.Add(MyRectangle)
MyDocument.DrawToWeb()
Anyway, that is the very long and expanded upon explanation of what is going on. The short answer is change the Width of your Label from 512 to 692.
Thanks,
ceTe Software Support Team
Re: Center Elements in Landscape Mode
Forum member · Mar 7, 2008Thanks, you're right, my fix wasn't a fix now that I look more closely.
The new label width solves my problem.
Thanks again.