|
| 1 | +using System.IO; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using Syncfusion.EJ2.PdfViewer; |
| 5 | +using SkiaSharp; |
| 6 | + |
| 7 | +namespace PDF_as_OLE_with_first_page_as_preview |
| 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 | +} |
0 commit comments