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