static pdf_document *test_creation(juggler_t *src) { /* create new document */ pdf_document *new_doc = pdf_create_document(src->ctx); size_t i; size_t sheets_count = src->pagecount / 2 + (src->pagecount % 2); for(i = 0; i < sheets_count; i++) juggler_impose_create_sheet(src->ctx, new_doc, 595.27 * 2, 842); return(new_doc); }
PdfCreator::PdfCreator() { ctx = fz_new_context(nullptr, nullptr, FZ_STORE_DEFAULT); if (!ctx) return; fz_try(ctx) { doc = pdf_create_document(ctx); } fz_catch(ctx) { doc = nullptr; } }
static int make_fake_doc(pdfapp_t *app) { fz_context *ctx = app->ctx; pdf_document *pdf = NULL; fz_buffer *contents = NULL; pdf_obj *page_obj = NULL; fz_var(contents); fz_var(page_obj); fz_try(ctx) { fz_rect mediabox = { 0, 0, app->winw, app->winh }; int i; pdf = pdf_create_document(ctx); contents = fz_new_buffer(ctx, 100); fz_append_printf(ctx, contents, "1 0 0 RG %g w 0 0 m %g %g l 0 %g m %g 0 l s\n", fz_min(mediabox.x1, mediabox.y1) / 20, mediabox.x1, mediabox.y1, mediabox.y1, mediabox.x1); /* Create enough copies of our blank(ish) page so that the * page number is preserved if and when a subsequent load * works. */ page_obj = pdf_add_page(ctx, pdf, &mediabox, 0, NULL, contents); for (i = 0; i < app->pagecount; i++) pdf_insert_page(ctx, pdf, -1, page_obj); } fz_always(ctx) { pdf_drop_obj(ctx, page_obj); fz_drop_buffer(ctx, contents); } fz_catch(ctx) { fz_drop_document(ctx, (fz_document *) pdf); return 1; } app->doc = (fz_document*)pdf; return 0; }
static int make_fake_doc(pdfapp_t *app) { fz_context *ctx = app->ctx; pdf_document *pdf = NULL; fz_buffer *contents = NULL; fz_try(ctx) { fz_rect mediabox = { 0, 0, app->winw, app->winh }; pdf_obj *page_obj; int i; contents = fz_new_buffer(ctx, 100); pdf = pdf_create_document(ctx); app->doc = (fz_document*)pdf; fz_buffer_printf(ctx, contents, "1 0 0 rg %f w 0 0 m %f %f l 0 %f m %f 0 l\n", fz_min(mediabox.x1, mediabox.y1) / 4, mediabox.x1, mediabox.y1, mediabox.y1, mediabox.x1); /* Create enough copies of our blank(ish) page so that the * page number is preserved if and when a subsequent load * works. */ page_obj = pdf_add_page(ctx, pdf, &mediabox, 0, contents, NULL); for (i = 0; i < app->pagecount; i++) pdf_insert_page(ctx, pdf, -1, page_obj); pdf_drop_obj(ctx, page_obj); } fz_always(ctx) { fz_drop_buffer(ctx, contents); } fz_catch(ctx) { fz_rethrow(ctx); } return 0; }
int pdfportfolio_main(int argc, char **argv) { char *password = ""; char *outfile = NULL; char *outopts = "compress"; char *infile; int exit_code = 0; int do_save = 0; int has_old_file = 0; int c; while ((c = fz_getopt(argc, argv, "p:o:O:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'o': outfile = fz_optarg; break; case 'O': outopts = fz_optarg; break; default: usage(); break; } } if (fz_optind == argc) usage(); infile = argv[fz_optind++]; if (!outfile) outfile = infile; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } if (fz_file_exists(ctx, infile)) { doc = pdf_open_document(ctx, infile); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); has_old_file = 1; } else { doc = pdf_create_document(ctx); /* add a blank page */ { const char *template = "BT /Tm 16 Tf 50 434 TD (This is a portfolio document.) Tj ET\n"; const unsigned char *data; int size; fz_font *font; pdf_obj *font_obj, *page_obj; pdf_obj *resources; fz_buffer *contents; fz_rect mediabox = { 0, 0, 400, 500 }; data = fz_lookup_base14_font(ctx, "Times-Roman", &size); font = fz_new_font_from_memory(ctx, "Times-Roman", data, size, 0, 0); font_obj = pdf_add_simple_font(ctx, doc, font, PDF_SIMPLE_ENCODING_LATIN); fz_drop_font(ctx, font); resources = pdf_add_new_dict(ctx, doc, 1); pdf_dict_putp_drop(ctx, resources, "Font/Tm", font_obj); contents = fz_new_buffer_from_shared_data(ctx, (const unsigned char *)template, strlen(template));
static int make_fake_doc(pdfapp_t *app) { fz_context *ctx = app->ctx; fz_matrix ctm = { 1, 0, 0, 1, 0, 0 }; fz_rect bounds; pdf_page *newpage = NULL; pdf_document *pdf = NULL; fz_device *dev = NULL; fz_path *path = NULL; fz_stroke_state stroke = fz_default_stroke_state; float red[3] = { 1, 0, 0 }; int i; fz_var(pdf); fz_var(dev); fz_var(newpage); fz_try(ctx) { pdf = pdf_create_document(ctx); app->doc = &pdf->super; bounds.x0 = 0; bounds.y0 = 0; bounds.x1 = app->winw; bounds.y1 = app->winh; newpage = pdf_create_page(ctx, pdf, bounds, 72, 0); dev = pdf_page_write(ctx, pdf, newpage); /* Now the page content */ fz_begin_page(ctx, dev, &bounds, &ctm); path = fz_new_path(ctx); fz_moveto(ctx, path, 0, 0); fz_lineto(ctx, path, bounds.x1, bounds.y1); fz_moveto(ctx, path, 0, bounds.y1); fz_lineto(ctx, path, bounds.x1, 0); stroke.linewidth = fz_min(bounds.x1, bounds.y1)/4; fz_stroke_path(ctx, dev, path, &stroke, &ctm, fz_device_rgb(ctx), red, 1); fz_end_page(ctx, dev); fz_drop_device(ctx, dev); dev = NULL; /* Create enough copies of our blank(ish) page so that the * page number is preserved if and when a subsequent load * works. */ for (i = 0; i < app->pagecount; i++) pdf_insert_page(ctx, pdf, newpage, INT_MAX); } fz_always(ctx) { fz_drop_path(ctx, path); pdf_drop_page(ctx, newpage); fz_drop_device(ctx, dev); dev = NULL; } fz_catch(ctx) { fz_rethrow(ctx); } return 0; }
int pdfmerge_main(int argc, char **argv) { pdf_write_options opts = { 0 }; char *output = "out.pdf"; char *infile_src; int c; while ((c = fz_getopt(argc, argv, "adlszo:")) != -1) { switch (c) { case 'o': output = fz_optarg; break; case 'a': opts.do_ascii ++; break; case 'd': opts.do_expand ^= PDF_EXPAND_ALL; break; case 'l': opts.do_linear ++; break; case 's': opts.do_clean ++; break; case 'z': opts.do_deflate ++; break; default: usage(); break; } } if (fz_optind == argc) usage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "Cannot initialise context\n"); exit(1); } fz_try(ctx) { doc_des = pdf_create_document(ctx); } fz_catch(ctx) { fprintf(stderr, "Failed to allocate destination document file %s\n", output); exit(1); } /* Step through the source files */ while (fz_optind < argc) { fz_try(ctx) { infile_src = argv[fz_optind++]; pdf_drop_document(ctx, doc_src); doc_src = pdf_open_document(ctx, infile_src); if (fz_optind == argc || !isrange(argv[fz_optind])) merge_range("1-"); else merge_range(argv[fz_optind++]); } fz_catch(ctx) { fprintf(stderr, "Failed merging document %s\n", infile_src); exit(1); } } fz_try(ctx) { pdf_save_document(ctx, doc_des, output, &opts); } fz_always(ctx) { pdf_drop_document(ctx, doc_des); pdf_drop_document(ctx, doc_src); } fz_catch(ctx) { fprintf(stderr, "Error encountered during file save.\n"); exit(1); } fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
int pdfmerge_main(int argc, char **argv) { pdf_write_options opts = { 0 }; char *output = "out.pdf"; char *flags = ""; char *input; int c; while ((c = fz_getopt(argc, argv, "o:O:")) != -1) { switch (c) { case 'o': output = fz_optarg; break; case 'O': flags = fz_optarg; break; default: usage(); break; } } if (fz_optind == argc) usage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "error: Cannot initialize MuPDF context.\n"); exit(1); } pdf_parse_write_options(ctx, &opts, flags); fz_try(ctx) { doc_des = pdf_create_document(ctx); } fz_catch(ctx) { fprintf(stderr, "error: Cannot create destination document.\n"); exit(1); } /* Step through the source files */ while (fz_optind < argc) { input = argv[fz_optind++]; fz_try(ctx) { pdf_drop_document(ctx, doc_src); doc_src = pdf_open_document(ctx, input); if (fz_optind == argc || !fz_is_page_range(ctx, argv[fz_optind])) merge_range("1-N"); else merge_range(argv[fz_optind++]); } fz_catch(ctx) { fprintf(stderr, "error: Cannot merge document '%s'.\n", input); exit(1); } } fz_try(ctx) { pdf_save_document(ctx, doc_des, output, &opts); } fz_always(ctx) { pdf_drop_document(ctx, doc_des); pdf_drop_document(ctx, doc_src); } fz_catch(ctx) { fprintf(stderr, "error: Cannot save output file: '%s'.\n", output); exit(1); } fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }