A collection of more and less advanced examples in one file. For learning the library it is better to start with the organized tutorial examples like tut_ex01.c and tut_ex02.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !(defined _WIN32 || defined __WIN32__)
#include <unistd.h>
#endif
#include <hpdf.h>
#include <math.h>
#include <setjmp.h>
#include <time.h>
#include <sys/stat.h>
#include <libgen.h>
#if !(defined _WIN32 || defined __WIN32__)
#include <sys/utsname.h>
#endif
#include "../src/hpdftbl.h"
#ifdef _WIN32
#define OUTPUT_FILE "example01.pdf"
#else
#define OUTPUT_FILE "/tmp/example01.pdf"
#endif
HPDF_Doc pdf_doc;
HPDF_Page pdf_page;
#define MAX_NUM_ROWS 10
#define MAX_NUM_COLS 10
char *labels[MAX_NUM_ROWS * MAX_NUM_COLS];
char *content[MAX_NUM_ROWS * MAX_NUM_COLS];
void
setup_dummy_data(void) {
char buff[255];
size_t cnt = 0;
for (size_t r = 0; r < MAX_NUM_ROWS; r++) {
for (size_t c = 0; c < MAX_NUM_COLS; c++) {
#if (defined _WIN32 || defined __WIN32__)
sprintf(buff, "Label %i:", cnt);
labels[cnt] = _strdup(buff);
sprintf(buff, "Content %i", cnt);
content[cnt] = _strdup(buff);
#else
snprintf(buff, sizeof(buff), "Label %zu:", cnt);
labels[cnt] = strdup(buff);
snprintf(buff, sizeof(buff), "Content %zu", cnt);
content[cnt] = strdup(buff);
#endif
cnt++;
}
}
}
#ifndef _MSC_VER
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
#if !(defined _WIN32 || defined __WIN32__)
static char *
cb_name(void *tag, size_t r, size_t c) {
static char buf[256];
struct utsname sysinfo;
return "???";
} else {
snprintf(buf, sizeof(buf), "Name: %s, Kernel: %s %s", sysinfo.nodename,
sysinfo.sysname, sysinfo.release);
return buf;
}
}
static char *
cb_date(void *tag, size_t r, size_t c) {
static char buf[64];
time_t t = time(NULL);
ctime_r(&t, buf);
return buf;
} else {
return "Wed May 4 19:01:01 2022";
}
}
void
cb_draw_segment_hbar(HPDF_Doc doc, HPDF_Page page, void *tag, size_t r,
size_t c, HPDF_REAL xpos, HPDF_REAL ypos,
HPDF_REAL width, HPDF_REAL height) {
const HPDF_REAL segment_tot_width = width * 0.5;
const HPDF_REAL segment_height = height / 3;
const HPDF_REAL segment_xpos = xpos + 40;
const HPDF_REAL segment_ypos = ypos + 4;
const size_t num_segments = 10;
const HPDF_RGBColor on_color = HPDF_COLOR_GREEN;
const double val_percent = 0.4;
const _Bool val_text_hide =
FALSE;
doc, page, segment_xpos, segment_ypos, segment_tot_width,
segment_height, num_segments, on_color, val_percent, val_text_hide);
}
void
cb_draw_hbar(HPDF_Doc doc, HPDF_Page page, void *tag, size_t r, size_t c,
HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width,
HPDF_REAL height) {
const HPDF_REAL wwidth = width * 0.5;
const HPDF_REAL wheight = height / 3;
const HPDF_REAL wxpos = xpos + 40;
const HPDF_REAL wypos = ypos + 4;
const HPDF_RGBColor color = HPDF_COLOR_GREEN;
const double val = 0.6;
const _Bool val_text_hide =
FALSE;
val_text_hide);
}
void
cb_draw_slider(HPDF_Doc doc, HPDF_Page page, void *tag, size_t r, size_t c,
HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width,
HPDF_REAL height) {
const HPDF_REAL wwidth = 37;
const HPDF_REAL wheight = 12;
const HPDF_REAL wxpos = xpos + 70;
const HPDF_REAL wypos = ypos + 5;
_Bool state = (r == 2);
state);
}
void
cb_draw_strength_meter(HPDF_Doc doc, HPDF_Page page, void *tag, size_t r,
size_t c, HPDF_REAL xpos, HPDF_REAL ypos,
HPDF_REAL width, HPDF_REAL height) {
const HPDF_REAL wwidth = 35;
const HPDF_REAL wheight = 20;
const HPDF_REAL wxpos = xpos + 70;
const HPDF_REAL wypos = ypos + 4;
const size_t num_segments = 5;
const HPDF_RGBColor on_color = HPDF_COLOR_GREEN;
const size_t num_on_segments = 3;
num_segments, on_color, num_on_segments);
}
void
cb_draw_boxed_letter(HPDF_Doc doc, HPDF_Page page, void *tag, size_t r,
size_t c, HPDF_REAL xpos, HPDF_REAL ypos,
HPDF_REAL width, HPDF_REAL height) {
const HPDF_REAL wwidth = 60;
const HPDF_REAL wheight = 15;
const HPDF_REAL wxpos = xpos + 60;
const HPDF_REAL wypos = ypos + 4;
const HPDF_RGBColor on_color = HPDF_COLOR_DARK_GRAY;
const HPDF_RGBColor off_color = HPDF_COLOR_GRAY;
const HPDF_RGBColor on_background = HPDF_COLOR_GREEN;
const HPDF_RGBColor off_background = HPDF_COLOR_LIGHT_GRAY;
const HPDF_REAL fsize = 11;
const char *letters = "ABCD";
on_color, off_color, on_background,
off_background, fsize, letters, state);
}
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif
void
example_page_header(void) {
{0, 0, 1, 4, "Server info:", cb_name, NULL, NULL, NULL},
{0, 4, 1, 2, "Date:", cb_date, NULL, NULL, NULL},
{0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL}
};
.
title=NULL, .use_header=0,
.use_labels=1, .use_labelgrid=1,
.rows=1, .cols=6,
.width=500, .height=0,
.content_cb=0, .label_cb=0, .style_cb=0, .post_cb=0,
.cell_spec=tbl1_data
};
if (-1 == ret) {
const char *buf;
int r, c;
fprintf(stderr,
"*** ERROR in creating table from data. ( %d : \"%s\" ) @ "
"[%d,%d]\n",
tbl_err, buf, r, c);
}
}
#endif
static void
add_a4page(void) {
pdf_page = HPDF_AddPage(pdf_doc);
HPDF_Page_SetSize(pdf_page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
}
void
ex_tbl1(void) {
int num_rows = 5;
int num_cols = 4;
char *table_title = "Example 1: Basic table with default theme";
HPDF_REAL height = 0;
}
void
ex_tbl2(void) {
int num_rows = 5;
int num_cols = 4;
char *table_title = "Example 2: Basic table with adjusted font styles";
const HPDF_RGBColor title_text_color = HPDF_COLOR_DARK_RED;
const HPDF_RGBColor title_bg_color = HPDF_COLOR_LIGHT_GRAY;
title_bg_color);
const HPDF_RGBColor content_text_color = HPDF_COLOR_DARK_GRAY;
const HPDF_RGBColor content_bg_color = HPDF_COLOR_LIGHT_BLUE;
content_text_color, content_bg_color);
HPDF_REAL height = 0;
}
void
ex_tbl3(void) {
int num_rows = 9;
int num_cols = 4;
char *table_title =
"Example 3: Table cell spannings and full grid and header";
const HPDF_RGBColor title_text_color = HPDF_COLOR_DARK_RED;
const HPDF_RGBColor title_bg_color = HPDF_COLOR_LIGHT_GRAY;
title_bg_color);
const HPDF_RGBColor content_text_color = HPDF_COLOR_DARK_GRAY;
const HPDF_RGBColor content_bg_color = HPDF_COLOR_WHITE;
content_text_color, content_bg_color);
HPDF_REAL height = 0;
}
void
ex_tbl4(void) {
const size_t num_rows = 5;
const size_t num_cols = 4;
char *table_title = "Example 4: Adjusting look and feel of single cell";
const HPDF_RGBColor title_text_color = HPDF_COLOR_DARK_RED;
const HPDF_RGBColor title_bg_color = HPDF_COLOR_LIGHT_GRAY;
title_bg_color);
const HPDF_RGBColor content_bg_color = HPDF_COLOR_ORANGE;
const HPDF_RGBColor content_text_color = HPDF_COLOR_ALMOST_BLACK;
content_text_color, content_bg_color);
content_text_color, content_bg_color);
HPDF_REAL height = 0;
if (-1 ==
const char *errstr;
int row, col;
fprintf(stderr, "ERROR: \"%s\"\n", errstr);
}
}
void
ex_tbl5(void) {
const int num_rows = 6;
const int num_cols = 4;
char *table_title = "Example 5: Using widgets in cells";
const HPDF_RGBColor title_text_color = HPDF_COLOR_DARK_RED;
const HPDF_RGBColor title_bg_color = HPDF_COLOR_LIGHT_GRAY;
title_bg_color);
size_t wrow = 0;
size_t wcol = 0;
content[wrow * num_cols + wcol] = NULL;
labels[wrow * num_cols + wcol] = "Horizontal seg bar:";
wrow += 1;
content[wrow * num_cols + wcol] = NULL;
labels[wrow * num_cols + wcol] = "Horizontal bar:";
wrow += 1;
content[wrow * num_cols + wcol] = NULL;
labels[wrow * num_cols + wcol] = "Slider on:";
wrow += 1;
content[wrow * num_cols + wcol] = NULL;
labels[wrow * num_cols + wcol] = "Slider off:";
wrow += 1;
content[wrow * num_cols + wcol] = NULL;
labels[wrow * num_cols + wcol] = "Strength meter:";
wrow += 1;
content[wrow * num_cols + wcol] = NULL;
labels[wrow * num_cols + wcol] = "Boxed letters:";
HPDF_REAL height = 0;
if (-1 ==
const char *errstr;
int row, col;
fprintf(stderr, "ERROR: \"%s\"\n", errstr);
}
}
typedef void (*t_func_tbl_stroke)(void);
int
main(int argc, char **argv) {
t_func_tbl_stroke examples[] = {ex_tbl1, ex_tbl2, ex_tbl3, ex_tbl4,
ex_tbl5};
const size_t num_examples = sizeof(examples) / sizeof(t_func_tbl_stroke);
printf("Stroking %ld examples.\n", num_examples);
HPDF_Free(pdf_doc);
return EXIT_FAILURE;
}
setup_dummy_data();
pdf_doc = HPDF_New(error_handler, NULL);
HPDF_SetCompressionMode(pdf_doc, HPDF_COMP_ALL);
for (size_t i = 0; i < num_examples; i++) {
add_a4page();
#if !(defined _WIN32 || defined __WIN32__)
example_page_header();
#endif
(*examples[i])();
}
return EXIT_FAILURE;
else
return EXIT_SUCCESS;
}
int hpdftbl_set_content(hpdftbl_t t, char **content)
Set the content for the table.
Definition: hpdftbl.c:1048
int hpdftbl_set_min_rowheight(hpdftbl_t t, float h)
Set the minimum row height in the table.
Definition: hpdftbl.c:416
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_content_style(hpdftbl_t t, char *font, HPDF_REAL fsize, HPDF_RGBColor color, HPDF_RGBColor background)
Set text style for text content.
Definition: hpdftbl.c:1100
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
int hpdftbl_set_labels(hpdftbl_t t, char **labels)
Set the text for the cell labels.
Definition: hpdftbl.c:1010
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_set_title_halign(hpdftbl_t t, hpdftbl_text_align_t align)
Set horizontal alignment for table title.
Definition: hpdftbl.c:1242
int hpdftbl_set_colwidth_percent(hpdftbl_t t, size_t c, float w)
Set column width as percentage of overall table width.
Definition: hpdftbl.c:435
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
int hpdftbl_stroke_from_data(HPDF_Doc pdf_doc, HPDF_Page pdf_page, hpdftbl_spec_t *tbl_spec, hpdftbl_theme_t *theme)
Construct the table from a array specification.
Definition: hpdftbl.c:1265
int hpdftbl_set_cell_content_style(hpdftbl_t t, size_t r, size_t c, char *font, HPDF_REAL fsize, HPDF_RGBColor color, HPDF_RGBColor background)
Set the text style for content of specified cell.
Definition: hpdftbl.c:1171
int hpdftbl_use_header(hpdftbl_t t, _Bool use)
Enable/disable the interpretation of the top row as a header row.
Definition: hpdftbl.c:646
int hpdftbl_set_title_style(hpdftbl_t t, char *font, HPDF_REAL fsize, HPDF_RGBColor color, HPDF_RGBColor background)
Set the table title text style.
Definition: hpdftbl.c:1200
void hpdftbl_table_widget_letter_buttons(HPDF_Doc doc, HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width, HPDF_REAL height, HPDF_RGBColor on_color, HPDF_RGBColor off_color, HPDF_RGBColor on_background, HPDF_RGBColor off_background, HPDF_REAL fsize, const char *letters, _Bool *state)
Display an array of letters as a table where each letter is its own "mini" cell and sorrounded by a...
Definition: hpdftbl_widget.c:74
#define HPDF_FF_COURIER_BOLD
Definition: hpdftbl.h:112
hpdftbl_theme_t * hpdftbl_get_default_theme(void)
Return the default theme.
Definition: hpdftbl_theme.c:259
void hpdftbl_widget_segment_hbar(HPDF_Doc doc, HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width, HPDF_REAL height, size_t num_segments, HPDF_RGBColor on_color, double val_percent, _Bool hide_val)
Draw a horizontal segment meter that can be used to visualize a discrete value.
Definition: hpdftbl_widget.c:299
@ CENTER
Definition: hpdftbl.h:294
#define HPDF_FF_HELVETICA_BOLD
Definition: hpdftbl.h:106
int hpdftbl_get_last_errcode(const char **errstr, int *row, int *col)
Return last error code.
Definition: hpdftbl_errstr.c:316
int hpdftbl_destroy_theme(hpdftbl_theme_t *theme)
Destroy existing theme structure and free memory.
Definition: hpdftbl_theme.c:302
#define TRUE
Boolean truth value.
Definition: hpdftbl.h:47
#define FALSE
Boolean false value.
Definition: hpdftbl.h:52
void hpdftbl_widget_hbar(HPDF_Doc doc, HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width, HPDF_REAL height, HPDF_RGBColor color, float val, _Bool hide_val)
Draw a horizontal partially filled bar to indicate an analog (percentage) value.
Definition: hpdftbl_widget.c:239
void hpdftbl_widget_strength_meter(HPDF_Doc doc, HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width, HPDF_REAL height, size_t num_segments, HPDF_RGBColor on_color, size_t num_on_segments)
Draw a phone strength meter.
Definition: hpdftbl_widget.c:383
#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
void hpdftbl_widget_slide_button(HPDF_Doc doc, HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width, HPDF_REAL height, _Bool state)
Table widget that draws a sliding on/off switch. Meant to be used in a canvas callback to display a b...
Definition: hpdftbl_widget.c:140
int hpdftbl_set_cell_canvas_cb(hpdftbl_t t, size_t r, size_t c, hpdftbl_canvas_callback_t cb)
Set cell canvas callback.
Definition: hpdftbl_callback.c:178
Used in data driven table creation.
Definition: hpdftbl.h:569
Used in data driven table creation.
Definition: hpdftbl.h:596
char * title
Definition: hpdftbl.h:598
Define a set of styles into a table theme.
Definition: hpdftbl.h:635
Core table handle.
Definition: hpdftbl.h:470
Common functions for all unit-test/examples.
int stroke_to_file(HPDF_Doc pdf_doc, int argc, char **argv)
Stroke the created PDF page to a file.
Definition: unit_test.inc.h:210
jmp_buf _hpdftbl_jmp_env
For simulated exception handling.
Definition: unit_test.inc.h:45
_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