Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/tools/pdfpages.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // Copyright (C) 2004-2021 Artifex Software, Inc. | |
| 2 // | |
| 3 // This file is part of MuPDF. | |
| 4 // | |
| 5 // MuPDF is free software: you can redistribute it and/or modify it under the | |
| 6 // terms of the GNU Affero General Public License as published by the Free | |
| 7 // Software Foundation, either version 3 of the License, or (at your option) | |
| 8 // any later version. | |
| 9 // | |
| 10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| 12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | |
| 13 // details. | |
| 14 // | |
| 15 // You should have received a copy of the GNU Affero General Public License | |
| 16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> | |
| 17 // | |
| 18 // Alternative licensing terms are available from the licensor. | |
| 19 // For commercial licensing, see <https://www.artifex.com/> or contact | |
| 20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, | |
| 21 // CA 94129, USA, for further information. | |
| 22 | |
| 23 /* | |
| 24 * Information tool. | |
| 25 * Print information about pages of a pdf. | |
| 26 */ | |
| 27 | |
| 28 #include "mupdf/fitz.h" | |
| 29 #include "mupdf/pdf.h" | |
| 30 | |
| 31 #include <stdlib.h> | |
| 32 #include <stdio.h> | |
| 33 | |
| 34 static int | |
| 35 infousage(void) | |
| 36 { | |
| 37 fprintf(stderr, | |
| 38 "usage: mutool pages [options] file.pdf [pages]\n" | |
| 39 "\t-p -\tpassword for decryption\n" | |
| 40 "\tpages\tcomma separated list of page numbers and ranges\n" | |
| 41 ); | |
| 42 return 1; | |
| 43 } | |
| 44 | |
| 45 static int | |
| 46 showbox(fz_context *ctx, fz_output *out, pdf_obj *page, char *text, pdf_obj *name) | |
| 47 { | |
| 48 fz_rect bbox; | |
| 49 pdf_obj *obj; | |
| 50 int failed = 0; | |
| 51 | |
| 52 fz_try(ctx) | |
| 53 { | |
| 54 obj = pdf_dict_get(ctx, page, name); | |
| 55 if (!pdf_is_array(ctx, obj)) | |
| 56 break; | |
| 57 | |
| 58 bbox = pdf_to_rect(ctx, obj); | |
| 59 | |
| 60 fz_write_printf(ctx, out, "<%s l=\"%g\" b=\"%g\" r=\"%g\" t=\"%g\" />\n", text, bbox.x0, bbox.y0, bbox.x1, bbox.y1); | |
| 61 } | |
| 62 fz_catch(ctx) | |
| 63 { | |
| 64 failed = 1; | |
| 65 } | |
| 66 | |
| 67 return failed; | |
| 68 } | |
| 69 | |
| 70 static int | |
| 71 shownum(fz_context *ctx, fz_output *out, pdf_obj *page, char *text, pdf_obj *name) | |
| 72 { | |
| 73 pdf_obj *obj; | |
| 74 int failed = 0; | |
| 75 | |
| 76 fz_try(ctx) | |
| 77 { | |
| 78 obj = pdf_dict_get(ctx, page, name); | |
| 79 if (!pdf_is_number(ctx, obj)) | |
| 80 break; | |
| 81 | |
| 82 fz_write_printf(ctx, out, "<%s v=\"%g\" />\n", text, pdf_to_real(ctx, obj)); | |
| 83 } | |
| 84 fz_catch(ctx) | |
| 85 { | |
| 86 failed = 1; | |
| 87 } | |
| 88 | |
| 89 return failed; | |
| 90 } | |
| 91 | |
| 92 static int | |
| 93 showpage(fz_context *ctx, pdf_document *doc, fz_output *out, int page) | |
| 94 { | |
| 95 pdf_obj *pageref; | |
| 96 int failed = 0; | |
| 97 | |
| 98 fz_write_printf(ctx, out, "<page pagenum=\"%d\">\n", page); | |
| 99 fz_try(ctx) | |
| 100 { | |
| 101 pageref = pdf_lookup_page_obj(ctx, doc, page-1); | |
| 102 if (!pageref) | |
| 103 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot retrieve info from page %d", page); | |
| 104 } | |
| 105 fz_catch(ctx) | |
| 106 { | |
| 107 fz_write_printf(ctx, out, "Failed to gather information for page %d\n", page); | |
| 108 failed = 1; | |
| 109 } | |
| 110 | |
| 111 if (!failed) | |
| 112 { | |
| 113 failed |= showbox(ctx, out, pageref, "MediaBox", PDF_NAME(MediaBox)); | |
| 114 failed |= showbox(ctx, out, pageref, "CropBox", PDF_NAME(CropBox)); | |
| 115 failed |= showbox(ctx, out, pageref, "ArtBox", PDF_NAME(ArtBox)); | |
| 116 failed |= showbox(ctx, out, pageref, "BleedBox", PDF_NAME(BleedBox)); | |
| 117 failed |= showbox(ctx, out, pageref, "TrimBox", PDF_NAME(TrimBox)); | |
| 118 failed |= shownum(ctx, out, pageref, "Rotate", PDF_NAME(Rotate)); | |
| 119 failed |= shownum(ctx, out, pageref, "UserUnit", PDF_NAME(UserUnit)); | |
| 120 } | |
| 121 | |
| 122 fz_write_printf(ctx, out, "</page>\n"); | |
| 123 | |
| 124 return failed; | |
| 125 } | |
| 126 | |
| 127 static int | |
| 128 showpages(fz_context *ctx, pdf_document *doc, fz_output *out, const char *pagelist) | |
| 129 { | |
| 130 int page, spage, epage; | |
| 131 int pagecount; | |
| 132 int ret = 0; | |
| 133 | |
| 134 if (!doc) | |
| 135 return infousage(); | |
| 136 | |
| 137 pagecount = pdf_count_pages(ctx, doc); | |
| 138 while ((pagelist = fz_parse_page_range(ctx, pagelist, &spage, &epage, pagecount))) | |
| 139 { | |
| 140 int fail; | |
| 141 if (spage > epage) | |
| 142 page = spage, spage = epage, epage = page; | |
| 143 for (page = spage; page <= epage; page++) | |
| 144 { | |
| 145 fail = showpage(ctx, doc, out, page); | |
| 146 /* On the first failure, check for the pagecount having changed. */ | |
| 147 if (fail && !ret) | |
| 148 { | |
| 149 pagecount = pdf_count_pages(ctx, doc); | |
| 150 if (epage > pagecount) | |
| 151 epage = pagecount; | |
| 152 } | |
| 153 ret |= fail; | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 return ret; | |
| 158 } | |
| 159 | |
| 160 static int | |
| 161 pdfpages_pages(fz_context *ctx, fz_output *out, char *filename, char *password, char *argv[], int argc) | |
| 162 { | |
| 163 enum { NO_FILE_OPENED, NO_INFO_GATHERED, INFO_SHOWN } state; | |
| 164 int argidx = 0; | |
| 165 pdf_document *doc = NULL; | |
| 166 int ret = 0; | |
| 167 | |
| 168 state = NO_FILE_OPENED; | |
| 169 while (argidx < argc) | |
| 170 { | |
| 171 if (state == NO_FILE_OPENED || !fz_is_page_range(ctx, argv[argidx])) | |
| 172 { | |
| 173 if (state == NO_INFO_GATHERED) | |
| 174 { | |
| 175 showpages(ctx, doc, out, "1-N"); | |
| 176 } | |
| 177 | |
| 178 pdf_drop_document(ctx, doc); | |
| 179 | |
| 180 filename = argv[argidx]; | |
| 181 fz_write_printf(ctx, out, "%s:\n", filename); | |
| 182 doc = pdf_open_document(ctx, filename); | |
| 183 if (pdf_needs_password(ctx, doc)) | |
| 184 if (!pdf_authenticate_password(ctx, doc, password)) | |
| 185 fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot authenticate password: %s", filename); | |
| 186 | |
| 187 state = NO_INFO_GATHERED; | |
| 188 } | |
| 189 else | |
| 190 { | |
| 191 ret |= showpages(ctx, doc, out, argv[argidx]); | |
| 192 state = INFO_SHOWN; | |
| 193 } | |
| 194 | |
| 195 argidx++; | |
| 196 } | |
| 197 | |
| 198 if (state == NO_INFO_GATHERED) | |
| 199 showpages(ctx, doc, out, "1-N"); | |
| 200 | |
| 201 pdf_drop_document(ctx, doc); | |
| 202 | |
| 203 return ret; | |
| 204 } | |
| 205 | |
| 206 int pdfpages_main(int argc, char **argv) | |
| 207 { | |
| 208 char *filename = ""; | |
| 209 char *password = ""; | |
| 210 int c; | |
| 211 int ret; | |
| 212 fz_context *ctx; | |
| 213 | |
| 214 while ((c = fz_getopt(argc, argv, "p:")) != -1) | |
| 215 { | |
| 216 switch (c) | |
| 217 { | |
| 218 case 'p': password = fz_optarg; break; | |
| 219 default: | |
| 220 return infousage(); | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 if (fz_optind == argc) | |
| 225 return infousage(); | |
| 226 | |
| 227 ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); | |
| 228 if (!ctx) | |
| 229 { | |
| 230 fprintf(stderr, "cannot initialise context\n"); | |
| 231 exit(1); | |
| 232 } | |
| 233 | |
| 234 ret = 0; | |
| 235 fz_try(ctx) | |
| 236 ret = pdfpages_pages(ctx, fz_stdout(ctx), filename, password, &argv[fz_optind], argc-fz_optind); | |
| 237 fz_catch(ctx) | |
| 238 { | |
| 239 fz_report_error(ctx); | |
| 240 ret = 1; | |
| 241 } | |
| 242 fz_drop_context(ctx); | |
| 243 return ret; | |
| 244 } |
