Skip to content

Commit a937e9f

Browse files
Added the replace excel embedded image to png
1 parent 9c396a8 commit a937e9f

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
12+
// Disable the PDF document cache to prevent caching of rendered pages
13+
PdfDocument.EnableCache = false;
14+
// Initialize the DocIORenderer component for converting Word documents to PDF
15+
using DocIORenderer docIORenderer = new DocIORenderer();
16+
// Create new DocIORenderer settings
17+
docIORenderer.Settings = new DocIORendererSettings();
18+
// Set the PDF conformance level to PDF/A-1B for archival standard compliance
19+
docIORenderer.Settings.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1B;
20+
// Set the chart rendering scaling mode to normal to preserve the aspect ratio of charts
21+
docIORenderer.Settings.ChartRenderingOptions.ScalingMode = Syncfusion.OfficeChart.ScalingMode.Normal;
22+
// Open the input Word document from a file stream
23+
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read);
24+
// Load the Word document into a WordDocument instance
25+
using var tempDocument = new WordDocument(inputStream, FormatType.Automatic);
26+
// Call a method to replace embedded Excel objects in the document with images
27+
ReplaceExcelToImage(tempDocument);
28+
// Convert the Word document to a PDF using the DocIORenderer component
29+
using PdfDocument pdf = docIORenderer.ConvertToPDF(tempDocument);
30+
// Create a file stream to save the output PDF document
31+
FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write);
32+
// Save the generated PDF to the specified file stream
33+
pdf.Save(outputStream);
34+
35+
36+
void ReplaceExcelToImage(WordDocument wordDocument)
37+
{
38+
//Get the Ole objects.
39+
List<Entity> oleObjects = wordDocument.FindAllItemsByProperty(EntityType.OleObject, null, null);
40+
//Iterate through the ole objects.
41+
for (int i = 0; i < oleObjects.Count; i++)
42+
{
43+
WOleObject ole = oleObjects[i] as WOleObject;
44+
//Check the type of OLE.
45+
string type = ole.ObjectType;
46+
//Get the height and width of OLE picture.
47+
float height = ole.OlePicture.Height;
48+
float width = ole.OlePicture.Width;
49+
//If the type contains "Excel", then the OLE object is extracted from Excel.
50+
if (type.Contains("Excel"))
51+
{
52+
//Create a Excel file using the Ole data.
53+
MemoryStream excelStream = new MemoryStream();
54+
excelStream.Write(ole.NativeData);
55+
excelStream.Position = 0;
56+
57+
//Creates a new instance for ExcelEngine.
58+
ExcelEngine excelEngine = new ExcelEngine();
59+
//Initialize IApplication.
60+
IApplication application = excelEngine.Excel;
61+
//Loads or open an existing workbook through Open method of IWorkbooks.
62+
IWorkbook workbook = application.Workbooks.Open(excelStream);
63+
IWorksheet sheet = workbook.Worksheets[0];
64+
65+
//Initialize XlsIORenderer.
66+
application.XlsIORenderer = new XlsIORenderer();
67+
68+
//Converts and save as stream.
69+
MemoryStream imgStream = new MemoryStream();
70+
sheet.ConvertToImage(1, 1, 6, 5, imgStream);
71+
imgStream.Position = 0;
72+
73+
//Load the converted image as OLE picture.
74+
ole.OlePicture.LoadImage(imgStream);
75+
ole.OlePicture.LockAspectRatio = false;
76+
ole.OlePicture.Height = height;
77+
ole.OlePicture.Width = width;
78+
79+
//Close and Dispose.
80+
workbook.Close();
81+
imgStream.Dispose();
82+
excelStream.Dispose();
83+
}
84+
}
85+
}
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)