Table of Contents

Pictures

XlsxDocumentSheet.Pictures lists a sheet's pictures with where they are, their size, their alternative text and their bytes, and adds a PNG or JPEG at a cell as Excel adds one. A picture is moved, resized, re-placed and removed; charts and shapes in the same drawing are kept as they are. Measured against Excel: it lists the pictures of what DocWright writes as DocWright reads them, and opens it without repair.

dotnet add package DocWright.Spreadsheet

Add a picture

using DocWright.Core.Primitives;
using DocWright.Spreadsheet;

XlsxPicture logo = sheet.Pictures.Add(File.ReadAllBytes("logo.png"), "B3");    // its own size, moves with B3
Console.WriteLine($"{logo}: {logo.Width.Value / 12700} x {logo.Height.Value / 12700} pt, {logo.Placement}, {logo.ContentType}");

XlsxPicture sales;
using (FileStream jpeg = File.OpenRead("chart.jpg"))
{
    sales = sheet.Pictures.Add(jpeg, "E3", new XlsxPictureOptions
    {
        Name = "Sales",
        Width = Emu.FromPoints(200),                   // a width alone keeps the proportions
        Placement = XlsxPicturePlacement.MoveAndSize,  // stretches with its cells
        AlternativeText = "Sales by region, as a chart",
    });
}

Console.WriteLine($"{sales.Name}: {sales.Anchor}");

With no size, a picture is its image's own: its pixels at 96 per inch, read from the image's header (the image is never decoded). A width or a height alone gives the other in the image's proportions. A new picture moves with its cell and keeps its size, as Excel places one (XlsxPicturePlacement.Move); MoveAndSize stretches it with its cells and FreeFloating leaves it where it is. The bytes are stored as they are, and the same image added again, on any sheet, is one part of the workbook. PNG and JPEG are taken; an image whose header states more pixels than ResourceLimits.MaxImagePixels is refused.

List, move, resize and remove

using DocWright.Core.Primitives;
using DocWright.Spreadsheet;

sales.Move("E4", offsetX: Emu.FromPoints(6));                 // keeps its size
sales.Resize(Emu.FromPoints(240), Emu.FromPoints(120));        // keeps its top-left corner
sheet.Pictures.Add(File.ReadAllBytes("logo.png"), "B20");     // the same image: one part of the workbook
sheet.Pictures.Add(File.ReadAllBytes("logo.png"), "B30").Remove();

foreach (XlsxPicture picture in sheet.Pictures)
{
    Console.WriteLine($"{picture}  {picture.Placement}  alt '{picture.AlternativeText}'");
}

Anchor.From and Anchor.To are Excel's top-left and bottom-right cells with the offsets into them; Anchor.Left and Anchor.Top place the picture from the sheet's corner. Sizes are Emu: 12,700 to a point. OpenData() returns a picture's image; a picture linked to a file outside the workbook has none, and the link is never fetched. Removing the last picture that shows an image takes the image out of the workbook. Rows and columns inserted or deleted under a picture move it, as in Excel.

Printed

SpreadsheetConverter draws a sheet's pictures over its cells, cropped as the file crops them, and prints the sheet out to the last picture, as Excel prints it. Charts are drawn over the pictures (Charts); shapes and comments are not drawn, and the conversion says so.

The sheet as a PDF, with its pictures

Download the workbook: report-pictures.xlsx. The whole run prints:

Picture 1 B3:C5: 48 x 36 pt, Move, image/png
Sales: TwoCell E3 +0 +0 to I13 +101600 +0
Picture 1 B3:C5  Move  alt ''
Sales E4:J12  MoveAndSize  alt 'Sales by region, as a chart'
Picture 3 B20:C22  Move  alt ''