Skip to content

Commit 158706e

Browse files
Added removed sample
1 parent 5b009e1 commit 158706e

File tree

10 files changed

+215
-0
lines changed

10 files changed

+215
-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.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Set-image-in-first-page-header", "Set-image-in-first-page-header\Set-image-in-first-page-header.csproj", "{49731287-5026-4C28-B705-6EFD40F92543}"
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+
{49731287-5026-4C28-B705-6EFD40F92543}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{49731287-5026-4C28-B705-6EFD40F92543}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{49731287-5026-4C28-B705-6EFD40F92543}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{49731287-5026-4C28-B705-6EFD40F92543}.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 = {C8022E12-CA10-42D8-912E-C5116435FDD6}
24+
EndGlobalSection
25+
EndGlobal
78.9 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
4+
//Creates a new Word document.
5+
using (WordDocument document = new WordDocument())
6+
{
7+
//Adds new section to the document.
8+
IWSection section = document.AddSection();
9+
//Sets DifferentFirstPage as true for inserting header and footer text.
10+
section.PageSetup.DifferentFirstPage = true;
11+
//Adds a paragraph to the section.
12+
IWParagraph paragraph = section.AddParagraph();
13+
string paraText = "AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.";
14+
//Appends some text to the first page in document.
15+
paragraph.AppendText("\r\r[ First Page ] \r\r" + paraText);
16+
paragraph.ParagraphFormat.PageBreakAfter = true;
17+
//Appends some text to the second page in document.
18+
paragraph = section.AddParagraph();
19+
//Appends some text to the second page in document.
20+
paragraph.AppendText("\r\r[ Second Page ] \r\r" + paraText);
21+
//Inserts the first page header.
22+
paragraph = section.HeadersFooters.FirstPageHeader.AddParagraph();
23+
//Adds image to the paragraph.
24+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Image.png"), FileMode.Open, FileAccess.ReadWrite);
25+
IWPicture picture = paragraph.AppendPicture(imageStream);
26+
//Sets the text wrapping style as Behind the text.
27+
picture.TextWrappingStyle = TextWrappingStyle.Behind;
28+
//Creates file stream.
29+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
30+
{
31+
//Saves the Word document to file stream.
32+
document.Save(outputFileStream, FormatType.Docx);
33+
}
34+
}
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>Set_image_in_first_page_header</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: 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.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-embedded-excel-as-image", "Replace-embedded-excel-as-image\Replace-embedded-excel-as-image.csproj", "{B41ABC4E-281E-4F88-B0EE-806DBBD5DF48}"
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+
{B41ABC4E-281E-4F88-B0EE-806DBBD5DF48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B41ABC4E-281E-4F88-B0EE-806DBBD5DF48}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B41ABC4E-281E-4F88-B0EE-806DBBD5DF48}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B41ABC4E-281E-4F88-B0EE-806DBBD5DF48}.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 = {540988BD-8B2C-4C40-BF5F-D7B0D77811A9}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

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