Quantcast
Channel: Aspose.Pdf Product Family
Viewing all articles
Browse latest Browse all 3131

Posting an Image in the Center of a Page

$
0
0
I'm not sure how to go about positioning an image. What I'm trying to do is place the image in the center of the page. This code works pretty good but it distorts the image instead of using its original size. 

How can I modify this to use the existing image size instead of just this square?

public static void InsertResellerLogo(Document profileDocument, string logoPath)
    {
        string fullLogoPath = HttpContext.Current.Request.PhysicalApplicationPath + @"\CustomLayout\" + logoPath;

        if (File.Exists(fullLogoPath))
        {
            // Set coordinates
            int lowerLeftX = 400;
            int lowerLeftY = 400;
            int upperRightX = 200;
            int upperRightY = 200;

            // Get the page where image needs to be added
            Page page = profileDocument.Pages[1];
            
            using (FileStream imageStream = new FileStream(fullLogoPath, FileMode.Open))
            {
                // Add image to Images collection of Page Resources
                page.Resources.Images.Add(imageStream);
                
                // Using GSave operator: this operator saves current graphics state
                page.Contents.Add(new Operator.GSave());

                // Create Rectangle and Matrix objects
                Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
                Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

                // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
                page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
                XImage ximage = page.Resources.Images[page.Resources.Images.Count];

                // Using Do operator: this operator draws image
                page.Contents.Add(new Operator.Do(ximage.Name));

                // Using GRestore operator: this operator restores graphics state
                page.Contents.Add(new Operator.GRestore());
            }

        }
    }


Viewing all articles
Browse latest Browse all 3131

Latest Images

Trending Articles



Latest Images