How to Replace Embed Excel With Images in .NET Core Word Conversion?
Syncfusion® Essential® DocIO is a .NET Word Library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can replace embedded Excel objects with image during Word to PDF conversion using C#.
Steps to replace embedded Excel with image during Word to PDF conversion:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIORenderer.Net.Core and Syncfusion.XlsIORenderer.Net.Core NuGet packages as a reference to your project from NuGet.org.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without trail message.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf;
using Syncfusion.DocIORenderer;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
- Use the following code example to replace a embedded Excel with image during Word to PDF conversion.
C#
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
{
//Open the input Word document.
using (WordDocument document = new WordDocument(inputStream, FormatType.Automatic))
{
//Replace embedded Excel objects in the document with images.
ReplaceExcelToImage(document);
//Initialize the DocIORenderer component for converting Word documents to PDF.
using (DocIORenderer docIORenderer = new DocIORenderer())
{
//Convert the Word document to a PDF using the DocIORenderer component.
using (PdfDocument pdf = docIORenderer.ConvertToPDF(document))
{
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write))
{
//Save the generated PDF to the specified file stream.
pdf.Save(outputStream);
}
}
}
}
}
- Use the following helper method to replace embedded Excel in a Word document with images, preserving their original dimensions.
C#
/// <summary>
/// Replaces embedded Excel OLE objects in a Word document with their corresponding images while maintaining the original size.
/// </summary>
void ReplaceExcelToImage(WordDocument wordDocument)
{
//Get the Ole objects.
List<Entity> oleObjects = wordDocument.FindAllItemsByProperty(EntityType.OleObject, null, null);
//Iterate through the ole objects.
for (int i = 0; i < oleObjects.Count; i++)
{
WOleObject ole = oleObjects[i] as WOleObject;
//Check the type of OLE.
string type = ole.ObjectType;
//Get the height and width of OLE picture.
float height = ole.OlePicture.Height;
float width = ole.OlePicture.Width;
//If the type contains "Excel", then the OLE object is extracted from Excel.
if (type.Contains("Excel"))
{
//Create a Excel file using the Ole data.
MemoryStream excelStream = new MemoryStream();
excelStream.Write(ole.NativeData);
excelStream.Position = 0;
//Creates a new instance for ExcelEngine.
ExcelEngine excelEngine = new ExcelEngine();
//Initialize IApplication.
IApplication application = excelEngine.Excel;
//Loads or open an existing workbook through Open method of IWorkbooks.
IWorkbook workbook = application.Workbooks.Open(excelStream);
IWorksheet sheet = workbook.Worksheets[0];
//Initialize XlsIORenderer.
application.XlsIORenderer = new XlsIORenderer();
//Converts and save as stream.
MemoryStream imgStream = new MemoryStream();
sheet.ConvertToImage(1, 1, 6, 5, imgStream);
imgStream.Position = 0;
//Load the converted image as OLE picture.
ole.OlePicture.LoadImage(imgStream);
ole.OlePicture.LockAspectRatio = false;
ole.OlePicture.Height = height;
ole.OlePicture.Width = width;
//Close and Dispose.
workbook.Close();
imgStream.Dispose();
excelStream.Dispose();
}
}
}
You can download a complete working sample to replace embedded Excel with image during Word to PDF conversion from GitHub.
By executing the program, you will get the Word document as follows.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to replace embedded excel with images in .NET Core Word Conversion.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!