[GH-ISSUE #373] Automatic print margins from header image #298

Closed
opened 2026-05-05 12:07:16 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @znakeeye on GitHub (May 17, 2022).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/373

Originally assigned to: @jmcnamara on GitHub.

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?

Originally created by @znakeeye on GitHub (May 17, 2022). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/373 Originally assigned to: @jmcnamara on GitHub. Consider your example [here](https://libxlsxwriter.github.io/headers_footers_8c-example.html). 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** ```c /* * 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: ```c 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: ```c 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?
gitea-mirror 2026-05-05 12:07:16 -06:00
Author
Owner

@jmcnamara commented on GitHub (May 17, 2022):

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.

<!-- gh-comment-id:1129085221 --> @jmcnamara commented on GitHub (May 17, 2022): > 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.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#298
No description provided.