Skip to content

Commit 4f4de73

Browse files
ES-262967-Replace-bookmark-content-with-OLE
1 parent 9c8fa2d commit 4f4de73

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-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 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-bookmark-content-with-OLE", "Replace-bookmark-content-with-OLE\Replace-bookmark-content-with-OLE.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
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+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.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 = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.IO;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using Syncfusion.EJ2.PdfViewer;
5+
using SkiaSharp;
6+
7+
namespace Replace_bookmark_content_with_OLE
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
// Open the Word document template file in read/write mode
14+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
15+
{
16+
// Load the Word document into a Syncfusion DocIO instance
17+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
18+
{
19+
// Open the PDF file that will be inserted as an OLE object
20+
FileStream pdfFileStream = new FileStream(Path.GetFullPath(@"Data/Adventure.pdf"), FileMode.Open, FileAccess.Read);
21+
22+
// Extract the first page of the PDF as an image (to use as a preview)
23+
byte[] extractedImages = GetPDFFirstPageasImage(pdfFileStream);
24+
25+
// Create a picture instance to hold the extracted image
26+
WPicture picture = new WPicture(document);
27+
picture.LoadImage(extractedImages);
28+
29+
// Create a bookmark navigator to locate the target bookmark in the document
30+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
31+
32+
// Move to the bookmark named "OLEObject" where the PDF will be inserted
33+
bookmarkNavigator.MoveToBookmark("OLEObject");
34+
35+
// Get the content within the bookmark and clear it
36+
TextBodyPart textBodyPart = bookmarkNavigator.GetBookmarkContent();
37+
textBodyPart.BodyItems.Clear();
38+
39+
// Create a new paragraph to hold the OLE object
40+
WParagraph paragraph = new WParagraph(document);
41+
textBodyPart.BodyItems.Add(paragraph);
42+
43+
// Reopen the PDF file stream (because it was read earlier and may be at the end)
44+
pdfFileStream = new FileStream(Path.GetFullPath(@"Data/Adventure.pdf"), FileMode.Open, FileAccess.Read);
45+
46+
// Insert the PDF file as an OLE object within the paragraph
47+
WOleObject oleObject = paragraph.AppendOleObject(pdfFileStream, picture, OleObjectType.AdobeAcrobatDocument);
48+
49+
// Dispose of the PDF file stream after use to free resources
50+
pdfFileStream.Dispose();
51+
52+
// Set the display size of the OLE object in the document
53+
oleObject.OlePicture.Height = 200;
54+
oleObject.OlePicture.Width = 200;
55+
56+
// Replace the bookmark's content with the new text body part containing the OLE object
57+
bookmarkNavigator.ReplaceBookmarkContent(textBodyPart);
58+
59+
// Create a file stream to save the modified document
60+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
61+
{
62+
// Save the modified Word document to the output file
63+
document.Save(outputStream, FormatType.Docx);
64+
}
65+
}
66+
}
67+
}
68+
69+
/// <summary>
70+
/// Extracts the first page of a PDF as an image (PNG format).
71+
/// </summary>
72+
/// <param name="pdfFileStream">The file stream of the PDF.</param>
73+
/// <returns>A byte array containing the image data.</returns>
74+
private static byte[] GetPDFFirstPageasImage(FileStream pdfFileStream)
75+
{
76+
using (PdfRenderer pdfRenderer = new PdfRenderer())
77+
{
78+
// Load the PDF file into the renderer
79+
pdfRenderer.Load(pdfFileStream);
80+
81+
// Export the first page of the PDF as an image
82+
using (SKBitmap bitmapimage = pdfRenderer.ExportAsImage(0))
83+
using (SKImage image = SKImage.FromBitmap(bitmapimage))
84+
using (SKData imageData = image.Encode(SKEncodedImageFormat.Png, 100))
85+
{
86+
// Convert the image to a byte array and return it
87+
return imageData.ToArray();
88+
}
89+
}
90+
}
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_bookmark_content_with_OLE</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core" Version="*" />
12+
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Adventure.pdf">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\Template.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)