OLE Object sizing issue

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?


Aspose.Words.Drawing.Shape shape = docBuilder.InsertOleObject(stream, progId, false, bitmap);
if (shape.OleFormat != null)
{
OleFormat of = shape.OleFormat;
of.AutoUpdate = true;
}
shape.width = 50;
shape.height = 50;

This does not set the OLE object to a 50 x 50 square, the size of the object is unchanged.

I want to include a high resolution picture for the ole object, but then fit it down to the page. How can I do that?

Hi,


Thanks for your inquiry. Have you tried Aspose.Words 15.8.0? Please attach following resources here for testing:

  • Your input Word document
  • Document you’re embedding
  • Image file
  • Source code for testing.

We will investigate the issue on our end and provide you more information.

Best regards,

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,


Thanks for the additional information. But, I was unable to observe this issue even when using the following overload withou specifying Width/Height:

Bitmap bitmap = new Bitmap(origBitmap);

I suggest you please upgrade to the latest versions of Aspose.Words (15.8.0) and Aspose.Cells (8.6.0) from following link:

http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx
http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/default.aspx

Hope, this helps.

Best regards,