Skip to content

ES-262967- Add the sample Replace-bookmark-content-with-OLE #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
EndGlobalSection
EndGlobal
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.IO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.EJ2.PdfViewer;
using SkiaSharp;

namespace Replace_bookmark_content_with_OLE
{
class Program
{
static void Main(string[] args)
{
// Open the Word document template file in read/write mode
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
// Load the Word document into a Syncfusion DocIO instance
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
{
// Open the PDF file that will be inserted as an OLE object
FileStream pdfFileStream = new FileStream(Path.GetFullPath(@"Data/Adventure.pdf"), FileMode.Open, FileAccess.Read);

// Extract the first page of the PDF as an image (to use as a preview)
byte[] extractedImages = GetPDFFirstPageasImage(pdfFileStream);

// Create a picture instance to hold the extracted image
WPicture picture = new WPicture(document);
picture.LoadImage(extractedImages);

// Create a bookmark navigator to locate the target bookmark in the document
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);

// Move to the bookmark named "OLEObject" where the PDF will be inserted
bookmarkNavigator.MoveToBookmark("OLEObject");

// Get the content within the bookmark and clear it
TextBodyPart textBodyPart = bookmarkNavigator.GetBookmarkContent();
textBodyPart.BodyItems.Clear();

// Create a new paragraph to hold the OLE object
WParagraph paragraph = new WParagraph(document);
textBodyPart.BodyItems.Add(paragraph);

// Reopen the PDF file stream (because it was read earlier and may be at the end)
pdfFileStream = new FileStream(Path.GetFullPath(@"Data/Adventure.pdf"), FileMode.Open, FileAccess.Read);

// Insert the PDF file as an OLE object within the paragraph
WOleObject oleObject = paragraph.AppendOleObject(pdfFileStream, picture, OleObjectType.AdobeAcrobatDocument);

// Dispose of the PDF file stream after use to free resources
pdfFileStream.Dispose();

// Set the display size of the OLE object in the document
oleObject.OlePicture.Height = 200;
oleObject.OlePicture.Width = 200;

// Replace the bookmark's content with the new text body part containing the OLE object
bookmarkNavigator.ReplaceBookmarkContent(textBodyPart);

// Create a file stream to save the modified document
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
// Save the modified Word document to the output file
document.Save(outputStream, FormatType.Docx);
}
}
}
}

/// <summary>
/// Extracts the first page of a PDF as an image (PNG format).
/// </summary>
/// <param name="pdfFileStream">The file stream of the PDF.</param>
/// <returns>A byte array containing the image data.</returns>
private static byte[] GetPDFFirstPageasImage(FileStream pdfFileStream)
{
using (PdfRenderer pdfRenderer = new PdfRenderer())
{
// Load the PDF file into the renderer
pdfRenderer.Load(pdfFileStream);

// Export the first page of the PDF as an image
using (SKBitmap bitmapimage = pdfRenderer.ExportAsImage(0))
using (SKImage image = SKImage.FromBitmap(bitmapimage))
using (SKData imageData = image.Encode(SKEncodedImageFormat.Png, 100))
{
// Convert the image to a byte array and return it
return imageData.ToArray();
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Replace_bookmark_content_with_OLE</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core" Version="*" />

</ItemGroup>

<ItemGroup>
<None Update="Data\Adventure.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading