How To Draw Heart Shaped ImageView In Excel And Convert The ImageView To PDF

In this article, I will show you how to display images with irregular shapes, such as a love heart outline, eclipse outline, and a cloud outline etc. I just want to show others that our software developers are a very romantic bunch. I toyed with the idea of leaving this post to coincide with Valentines’ Day but I decided that publishing it now would give people a whole one and half months to work on some new and interesting variations upon this theme.

Drawing a custom shape ImageView is quite simple using Spire.XLS. Before we start drawing the ImageView, we only need to add Spire.Xls.dll as the reference to our application. Firstly, we need to create an MS Excel workbook object and get the first worksheet. There are the three worksheets by default.

  1. Workbook workbook = new Workbook();  
  2. Worksheet sheet = workbook.Worksheets[0];  
Then, I need to draw a heart shape and define its position and size. Fill-in the heart shape with a picture.
  1. //Add a heart shape  
  2. IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 1, 150, 220, PrstGeomShapeType.Heart);  
  3.   
  4. //Fill the heart with picture  
  5. heart.Fill.CustomPicture(Image.FromFile("pic.jpg"),"pic.jpg");  
  6. heart.Fill.FillType = ShapeFillType.Picture;  

 How To Draw Heart Shaped ImageView In Excel And Convert The ImageView To PDF

We could use a similar method to draw many kinds of shapes to the Excel worksheets, such as Oval, Star, Cloud, Triangle, Rectangle, Circle etc. There are 187 kinds of shapes in total.
 
 How To Draw Heart Shaped ImageView In Excel And Convert The ImageView To PDF

After adding the shapes, save the document to the file.

  1. workbook.SaveToFile("Addheart.xlsx", ExcelVersion.Version2013);  

 How To Draw Heart Shaped ImageView In Excel And Convert The ImageView To PDF

The source code for this article is mentioned below.
  1. using System.Drawing;  
  2. using Spire.Xls;  
  3. using Spire.Xls.Core;  
  4.   
  5.   
  6. Workbook workbook = new Workbook();  
  7. Worksheet sheet = workbook.Worksheets[0];  
  8.   
  9.   
  10. //Add a heart shape  
  11. IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 1, 150, 220, PrstGeomShapeType.Heart);  
  12.   
  13. //Fill the heart with picture  
  14. heart.Fill.CustomPicture(Image.FromFile("pic.jpg"),"pic.jpg");  
  15.   
  16. heart.Fill.FillType = ShapeFillType.Picture;  
  17.   
  18. //Add an ellipse shape  
  19. IPrstGeomShape ellipse= sheet.PrstGeomShapes.AddPrstGeomShape(2, 4, 150, 225, PrstGeomShapeType.Ellipse);  
  20.   
  21. ellipse.Fill.CustomPicture(Image.FromFile("pic.jpg"), "pic.jpg");  
  22.   
  23. ellipse.Fill.FillType = ShapeFillType.Picture;  
  24.   
  25. //Add a rectangle shape  
  26. IPrstGeomShape rec = sheet.PrstGeomShapes.AddPrstGeomShape(2, 7, 140, 215, PrstGeomShapeType.Rect);  
  27.   
  28. rec.Fill.CustomPicture(Image.FromFile("pic.jpg"), "pic.jpg");  
  29. rec.Fill.FillType = ShapeFillType.Picture;  
  30.   
  31. //Add a cloud shape  
  32. IPrstGeomShape cloud = sheet.PrstGeomShapes.AddPrstGeomShape(21, 1, 150, 225, PrstGeomShapeType.Cloud);  
  33.   
  34. cloud.Fill.CustomPicture(Image.FromFile("pic.jpg"), "pic.jpg");  
  35.   
  36. cloud.Fill.FillType = ShapeFillType.Picture;  
  37.   
  38. //Add a star shape  
  39. IPrstGeomShape star = sheet.PrstGeomShapes.AddPrstGeomShape(20, 4, 150,225, PrstGeomShapeType.Star32);  
  40.   
  41. star.Fill.CustomPicture(Image.FromFile("pic.jpg"), "pic.jpg");  
  42.   
  43. star.Fill.FillType = ShapeFillType.Picture;  
  44.   
  45. //Add a star shape  
  46. IPrstGeomShape star8 = sheet.PrstGeomShapes.AddPrstGeomShape(20, 7, 150, 225, PrstGeomShapeType.Star8);  
  47.   
  48. star8.Fill.CustomPicture(Image.FromFile("pic.jpg"), "pic.jpg");  
  49.   
  50. star8.Fill.FillType = ShapeFillType.Picture;  
  51.   
  52. //Save the file  
  53. workbook.SaveToFile("AddShapes.xlsx", ExcelVersion.Version2013);  

When we want to show these ImageViews to others, the PDF format is much better. It can’t be changed easily and now, I will show you how to save the Excel to PDF.

  1. //load the sample document  
  2. Workbook workbook = new Workbook();  
  3.   
  4. workbook.LoadFromFile("Addshapes.xlsx", ExcelVersion.Version2013);  
  5.   
  6. //Save the file  
  7. workbook.SaveToFile("ExcelToPDF.pdf", FileFormat.PDF);  

How To Draw Heart Shaped ImageView In Excel And Convert The ImageView To PDF

 

Now, I have got the PDF file I want. It is quite simple, right? I hope it helps and Happy New Year!