When inserting an OLE object, the object cannot be resized with the shape.width and shape.height properties. How do we resize an OLE object to be a different size from the image it has?
Hi,
- Your input Word document
- Document you’re embedding
- Image file
- Source code for testing.
I’m using 15.8.
We’re creating a new document and embedded excel file into it, so there is nothing there to show.
Book1.xlsx is the file we’re embedding.
The image is generated by a SheetRender instance.
Here is my code:
public static void EmbedExcelIntoWord(string excelFilePath, string wordFilePath)
{
//create a new document with documnet builder
DocumentBuilder docBuilder = new DocumentBuilder(new Document());
//always set the generated exhibit to landscape
docBuilder.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
//create OLE object image based on excel file
docBuilder.MoveToDocumentEnd();
Workbook wb = new Workbook(excelFilePath);
//auto fit all rows before imaging
wb.Worksheets[0].AutoFitRows();
Bitmap origBitmap = GetExcelSheetImage(wb.Worksheets[0]);
//determine the progid based on file save format
string progId = “”;
switch (Path.GetExtension(excelFilePath).ToLower())
{
case “.xls”:
progId = “Excel.Sheet.8”;
break;
case “.xlsx”:
progId = “Excel.Sheet.12”;
break;
}
//stream the file to the OLE object
//this is required for it to work wb.SaveToStream() DOES NOT WORK
Stream stream = File.OpenRead(excelFilePath);
//this changes the size
Bitmap bitmap = new Bitmap(origBitmap, (int)(origBitmap.Width * .1), (int)(origBitmap.Height * .1));
Aspose.Words.Drawing.Shape shape = docBuilder.InsertOleObject(stream, progId, false, bitmap);
if (shape.OleFormat != null)
{
OleFormat of = shape.OleFormat;
of.AutoUpdate = true;
}
shape.Height = 50;
shape.Width = 50;
//save the document
docBuilder.Document.Save(wordFilePath);
}
private static Bitmap GetExcelSheetImage(Worksheet sheet)
{
sheet.PageSetup.PrintArea = “A1:” + sheet.Cells.LastCell.Name;
sheet.PageSetup.BottomMargin = 0;
sheet.PageSetup.RightMargin = 0;
sheet.PageSetup.LeftMargin = 0;
sheet.PageSetup.TopMargin = 0;
sheet.PageSetup.Zoom = 100;
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Bmp;
imgOptions.OnePagePerSheet = true;
imgOptions.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
imgOptions.AllColumnsInOnePagePerSheet = true;
imgOptions.IsCellAutoFit = true;
imgOptions.OnlyArea = true;
imgOptions.PrintingPage = PrintingPageType.Default;
imgOptions.HorizontalResolution = 1100;
imgOptions.VerticalResolution = 1100;
SheetRender sr = new SheetRender(sheet, imgOptions);
return sr.ToImage(0);
}
The problem is the shape.Height and shape.Width being set to 50 does NOT change it to a 50x50 grid. I have to change the size of the bitmap to change the size of the OLE object. In addition, when you have a higher resolution image, it appears as a bigger OLE object, instead of as a higher quality image.
What I want is to be able to include a high resolution picture as the image for the OLE object and then be able to resize the OLE object to what size I need.
Hi,