jmcnamara / libxlsxwriter

A C library for creating Excel XLSX files.
https://libxlsxwriter.github.io
Other
1.49k stars 332 forks source link

Automatic print margins from header image #373

Closed znakeeye closed 2 years ago

znakeeye commented 2 years ago

Consider your example here.

As shown below, you need to call worksheet_set_margins to make the header image fit. It would be nice if the worksheet_set_header_opt could do this automatically. E.g. through a new auto_fit_images setting.

The original sample

/*
 * A simple example to start
 */
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Image");
lxw_header_footer_options header_options = {.image_left = "logo_small.png"};

worksheet_set_header_opt(worksheet2, "&L&[Picture]", &header_options);
worksheet_set_margins(worksheet2, -1, -1, 1.3, -1); // Here we have a constant. Not so nice...

Auto-sizing of the print margins With this new feature, the library would perform the following when a header image is added:

if (options->auto_fit_images)
{
    double top_margin = top = max(image_left->height, image_center->height, image_right->height);
    worksheet_set_margins(worksheet, -1, -1, top_margin, -1)`;
}

And then, the sample above simply becomes:

lxw_header_footer_options header_options = {.image_left = "logo_small.png", .auto_fit_images = true };
worksheet_set_header_opt(worksheet2, "&L&[Picture]", &header_options);

Motivation It might be problematic for the consumer of the library to calculate the image height. Since libxlsxwriter is able to read the height of many types of images, why not allow for this feature natively?

jmcnamara commented 2 years ago

It might be problematic for the consumer of the library to calculate the image height. Since libxlsxwriter is able to read the height of many types of images, why not allow for this feature natively?

Thanks for the suggestion. Overall I think this is outside the scope of libxlsxwriter and falls into to the category of things that the user can do themselves without the help of the library.

In general people use the header image option to set a company logo, or similar, and that is generally a one-off activity. So for me, it is best to leave specifying the top margin to the end user, since they are likely to only have to figure that once per logo (as you would have to do with Excel). If there was a auto_fit_images property of Excel file format then I would be happy to implement that but any heuristic calculation by the library might not give the end user what they expect. For example the space required in the margin doesn't just depend on the image height but also on the DPI.

So for me this is a pass, but thanks once more for the suggestion.