Skip to content

Commit 8cf6d06

Browse files
authored
Merge pull request #1862 from syncfusion-content/991467-Image-Cell
991467- FAQ for how to align the image inside the cell
2 parents f78da48 + e221282 commit 8cf6d06

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

Document-Processing-toc.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5879,6 +5879,18 @@
58795879
<li>
58805880
<a href="/document-processing/excel/excel-library/net/faqs/what-is-the-unit-of-row-height-and-column-width-in-Excel">What is the unit of row height and column width in Excel?</a>
58815881
</li>
5882+
<li>
5883+
<a href="/document-processing/excel/excel-library/net/faqs/how-to-apply-the-formatting-for-a-particular-column-while-importing-data-from-collection-objects">How to apply formatting to a specific column while importing data?</a>
5884+
</li>
5885+
<li>
5886+
<a href="/document-processing/excel/excel-library/net/faqs/how-to-align-the-image-inside-the-cell">How to align a picture inside a cell in an Excel worksheet?</a>
5887+
</li>
5888+
<li>
5889+
<a href="/document-processing/excel/excel-library/net/faqs/how-to-resolve-the-cannot-open-Pivot-table-source-file-error">How to resolve the cannot open pivot table source file error?</a>
5890+
</li>
5891+
<li>
5892+
<a href="/document-processing/excel/excel-library/net/faqs/how-to-extract-embedded-OLE-files-from-an-Excel-workbook-as-streams">How to extract embedded OLE files from an Excel workbook as streams?</a>
5893+
</li>
58825894
</ul>
58835895
</li>
58845896
</ul>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: Align a picture inside a cell in an Excel worksheet | Syncfusion
3+
description: Learn how to align an image precisely within a worksheet cell using the Syncfusion .NET Excel (XlsIO) library, including positioning, fitting to the cell.
4+
platform: document-processing
5+
control: XlsIO
6+
documentation: UG
7+
---
8+
9+
# How to align a picture inside a cell in an Excel worksheet?
10+
11+
Image can be aligned in the cell as required using the **TopRowOffset** and **LeftColumnOffset** properties of **ShapeImpl** instance. The following example shows how to align a picture inside a worksheet cell using the Syncfusion .NET Excel (XlsIO) library.
12+
13+
{% tabs %}
14+
{% highlight c# tabtitle="C# [Cross-platform]" %}
15+
using (ExcelEngine excelEngine = new ExcelEngine())
16+
{
17+
IApplication application = excelEngine.Excel;
18+
application.DefaultVersion = ExcelVersion.Xlsx;
19+
IWorkbook workbook = application.Workbooks.Create(1);
20+
IWorksheet worksheet = workbook.Worksheets[0];
21+
22+
int row = 2;
23+
int column = 3;
24+
25+
//Adding a picture
26+
FileStream imageStream = new FileStream("../../../Data/Image.png", FileMode.Open, FileAccess.Read);
27+
IPictureShape shape = worksheet.Pictures.AddPicture(row, column, imageStream);
28+
29+
//Insert the image into the cell
30+
(shape as ShapeImpl).Height = worksheet.GetRowHeightInPixels(row);
31+
(shape as ShapeImpl).Width = worksheet.GetColumnWidthInPixels(column);
32+
33+
//Algin the image inside the cell
34+
(shape as ShapeImpl).TopRowOffset = 50;
35+
(shape as ShapeImpl).LeftColumnOffset = 50;
36+
37+
#region Save
38+
//Saving the workbook
39+
workbook.SaveAs("../../../Output/Picture.xlsx");
40+
#endregion
41+
42+
//Dispose streams
43+
imageStream.Dispose();
44+
}
45+
{% endhighlight %}
46+
47+
{% highlight c# tabtitle="C# [Windows-specific]" %}
48+
using (ExcelEngine excelEngine = new ExcelEngine())
49+
{
50+
IApplication application = excelEngine.Excel;
51+
application.DefaultVersion = ExcelVersion.Xlsx;
52+
IWorkbook workbook = application.Workbooks.Create(1);
53+
IWorksheet worksheet = workbook.Worksheets[0];
54+
55+
int row = 2;
56+
int column = 3;
57+
58+
//Adding a picture
59+
string image = "../../Data/Image.png";
60+
IPictureShape shape = worksheet.Pictures.AddPicture(row, column, image);
61+
62+
// Insert the image into the cell
63+
(shape as ShapeImpl).Height = worksheet.GetRowHeightInPixels(row);
64+
(shape as ShapeImpl).Width = worksheet.GetColumnWidthInPixels(column);
65+
66+
//Algin the image inside the cell
67+
(shape as ShapeImpl).TopRowOffset = 50;
68+
(shape as ShapeImpl).LeftColumnOffset = 50;
69+
70+
#region Save
71+
//Saving the workbook
72+
workbook.SaveAs("../../Output/Picture.xlsx");
73+
#endregion
74+
}
75+
{% endhighlight %}
76+
77+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
78+
Using excelEngine As New ExcelEngine()
79+
Dim application As IApplication = excelEngine.Excel
80+
application.DefaultVersion = ExcelVersion.Xlsx
81+
82+
Dim workbook As IWorkbook = application.Workbooks.Create(1)
83+
Dim worksheet As IWorksheet = workbook.Worksheets(0)
84+
85+
Dim row As Integer = 2
86+
Dim column As Integer = 3
87+
88+
' Adding a picture
89+
Dim image As String = "../../Data/Image.png"
90+
Dim shape As IPictureShape = worksheet.Pictures.AddPicture(row, column, image)
91+
92+
' Insert the image into the cell
93+
Dim impl As ShapeImpl = CType(shape, ShapeImpl)
94+
impl.Height = worksheet.GetRowHeightInPixels(row)
95+
impl.Width = worksheet.GetColumnWidthInPixels(column)
96+
97+
' Align the image inside the cell
98+
impl.TopRowOffset = 50
99+
impl.LeftColumnOffset = 50
100+
101+
' Save
102+
workbook.SaveAs("../../Output/Picture.xlsx")
103+
End Using
104+
{% endhighlight %}
105+
{% endtabs %}

0 commit comments

Comments
 (0)