Skip to content

Commit

Permalink
Add worksheet option to print in black and white.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Feb 26, 2022
1 parent 0b00e3d commit 3de43b1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
13 changes: 13 additions & 0 deletions include/xlsxwriter/worksheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,7 @@ typedef struct lxw_worksheet {
uint8_t show_zeros;
uint8_t vcenter;
uint8_t zoom_scale_normal;
uint8_t black_white;
uint8_t num_validations;
uint8_t has_dynamic_arrays;
char *vba_codename;
Expand Down Expand Up @@ -5372,6 +5373,18 @@ void worksheet_set_start_page(lxw_worksheet *worksheet, uint16_t start_page);
*/
void worksheet_set_print_scale(lxw_worksheet *worksheet, uint16_t scale);

/**
* @brief Set the worksheet to print in black and white
*
* @param worksheet Pointer to a lxw_worksheet instance to be updated.
*
* Set the option to print the worksheet in black and white:
* @code
* worksheet_print_black_and_white(worksheet);
* @endcode
*/
void worksheet_print_black_and_white(lxw_worksheet *worksheet);

/**
* @brief Display the worksheet cells from right to left for some versions of
* Excel.
Expand Down
13 changes: 13 additions & 0 deletions src/worksheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,9 @@ _worksheet_write_page_setup(lxw_worksheet *self)
else
LXW_PUSH_ATTRIBUTES_STR("orientation", "landscape");

if (self->black_white)
LXW_PUSH_ATTRIBUTES_STR("blackAndWhite", "1");

/* Set start page active flag. */
if (self->page_start)
LXW_PUSH_ATTRIBUTES_INT("useFirstPageNumber", 1);
Expand Down Expand Up @@ -10060,6 +10063,16 @@ worksheet_set_print_scale(lxw_worksheet *self, uint16_t scale)
self->page_setup_changed = LXW_TRUE;
}

/*
* Set the print in black and white option.
*/
void
worksheet_print_black_and_white(lxw_worksheet *self)
{
self->black_white = LXW_TRUE;
self->page_setup_changed = LXW_TRUE;
}

/*
* Store the horizontal page breaks on a worksheet.
*/
Expand Down
25 changes: 25 additions & 0 deletions test/functional/src/test_print_options07.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2022, John McNamara, [email protected]
*
*/

#include "xlsxwriter.h"

int main() {

lxw_workbook *workbook = workbook_new("test_print_options07.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

worksheet_write_string(worksheet, 0, 0, "Foo" , NULL);

worksheet_set_paper(worksheet, 9);
worksheet->vertical_dpi = 200;

worksheet_print_black_and_white(worksheet);

return workbook_close(workbook);
}
4 changes: 3 additions & 1 deletion test/functional/test_print_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ def test_print_options06(self):
self.ignore_elements = {'xl/worksheets/sheet1.xml': ['<pageMargins']}
self.run_exe_test('test_print_options06')


def test_print_options07(self):
self.ignore_elements = {'xl/worksheets/sheet1.xml': ['<pageMargins']}
self.run_exe_test('test_print_options07')
Binary file added test/functional/xlsx_files/print_options07.xlsx
Binary file not shown.

0 comments on commit 3de43b1

Please sign in to comment.