Skip to content

Commit abad7cc

Browse files
Merge pull request #370 from SyncfusionExamples/ES-889528-Fix-image-position
ES-889528- Add the sample Fix-image-position
2 parents bd9c9bb + dba93b6 commit abad7cc

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fix-image-position", "Fix-image-position\Fix-image-position.csproj", "{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {ADDC6FEE-8718-4B00-B091-6C4DC0CBF62A}
24+
EndGlobalSection
25+
EndGlobal
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Fix_image_position</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Image.png">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Fix_image_position
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Open the Word document
11+
using (WordDocument document = new WordDocument())
12+
{
13+
//Adds a section into Word document
14+
IWSection section = document.AddSection();
15+
//Adds a new table into Word document
16+
IWTable table = section.AddTable();
17+
//Specifies the total number of rows & columns
18+
table.ResetCells(2, 2);
19+
//Remove borders
20+
table.TableFormat.Borders.BorderType = BorderStyle.None;
21+
22+
//Iterate through all rows
23+
foreach (WTableRow row in table.Rows)
24+
{
25+
//Iterate through all cells
26+
foreach (WTableCell cell in row.Cells)
27+
{
28+
//Add Image to the cell
29+
AddImageToCell(cell);
30+
}
31+
}
32+
// Save the modified document to a new file
33+
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
34+
{
35+
document.Save(docStream1, FormatType.Docx);
36+
}
37+
}
38+
}
39+
///<summary>
40+
///Add image to a table cell
41+
///</summary>
42+
static void AddImageToCell(WTableCell cell)
43+
{
44+
//Specifies the same padding as table option as false to preserve current cell padding
45+
cell.CellFormat.SamePaddingsAsTable = false;
46+
//Add cell paddings
47+
cell.CellFormat.Paddings.Left = 10f;
48+
cell.CellFormat.Paddings.Right = 10f;
49+
cell.CellFormat.Paddings.Top = 20f;
50+
cell.CellFormat.Paddings.Bottom = 20f;
51+
52+
//Adds the image into the cell.
53+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Image.png"), FileMode.Open, FileAccess.ReadWrite);
54+
IWPicture picture = cell.AddParagraph().AppendPicture(imageStream);
55+
//Set height and width for the image
56+
picture.Height = 220;
57+
picture.Width = 220;
58+
//Close the image stream
59+
imageStream.Close();
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)