Free Support Forum - aspose.com

Asbolute position Image within bounding box and centered


#1

Hello, I am trying to place a photo on a one page PDF. I want the image to be placed inside an absolute position box (top,left,bottom,right) and be centered. How do I get this done? I am thinking of placing the images inside a floating box as described below.

public void InsertPhoto( Document doc, string photoPath )
{

     Page page = doc.Pages[1]; 

      FloatingBox photoBox = new Aspose.Pdf.FloatingBox();
      photoBox.ZIndex = 10;
      photoBox.Margin = new MarginInfo(0, 0, 0, 0);             
      photoBox.Top     = 72;
      photoBox.Left     = 72;
      photoBox.Height = 150;
      photoBox.Width  = 150;

      photoBox.BackgroundColor = Aspose.Pdf.Color.Black;
      photoBox.VertialAlignment = VerticalAlignment.Top; 
      photoBox.HorizontalAlignment = HorizontalAlignment.Center; 
      page.Paragraphs.Add( photoBox  ); 

}


Font Selection Aspose.PDF versus Words
#2

@Toberville

Thank you for contacting support.

We are looking into your requirements and will get back to you soon.


#3

@Toberville

Please use below code snippet to add an image in a floating box and align it as per your requirements.

public static void InsertPhoto(Document doc, string photoPath)
{
    Page page = doc.Pages[1];
    FloatingBox photoBox = new Aspose.Pdf.FloatingBox();
    photoBox.ZIndex = 10;
    photoBox.Margin = new MarginInfo(0, 0, 0, 0);
    photoBox.Top = 72;
    photoBox.Left = 72;
    photoBox.Height = 150;
    photoBox.Width = 150;
    photoBox.BackgroundColor = Aspose.Pdf.Color.Black;
    Aspose.Pdf.Image img = new Aspose.Pdf.Image();
    img.HorizontalAlignment = HorizontalAlignment.Center;
    img.VerticalAlignment = VerticalAlignment.Top;
    img.File = photoPath;
    img.FixWidth = 100;
    img.FixHeight = 100;
    photoBox.Paragraphs.Add(img);
    page.Paragraphs.Add(photoBox);
}

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#4

Hello Farhan,

Thanks for you patience. I have been working on the problem a few days and I have my solution below. I am posting the solution to share. It works as is but I am open for any programming refactoring to make the code more succinct and efficient.

public void InsertPhoto( Document doc )
{

        Page page = doc.Pages[1];

        // ------------------------------------------------------------------------------------
        // --- Absolute Positioned Bounding Box
        // ------------------------------------------------------------------------------------

        FloatingBox photoBox = new Aspose.Pdf.FloatingBox();
        photoBox.ZIndex = 10;
        photoBox.Margin = new MarginInfo(0, 0, 0, 0);
        photoBox.Top = photoConfig.top;
        photoBox.Left = photoConfig.left;
        photoBox.Height = photoConfig.height;
        photoBox.Width = photoConfig.width;
        photoBox.BackgroundColor = Aspose.Pdf.Color.Black;

        // ------------------------------------------------------------------------------------
        // --- Fast Method to get Image Dimension without leaking and locking
        // ------------------------------------------------------------------------------------

        double fotoHeight = 0;
        double fotoWidth = 0;

        using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile( photoPath ))
        {
            fotoWidth = sourceImage.Width;
            fotoHeight = sourceImage.Height;
        }

        // ------------------------------------------------------------------------------------
        // --- Resizer to fit within bounding box with aspect ratio integrity
        // ------------------------------------------------------------------------------------

        if ((fotoHeight > photoConfig.height) || (fotoWidth > photoConfig.width))
        {
            double maxWidth  = 900;                             // 3.00 Inches
            double maxHeight = 675;                             // 2.25 Inches
            var ratioX = maxWidth / fotoWidth;
            var ratioY = maxHeight / fotoHeight;
            var ratio = Math.Min(ratioX, ratioY);
            var newWidth = (int)(fotoWidth * ratio);
            var newHeight = (int)(fotoHeight * ratio);
            fotoWidth = newWidth;
            fotoHeight = newHeight;
        }
         
        // ------------------------------------------------------------------------------------
        // --- Insert Image with pixel to point conversion
        // ------------------------------------------------------------------------------------

        Aspose.Pdf.Image img = new Aspose.Pdf.Image();
        img.File = photoPath;
        img.FixHeight = (fotoHeight / 300) * 72 ;               // Convert to Points                   
        img.FixWidth  = (fotoWidth / 300) * 72 ;                // Convert to Points
        img.VerticalAlignment = VerticalAlignment.Top;
        img.HorizontalAlignment = HorizontalAlignment.Center;

        photoBox.Paragraphs.Add( img );
        page.Paragraphs.Add ( photoBox ); 

}


#5

@Toberville

Thank you for sharing the solution.

We are glad to know that you have been able to achieve your requirements. The approach is good enough and will certainly help others with similar requirements.