libhpdftbl 1.5.0
Table construction library for Haru PDF library
Loading...
Searching...
No Matches
tut_ex07.c

Expand cells over multiple columns and rows.

#include "unit_test.inc.h"
#ifndef _MSC_VER
// Silent gcc about unused "arg" in the callback and error functions
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
static char *
cb_date(void *tag, size_t r, size_t c) {
static char buf[64];
if ( ! run_as_unit_test ) {
time_t t = time(NULL);
ctime_r(&t, buf);
return buf;
} else {
return "Wed May 4 19:01:01 2022";
}
}
static char *
cb_content(void *tag, size_t r, size_t c) {
static char buf[32];
#if (defined _WIN32 || defined __WIN32__)
snprintf(buf, sizeof buf, "Content %02ix%02i", r, c);
#else
snprintf(buf, sizeof buf, "Content %02zux%02zu", r, c);
#endif
return buf;
}
static char *
cb_labels(void *tag, size_t r, size_t c) {
static char buf[32];
#if (defined _WIN32 || defined __WIN32__)
if (0==r && 0==c) {
snprintf(buf, sizeof buf, "Date:");
} else {
snprintf(buf, sizeof buf, "Label %ix%i:", r, c);
}
#else
if (0==r && 0==c) {
snprintf(buf, sizeof buf, "Date:");
} else {
snprintf(buf, sizeof buf, "Label %zux%zu:", r, c);
}
#endif
return buf;
}
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif
void
create_table_ex07(HPDF_Doc pdf_doc, HPDF_Page pdf_page) {
const size_t num_rows = 7;
const size_t num_cols = 5;
char *table_title = "tut_ex07: 7x5 table with row and colspans";
hpdftbl_t tbl = hpdftbl_create_title(num_rows, num_cols, table_title);
hpdftbl_set_content_cb(tbl, cb_content);
hpdftbl_set_label_cb(tbl, cb_labels);
hpdftbl_set_cell_content_cb(tbl, 0, 0, cb_date);
hpdftbl_set_cellspan(tbl, 0, 0, 1, 3);
hpdftbl_set_cellspan(tbl, 2, 2, 3, 3);
hpdftbl_set_cellspan(tbl, 3, 0, 4, 1);
HPDF_REAL xpos = hpdftbl_cm2dpi(1);
HPDF_REAL ypos = hpdftbl_cm2dpi(A4PAGE_HEIGHT_CM - 1);
HPDF_REAL width = hpdftbl_cm2dpi(18);
HPDF_REAL height = 0; // Calculate height automatically
// Stroke the table to the page
hpdftbl_stroke(pdf_doc, pdf_page, tbl, xpos, ypos, width, height);
}
TUTEX_MAIN(create_table_ex07, FALSE)
int hpdftbl_use_labelgrid(hpdftbl_t t, _Bool use)
Shorter vertical line to mark labels.
Definition: hpdftbl.c:687
int hpdftbl_use_labels(hpdftbl_t t, _Bool use)
Enable/Disable the use of cell labels.
Definition: hpdftbl.c:666
int hpdftbl_set_cellspan(hpdftbl_t t, size_t r, size_t c, size_t rowspan, size_t colspan)
Set cell spanning.
Definition: hpdftbl.c:839
hpdftbl_t hpdftbl_create_title(size_t rows, size_t cols, char *title)
Create a new table with title top row.
Definition: hpdftbl.c:326
int hpdftbl_stroke(HPDF_Doc pdf, const HPDF_Page page, hpdftbl_t t, const HPDF_REAL xpos, const HPDF_REAL ypos, const HPDF_REAL width, HPDF_REAL height)
Stroke the table.
Definition: hpdftbl.c:1682
#define TRUE
Boolean truth value.
Definition: hpdftbl.h:47
#define FALSE
Boolean false value.
Definition: hpdftbl.h:52
int hpdftbl_set_cell_content_cb(hpdftbl_t t, size_t r, size_t c, hpdftbl_content_callback_t cb)
Set cell content callback.
Definition: hpdftbl_callback.c:107
#define A4PAGE_HEIGHT_CM
Standard A4 paper height in cm.
Definition: hpdftbl.h:198
#define hpdftbl_cm2dpi(c)
Convert cm to dots using the default resolution (72 DPI)
Definition: hpdftbl.h:256
int hpdftbl_set_label_cb(hpdftbl_t t, hpdftbl_content_callback_t cb)
Set table label callback.
Definition: hpdftbl_callback.c:212
int hpdftbl_set_content_cb(hpdftbl_t t, hpdftbl_content_callback_t cb)
Set table content callback.
Definition: hpdftbl_callback.c:86
Core table handle.
Definition: hpdftbl.h:470
Common functions for all unit-test/examples.
_Bool run_as_unit_test
For the case when we use this example as a unit/integration test we do not want data such as dates,...
Definition: unit_test.inc.h:35
#define TUTEX_MAIN(_tbl_, _showgrid_)
Macro to create a main() function to call the table creation function for each example....
Definition: unit_test.inc.h:322