Created
April 24, 2025 06:20
-
-
Save tyano463/42b7e272a90b00bf59b5a35dc9f880d1 to your computer and use it in GitHub Desktop.
mupdf annotation sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void add_ink_annot_sample(const char *input_pdf) | |
{ | |
const char *output_pdf = next_file_name(input_pdf); | |
fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); | |
fz_try(ctx) | |
{ | |
pdf_document *doc = pdf_open_document(ctx, input_pdf); | |
fz_page *page = fz_load_page(ctx, (fz_document *)doc, 0); | |
pdf_page *pdfpage = (pdf_page *)page; | |
fz_point points[3] = { | |
{100, 500}, | |
{150, 520}, | |
{180, 580}}; | |
pdf_annot *annot = pdf_create_annot(ctx, pdfpage, PDF_ANNOT_INK); | |
fz_colorspace *cs = fz_device_rgb(ctx); | |
float color[3] = {0.57f, 0.25f, 0.67f}; | |
pdf_set_annot_color(ctx, annot, 3, color); | |
pdf_set_annot_border(ctx, annot, 2.0f); | |
pdf_obj *obj = pdf_annot_obj(ctx, annot); | |
pdf_dict_puts(ctx, obj, PDF_NAME(CA), pdf_new_real(ctx, 0.5f)); | |
pdf_obj *inklist = pdf_new_array(ctx, doc, 1); | |
pdf_obj *stroke = pdf_new_array(ctx, doc, 6); | |
for (int i = 0; i < 3; i++) | |
{ | |
pdf_array_push(ctx, stroke, pdf_new_real(ctx, points[i].x)); | |
pdf_array_push(ctx, stroke, pdf_new_real(ctx, points[i].y)); | |
} | |
pdf_array_push(ctx, inklist, stroke); | |
pdf_dict_puts(ctx, obj, PDF_NAME(InkList), inklist); | |
pdf_update_annot(ctx, annot); | |
fz_output *output = fz_new_output_with_path(ctx, output_pdf, 0); | |
fz_document_writer *writer = fz_new_pdf_writer_with_output(ctx, output, NULL); | |
fz_write_document(ctx, writer, (fz_document *)doc); | |
fz_close_document_writer(ctx, writer); | |
fz_close_output(ctx, output); | |
fz_drop_page(ctx, page); | |
fz_drop_document(ctx, &doc->super); | |
} | |
fz_catch(ctx) | |
{ | |
d("Error: %s", fz_caught_message(ctx)); | |
} | |
fz_drop_context(ctx); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment