Add bindings for getting/setting PDF page boxes

This commit is contained in:
Kovid Goyal 2019-11-22 08:51:49 +05:30
parent f44b5e8c6f
commit 3eb3d75bf5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -206,6 +206,66 @@ PDFDoc_delete_pages(PDFDoc *self, PyObject *args) {
Py_RETURN_NONE; Py_RETURN_NONE;
} // }}} } // }}}
// get_page_box() {{{
static PyObject *
PDFDoc_get_page_box(PDFDoc *self, PyObject *args) {
int pagenum = 0;
const char *which;
if (PyArg_ParseTuple(args, "si", &which, &pagenum)) {
try {
PdfPagesTree* tree = self->doc->GetPagesTree();
PdfPage* page = tree->GetPage(pagenum - 1);
if (!page) { PyErr_Format(PyExc_ValueError, "page number %d not found in PDF file", pagenum); return NULL; }
PdfRect rect;
if (strcmp(which, "MediaBox") == 0) {
rect = page->GetMediaBox();
} else if (strcmp(which, "CropBox") == 0) {
rect = page->GetCropBox();
} else if (strcmp(which, "TrimBox") == 0) {
rect = page->GetTrimBox();
} else if (strcmp(which, "BleedBox") == 0) {
rect = page->GetBleedBox();
} else if (strcmp(which, "ArtBox") == 0) {
rect = page->GetArtBox();
} else {
PyErr_Format(PyExc_KeyError, "%s is not a known box", which);
return NULL;
}
return Py_BuildValue("dddd", rect.GetLeft(), rect.GetBottom(), rect.GetWidth(), rect.GetHeight());
} catch(const PdfError & err) {
podofo_set_exception(err);
return NULL;
}
} else return NULL;
Py_RETURN_NONE;
} // }}}
// set_page_box() {{{
static PyObject *
PDFDoc_set_page_box(PDFDoc *self, PyObject *args) {
int pagenum = 0;
double left, bottom, width, height;
const char *which;
if (PyArg_ParseTuple(args, "sidddd", &which, &pagenum, left, bottom, width, height)) {
try {
PdfPagesTree* tree = self->doc->GetPagesTree();
PdfPage* page = tree->GetPage(pagenum - 1);
if (!page) { PyErr_Format(PyExc_ValueError, "page number %d not found in PDF file", pagenum); return NULL; }
PdfRect rect(left, bottom, width, height);
PdfObject box;
rect.ToVariant(box);
page->GetObject()->GetDictionary().AddKey(PdfName(which), box);
Py_RETURN_NONE;
} catch(const PdfError & err) {
podofo_set_exception(err);
return NULL;
}
} else return NULL;
Py_RETURN_NONE;
} // }}}
// copy_page() {{{ // copy_page() {{{
static PyObject * static PyObject *
PDFDoc_copy_page(PDFDoc *self, PyObject *args) { PDFDoc_copy_page(PDFDoc *self, PyObject *args) {
@ -737,6 +797,12 @@ static PyMethodDef PDFDoc_methods[] = {
{"delete_pages", (PyCFunction)PDFDoc_delete_pages, METH_VARARGS, {"delete_pages", (PyCFunction)PDFDoc_delete_pages, METH_VARARGS,
"delete_page(page_num, count=1) -> Delete the specified pages from the pdf." "delete_page(page_num, count=1) -> Delete the specified pages from the pdf."
}, },
{"get_page_box", (PyCFunction)PDFDoc_get_page_box, METH_VARARGS,
"get_page_box(which, page_num) -> Get the specified box for the specified page as (left, bottom, width, height)"
},
{"set_page_box", (PyCFunction)PDFDoc_set_page_box, METH_VARARGS,
"set_page_box(which, page_num, left, bottom, width, height) -> Set the specified box for the specified page."
},
{"copy_page", (PyCFunction)PDFDoc_copy_page, METH_VARARGS, {"copy_page", (PyCFunction)PDFDoc_copy_page, METH_VARARGS,
"copy_page(from, to) -> Copy the specified page." "copy_page(from, to) -> Copy the specified page."
}, },