Image Align

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for .NET (v5.x)  /  Display Thread

DynamicPDF Generator for .NET (v5.x) Forum

 Oct 21 2009 12:17 PM
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.
 Oct 21 2009 12:37 PM
Posted by a ceTe Software moderator
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.
 Oct 21 2009 2:17 PM
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);
 Oct 21 2009 3:19 PM
Posted by a ceTe Software moderator
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
 Feb 15 2017 7:21 AM
Hello,

Does the API changed since ?
Because this code doesn't work anymore...

Thanks for your help !
 Feb 15 2017 11:14 AM
Posted by a ceTe Software moderator
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 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.
 Oct 11 2019 9:19 AM
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
 Oct 11 2019 1:01 PM
Posted by a ceTe Software moderator
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
 Oct 12 2019 6:48 PM
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);
    
            }
 Oct 14 2019 9:50 AM
Posted by a ceTe Software moderator
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 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
 Oct 14 2019 9:50 AM
Posted by a ceTe Software moderator
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 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
 Oct 15 2019 10:47 AM
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

 Oct 15 2019 12:37 PM
Posted by a ceTe Software moderator
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

All times are US Eastern Standard time. The time now is 9:05 AM.