diff --git a/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE.sln b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE.sln new file mode 100644 index 000000000..0a3c79e62 --- /dev/null +++ b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE.sln @@ -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 diff --git a/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Data/Adventure.pdf b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Data/Adventure.pdf new file mode 100644 index 000000000..a47ac0788 Binary files /dev/null and b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Data/Adventure.pdf differ diff --git a/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Data/Template.docx b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Data/Template.docx new file mode 100644 index 000000000..81f7ea08c Binary files /dev/null and b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Data/Template.docx differ diff --git a/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Output/.gitkeep b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Program.cs b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Program.cs new file mode 100644 index 000000000..054478e80 --- /dev/null +++ b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Program.cs @@ -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); + } + } + } + } + + /// + /// Extracts the first page of a PDF as an image (PNG format). + /// + /// The file stream of the PDF. + /// A byte array containing the image data. + 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(); + } + } + } + } +} diff --git a/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Replace-bookmark-content-with-OLE.csproj b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Replace-bookmark-content-with-OLE.csproj new file mode 100644 index 000000000..f143d92d5 --- /dev/null +++ b/Bookmarks/Replace-bookmark-content-with-OLE/.NET/Replace-bookmark-content-with-OLE/Replace-bookmark-content-with-OLE.csproj @@ -0,0 +1,27 @@ + + + + Exe + net8.0 + Replace_bookmark_content_with_OLE + + + + + + + + + + + Always + + + Always + + + Always + + + +