Skip to content

Commit 2985133

Browse files
Added
1 parent 010b07c commit 2985133

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-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.8.34322.80
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-Excel-embedded-image-to-PNG", "Replace-Excel-embedded-image-to-PNG\Replace-Excel-embedded-image-to-PNG.csproj", "{269AC7DF-6678-47E4-BC60-BD9811FAE8C5}"
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+
{269AC7DF-6678-47E4-BC60-BD9811FAE8C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{269AC7DF-6678-47E4-BC60-BD9811FAE8C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{269AC7DF-6678-47E4-BC60-BD9811FAE8C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{269AC7DF-6678-47E4-BC60-BD9811FAE8C5}.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 = {65FEFDF1-8E17-46B2-A4AD-3C036DDA9C16}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.DocIO.DLS;
5+
using Syncfusion.DocIO;
6+
using Syncfusion.Drawing;
7+
using System.Drawing.Imaging;
8+
using Syncfusion.XlsIO;
9+
using Syncfusion.XlsIORenderer;
10+
11+
// Initialize the DocIORenderer component for converting Word documents to PDF
12+
using DocIORenderer docIORenderer = new DocIORenderer();
13+
// Create new DocIORenderer settings
14+
docIORenderer.Settings = new DocIORendererSettings();
15+
// Open the input Word document from a file stream
16+
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read);
17+
// Load the Word document into a WordDocument instance
18+
using var tempDocument = new WordDocument(inputStream, FormatType.Automatic);
19+
// Call a method to replace embedded Excel objects in the document with images
20+
ReplaceExcelToImage(tempDocument);
21+
// Convert the Word document to a PDF using the DocIORenderer component
22+
using PdfDocument pdf = docIORenderer.ConvertToPDF(tempDocument);
23+
// Create a file stream to save the output PDF document
24+
FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write);
25+
// Save the generated PDF to the specified file stream
26+
pdf.Save(outputStream);
27+
//Dispose the streams.
28+
inputStream.Dispose();
29+
outputStream.Dispose();
30+
31+
32+
/// <summary>
33+
/// Replaces Excel OLE objects in a Word document with images, preserving their original dimensions.
34+
/// </summary>
35+
void ReplaceExcelToImage(WordDocument wordDocument)
36+
{
37+
//Get the Ole objects.
38+
List<Entity> oleObjects = wordDocument.FindAllItemsByProperty(EntityType.OleObject, null, null);
39+
//Iterate through the ole objects.
40+
for (int i = 0; i < oleObjects.Count; i++)
41+
{
42+
WOleObject ole = oleObjects[i] as WOleObject;
43+
//Check the type of OLE.
44+
string type = ole.ObjectType;
45+
//Get the height and width of OLE picture.
46+
float height = ole.OlePicture.Height;
47+
float width = ole.OlePicture.Width;
48+
//If the type contains "Excel", then the OLE object is extracted from Excel.
49+
if (type.Contains("Excel"))
50+
{
51+
//Create a Excel file using the Ole data.
52+
MemoryStream excelStream = new MemoryStream();
53+
excelStream.Write(ole.NativeData);
54+
excelStream.Position = 0;
55+
56+
//Creates a new instance for ExcelEngine.
57+
ExcelEngine excelEngine = new ExcelEngine();
58+
//Initialize IApplication.
59+
IApplication application = excelEngine.Excel;
60+
//Loads or open an existing workbook through Open method of IWorkbooks.
61+
IWorkbook workbook = application.Workbooks.Open(excelStream);
62+
IWorksheet sheet = workbook.Worksheets[0];
63+
64+
//Initialize XlsIORenderer.
65+
application.XlsIORenderer = new XlsIORenderer();
66+
67+
//Converts and save as stream.
68+
MemoryStream imgStream = new MemoryStream();
69+
sheet.ConvertToImage(1, 1, 6, 5, imgStream);
70+
imgStream.Position = 0;
71+
72+
//Load the converted image as OLE picture.
73+
ole.OlePicture.LoadImage(imgStream);
74+
ole.OlePicture.LockAspectRatio = false;
75+
ole.OlePicture.Height = height;
76+
ole.OlePicture.Width = width;
77+
78+
//Close and Dispose.
79+
workbook.Close();
80+
imgStream.Dispose();
81+
excelStream.Dispose();
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_Excel_embedded_image_to_PNG</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.NET" Version="*" />
13+
<PackageReference Include="Syncfusion.XlsIORenderer.NET" Version="*" />
14+
<PackageReference Include="System.Drawing.Common" Version="8.0.2" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<None Update="Data\Input.docx">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Output\.gitkeep">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
</Project>

0 commit comments

Comments
 (0)