Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.
Thu Aug 01, 2024 4:21 pm
Hi,
Followed the tutorial for adding an Image to a Cell and the Image is drawn all the way over to the left of the cell. Didn't matter what value I entered for the Left in the line:
- Code: Select all
grid.Style.CellPadding = new PdfPaddings(10, 1, 1, 1)
How do I get the image centered?
-

tim8w123
-
- Posts: 18
- Joined: Wed Feb 22, 2023 3:55 pm
Fri Aug 02, 2024 10:08 am
Hello,
Thanks for your inquiry.
You can refer to the following example code to insert your image. If there is any other issues, just feel free to write back.
- Code: Select all
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
PdfGrid grid = new PdfGrid();
PdfGridRow row1 = grid.Rows.Add();
grid.Columns.Add(1);
grid.Columns[0].Width = 120f;
SizeF imageSize = new SizeF(50, 50);
//Loading images
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
content.Image = PdfImage.FromFile("logo.png");
content.ImageSize = imageSize;
contentList.List.Add(content);
//Set the image to center format
row1.Cells[0].StringFormat.Alignment = PdfTextAlignment.Center;
//Fill the cell with the image
row1.Cells[0].Value = contentList;
//Draw the table onto the page
grid.Draw(page, new PointF(0f, 30f));
pdf.SaveToFile("centerImage.pdf");
Sincerely,
Amin
E-iceblue support team
-


Amin.Gan
-
- Posts: 306
- Joined: Mon Jul 15, 2024 5:40 am
Fri Aug 02, 2024 2:30 pm
Thanks! That worked perfectly.
-

tim8w123
-
- Posts: 18
- Joined: Wed Feb 22, 2023 3:55 pm
Mon Aug 05, 2024 1:19 am
Hello,
Thanks for your reply.
I'm glad to hear this news. If you have any other questions, please feel free to write to me.
Sincerely,
Amin
E-iceblue support team
-


Amin.Gan
-
- Posts: 306
- Joined: Mon Jul 15, 2024 5:40 am