Welcome Guest Search | Active Topics | Sign In | Register

Simple pdf with one image on it. Need to center image. Options
B at Diamond Drugs
Posted: Tuesday, August 5, 2014 9:51:00 AM
Rank: Newbie
Groups: Member

Joined: 8/5/2014
Posts: 5
I have simple task, but I need some help getting the image in the center of the PDF. For some reason, it's always showing up in the bottom left corner of the PDF. I want to center the image in the PDF document. I'm always using the standard 8.5 x 11 page size. I just need some guidance on centering the image in the page. Thank you! PS. I would send you a copy of the pdf file, but I don't see a way for me to attach it here.

Code: Visual Basic.NET
'Create a new PDF document
                        Dim doc As New PdfDocument()
                        ''Create a new page
                        Dim docPage As PdfPage = doc.Pages.Add()

                        'Load the image
                        Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(file.FullName)
                        'Create a new PdfImage object
                        Dim pdfImage As New EO.Pdf.Drawing.PdfImage(image)
                        'Create a new PdfImageContent object
                        Dim content As New EO.Pdf.Contents.PdfImageContent(pdfImage)

                        'Scale the image
                        content.AutoScale()
                        'Move the image
                        content.GfxMatrix.Translate(0, 0)
                        'Place the image on the page
                        docPage.Contents.Add(content)

                        'Load the image
                        'Dim gdiImage As System.Drawing.Image = System.Drawing.Image.FromFile(file.FullName)
                        'Dim content As New AcmContent(New AcmImage(gdiImage))
                        doc.Save(path + "bonnieTestFile08052014.pdf")
eo_support
Posted: Tuesday, August 5, 2014 10:37:44 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

This line in your code is the line that you move image:

Code: C#
content.GfxMatrix.Translate(x, y);


The coordinate origin is bottom left corner of the page. In your code you passed 0 to both argument so obviously it will stay at the bottom left corner. In order to center the image, you just need to calculate the correct X, Y value. The unit for X, Y is 1/72 inch. So you must do the following conversion:

1. Find out the inch size of your image;
2. Calculate the x, y position in inch;
3. Multiply x, y by 72. These are the final value you would need to pass to content.GfxMatrix.Translate.

Hope this helps.

Thanks!
B at Diamond Drugs
Posted: Tuesday, August 5, 2014 1:46:58 PM
Rank: Newbie
Groups: Member

Joined: 8/5/2014
Posts: 5
Thank you for your help! That was the information I needed to get it working. Here is a copy of my updated code that works, in case anybody else would want to use it:

Code: Visual Basic.NET
'Create a new PDF document
                        Dim doc As New PdfDocument()
                        ''Create a new page
                        Dim docPage As PdfPage = doc.Pages.Add()

                        'Load the image
                        Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(file.FullName)
                        'Create a new PdfImage object
                        Dim pdfImage As New EO.Pdf.Drawing.PdfImage(image)
                        'Create a new PdfImageContent object
                        Dim content As New EO.Pdf.Contents.PdfImageContent(pdfImage)

                        'The unit for X, Y is 1/72 inch.
                        Dim middlePageX As Double = (8.5 * 72) / 2
                        Dim middlePageY As Double = (11 * 72) / 2

                        'Calculate the values for X and Y to move the image.  I'm using 96 for my DPI.
                        Dim xPerInch As Double = (image.Width / 96)
                        Dim yPerInch As Double = (image.Height / 96)
                        Dim x As Double = middlePageX - ((xPerInch * 72) / 2)
                        Dim y As Double = middlePageY - ((yPerInch * 72) / 2)


                        'Move the image
                        'content.GfxMatrix.Translate(0, 100)
                        content.GfxMatrix.Translate(x, y)

                        'Scale the image
                        content.AutoScale()


                        'Place the image on the page
                        docPage.Contents.Add(content)

                        doc.Save(path + "bonnieTestFile08052014.pdf")
eo_support
Posted: Tuesday, August 5, 2014 3:37:37 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Great. Glad to hear that you got it working and thanks for sharing!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.