Image Align

Image Align

Forum member · Oct 21, 2009
Hi, I need some help with displaying image in the center of the page. My page width is 500 float points and height is 700 float points. I tried both Align.Center and Valign.Center but the picture doesn't align in center. Here's my code: PageElements.Image bitmapImage = new PageElements.Image(newImage, 0, 20); bitmapImage.VAlign = ceTe.DynamicPDF.VAlign.Center; Thanks, John.

Re: Image Align

Support · Oct 21, 2009
Hello John, Yes, you can place the image in the middle of the page and below is a sample code for this. Document document = new Document(); Page page = new Page(); float centerX = (page.Dimensions.Width - page.Dimensions.LeftMargin - page.Dimensions.RightMargin) / 2; float centerY = (page.Dimensions.Height - page.Dimensions.TopMargin - page.Dimensions.BottomMargin) / 2; Image MyImage = new Image(@"D:\Attachments\Images\logo.tif", centerX, centerY); MyImage.VAlign = VAlign.Center; MyImage.Align = Align.Center; page.Elements.Add(MyImage); document.Pages.Add(page); document.DrawToWeb(); Thanks, ceTe Software Support Team.

Re: Image Align

Forum member · Oct 21, 2009
Hi, Thanks for your reply. It worked out. I have one more question. I want to display image caption after the image. How do I find what is the end of the image (end Y) on the page? I tried image.Height + 15 as Y coordinate for formattedImageText but it doesn't work. float centerX = ContentWidth / 2; float centerY = ContentHeight / 2 ; PageElements.Image scaledImage = new PageElements.Image(newImage, centerX, centerY); scaledImage.VAlign = ceTe.DynamicPDF.VAlign.Center; scaledImage.Align = ceTe.DynamicPDF.Align.Center; picGroup.Add(scaledImage); float imgHeight = newImage.Height; PageElements.FormattedTextArea formattedimageText = new PageElements.FormattedTextArea(imageText, 0, imgHeight + 15, ContentWidth, 0, imageStyle); formattedimageText.Style.Paragraph.Align = ceTe.DynamicPDF.TextAlign.Center; float imageTextHeight = formattedimageText.GetRequiredHeight(); formattedimageText.Height = imageTextHeight; picGroup.Add(formattedimageText);

Re: Image Align

Support · Oct 21, 2009
Hello, You can calculate the Y coordinate of where the image ended using the starting Y coordinate where you place the image and the height of the image. Thansk, ceTe Software Support Team

Re: Image Align

Forum member · Feb 15, 2017
Hello, Does the API changed since ? Because this code doesn't work anymore... Thanks for your help !

Re: Image Align

Support · Feb 15, 2017
Hello, The code posted above is for v5 and we tested using the v5.1.2.15141 DLL and we are able to place the image correctly in the middle of the page using code sample posted above. Please send over the following details to [^a|mailto:support@cete.com|opennewwindow|support@cete.com^] so we can look into it further. 1.Code sample which uses static data to recreate the behaviour. 2.Output PDF. 3.Image file. 4.Exact version and build number of the DynamicPDF DLL file. You can get this information in DLL references properties (Version and Description fields) in Visual Studio. Thanks, ceTe Software Support Team.

Re: Image Align

Forum member · Oct 11, 2019
Hi, This Code is not working. It is not placing the image in the center of the document. However, in my project the text align is working fine. float centerX = (page.Dimensions.Width - page.Dimensions.LeftMargin - page.Dimensions.RightMargin) / 2; var image1 = new ceTe.DynamicPDF.PageElements.Image(Image1, centerX, 100, 0.5f); image1.Align = Align.Center; page.Elements.Add(image1); Thanks

Re: Image Align

Support · Oct 11, 2019
Hello, In order to add an image in the center of the page(horizontally), you will need to calculate the X position as per the page and image dimensions. Set the calculated X position to the image and see it works for you. Below is the code sample. Document document = new Document(); Page page = new Page(); document.Pages.Add(page); Image image = new Image(@"input image file path",0,0,0.5F); float pageMidPoint = page.Dimensions.Body.Width / 2; float imageMidPoint = image.Width / 2; float newXPosition = pageMidPoint - imageMidPoint; image.X = newXPosition; page.Elements.Add(image); string output = @"C:\Temp\MyDocument.pdf"; document.Draw(output); Thanks, ceTe Software Support Team

Re: Image Align

Forum member · Oct 12, 2019
That actually moves it to the left way more.... cutting a lot of the picture off of the document. if (!string.IsNullOrEmpty(Image1)) { var image1 = new ceTe.DynamicPDF.PageElements.Image(Image1, 0, 100, 0.5f); image1.Align = Align.Center; float pageMidPoint = page.Dimensions.Body.Width / 2; float imageMidPoint = image1.Width / 2; float newXPosition = pageMidPoint - imageMidPoint; image1.X = newXPosition; page.Elements.Add(image1); }

Re: Image Align

Support · Oct 14, 2019
Hello, Please try commenting out the line of code which is setting Alignment to image (image1.VAlign = VAlign.Center;) and see it works for you. If you continue facing any issues then please send over the following information to [^a|mailto:support@dynamicpdf.com|opennewwindow|support@dynamicpdf.com^] so we can look into it further. 1. Image used for adding to the PDF. 2. Code sample. 3. Output PDF. 4. Exact version and build number of the DynamicPDF DLL file used in your application. You can get this information in DLL references properties (Version and Description fields) in Visual Studio. Thanks, ceTe Software Support Team

Re: Image Align

Support · Oct 14, 2019
Hello, Please try commenting out the line of code which is setting Alignment to image (image1.VAlign = VAlign.Center;) and see it works for you. If you continue facing any issues then please send over the following information to [^a|mailto:support@dynamicpdf.com|opennewwindow|support@dynamicpdf.com^] so we can look into it further. 1. Image used for adding to the PDF. 2. Code sample. 3. Output PDF. 4. Exact version and build number of the DynamicPDF DLL file used in your application. You can get this information in DLL references properties (Version and Description fields) in Visual Studio. Thanks, ceTe Software Support Team

Re: Image Align

Forum member · Oct 15, 2019
So, still an issue but I think I found the problem. In the line of code: float imageMidPoint = image1.Width / 2; Is the "Width" unit of measure "points" ? I am assuming page.Dimensions.Width is in points. (Where 72 points = 1 inch) Thanks

Re: Image Align

Support · Oct 15, 2019
Hello, Yes, the DynamicPDF Generator for .NET uses a coordinate system based on points. Please refer to the documentation on coordinate system here . Thanks, ceTe Software Support Team

Re: Image Align

Forum member · Oct 21, 2019
With this line of code: var headingText = new Label(HeadingText, 5, 10, page.Dimensions.Width - 10, 20, HeadingTextFont, HeadingTextPoints, HeadingTextColor); Where headingText is just "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" It puts the first "W" just about in the center of the document.... instead of 5pts from the left like I was expecting. Thanks

Re: Image Align

Support · Oct 22, 2019
Hello, We did testing on our end and are able to add the label at a specified position(X=5 and Y=10). Below is the code sample used for our testing. Please note that the DynamicPDF API adds the page element with respect to the left and top margins set to the page. Also you can add a LayoutGrid to know the positions of the page elements. Document document = new Document(); Page page = new Page(); document.Pages.Add(page); Font HeadingTextFont = Font.Helvetica; float HeadingTextPoints = 13; RgbColor HeadingTextColor = RgbColor.Blue; string HeadingText = "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"; Label label = new Label(HeadingText, 5, 10, page.Dimensions.Width - 10, 20, HeadingTextFont, HeadingTextPoints, HeadingTextColor); page.Elements.Add(new LayoutGrid()); page.Elements.Add(label); document.Draw(@"C:\Temp\MyDocument.pdf"); Also below is the code sample to add the text at the center of the page (horizontally) using Label page element. string HeadingText = "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"; Label label = new Label(HeadingText, 0, 10, page.Dimensions.Body.Width, 20, HeadingTextFont, HeadingTextPoints, HeadingTextColor); label.Align = TextAlign.Center; If you continue facing any issues then please send over the following information to [^a|mailto:support@dynamicpdf.com|opennewwindow|support@dynamicpdf.com^] so we can look into it further. 1. Code sample which uses static data to recreate the behavior. 2. Output PDF. 3. Exact version and build number of the DynamicPDF DLL file used in the application. You can get this information in DLL references properties (Version and Description fields) in Visual Studio. Thanks, ceTe Software support Team