From 5d05eae947e7c2b20106970cb69034b9fc828fcf Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Sun, 3 Nov 2013 21:32:27 +1030 Subject: [PATCH 04/11] glib API for PDFWriter --- glib/poppler-document.cc | 399 +++++++++++++++++++++++++++++++++++++++++++++++ glib/poppler-document.h | 43 ++++- glib/poppler-private.h | 10 ++ glib/poppler.h | 27 ++++ 4 files changed, 478 insertions(+), 1 deletion(-) diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 61d92e8..1dcb5be 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -2718,3 +2718,402 @@ _poppler_convert_pdf_date_to_gtime (GooString *date, return retval; } + +typedef struct _PopplerPDFFileClass PopplerPDFFileClass; +struct _PopplerPDFFileClass +{ + GObjectClass parent_class; +}; + +G_DEFINE_TYPE (PopplerPDFFile, poppler_pdf_file, G_TYPE_OBJECT) + +static void poppler_pdf_file_finalize (GObject *object); + + +static void +poppler_pdf_file_class_init (PopplerPDFFileClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = poppler_pdf_file_finalize; +} + +static void +poppler_pdf_file_init (PopplerPDFFile *pdf_file) +{ +} + +static void +poppler_pdf_file_finalize (GObject *object) +{ + PopplerPDFFile *pdf_file = POPPLER_PDF_FILE (object); + + delete pdf_file->writer; + g_object_unref (pdf_file->document); + + G_OBJECT_CLASS (poppler_pdf_file_parent_class)->finalize (object); +} + +/** + * poppler_pdf_file_new: + * @document: a #PopplerDocument + * + * Create a new PDF file for writing the document with printing options + * + * Return value: a PopplerPDFFile + **/ +PopplerPDFFile * +poppler_pdf_file_new (PopplerDocument *document) +{ + PopplerPDFFile *pdf_file; + + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); + + pdf_file = (PopplerPDFFile *) g_object_new (POPPLER_TYPE_PDF_FILE, NULL); + pdf_file->document = (PopplerDocument *) g_object_ref (document); + pdf_file->writer = new PDFWriter(document->doc); + + return pdf_file; +} + +/** + * poppler_pdf_file_set_num_copies: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @copies: number of copies to print + * + * Set the number of copies to print + * + **/ +void +poppler_pdf_file_set_num_copies (PopplerPDFFile *pdf_file, + gint copies) +{ + pdf_file->writer->setNumCopies(copies); +} + +/** + * poppler_pdf_file_set_collate: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @collate: whether to collate the printed pages + * + * Enable or disable collating of multiple copies. + * + **/ +void +poppler_pdf_file_set_collate (PopplerPDFFile *pdf_file, + gboolean collate) +{ + pdf_file->writer->setCollate(collate); +} + +/** + * poppler_pdf_file_set_reverse: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @reverse: whether to reverse the order of the printed pages + * + * Enable or disable reversing order of printed pages. + * + **/ +void +poppler_pdf_file_set_reverse (PopplerPDFFile *pdf_file, + gboolean reverse) +{ + pdf_file->writer->setReverse(reverse); +} + +/** + * poppler_pdf_file_set_duplex: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @duplex: whether to print duplex + * + * When enabled and multiple copies are being printed, a blank page is + * inserted at the end of each copy with an odd number of pages to + * ensure that the first page of each copy begins on a new sheet. Note + * that enabling this option will not cause the printer to print in + * duplex. + * + **/ +void +poppler_pdf_file_set_duplex (PopplerPDFFile *pdf_file, + gboolean duplex) +{ + pdf_file->writer->setDuplex(duplex); +} + +/** + * poppler_pdf_file_set_page_set: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @page_set: the set of pages to be printed + * + * Specify whether to print all pages, or only odd or even numbered pages. + * + **/ +void +poppler_pdf_file_set_page_set (PopplerPDFFile *pdf_file, + PopplerPageSet page_set) +{ + PDFWriter::PageSet set; + switch (page_set) { + case POPPLER_PAGESET_ALL: + set = PDFWriter::ALL; + break; + case POPPLER_PAGESET_ODD: + set = PDFWriter::ODD; + break; + case POPPLER_PAGESET_EVEN: + set = PDFWriter::EVEN; + break; + } + pdf_file->writer->setPageSet(set); +} + +/** + * poppler_pdf_file_set_number_up: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @number_up: the number of pages to print on each sheet + * + * Set the number of pages to print on each sheet. Setting number_up + * to 1 disables number-up printing. + * + **/ +void +poppler_pdf_file_set_number_up (PopplerPDFFile *pdf_file, + gint number_up) +{ + if (number_up != 1 && + number_up != 2 && + number_up != 4 && + number_up != 6 && + number_up != 9 && + number_up != 16) + number_up = 1; + + pdf_file->writer->setNumberUp(number_up); +} + +/** + * poppler_pdf_file_set_number_up_layout: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @layout: the layout to use in number-up mode + * + * Set the layout to use in number-up mode. + * + **/ +void +poppler_pdf_file_set_number_up_layout (PopplerPDFFile *pdf_file, + PopplerNumberUpLayout layout) + +{ + PDFWriter::NumberUpLayout order; + switch (layout) { + case POPPLER_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM: + order = PDFWriter::LEFT_TO_RIGHT_TOP_TO_BOTTOM; + break; + case POPPLER_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP: + order = PDFWriter::LEFT_TO_RIGHT_BOTTOM_TO_TOP; + break; + case POPPLER_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM: + order = PDFWriter::RIGHT_TO_LEFT_TOP_TO_BOTTOM; + break; + case POPPLER_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP: + order = PDFWriter::RIGHT_TO_LEFT_BOTTOM_TO_TOP; + break; + case POPPLER_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT: + order = PDFWriter::TOP_TO_BOTTOM_LEFT_TO_RIGHT; + break; + case POPPLER_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT: + order = PDFWriter::TOP_TO_BOTTOM_RIGHT_TO_LEFT; + break; + case POPPLER_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT: + order = PDFWriter::BOTTOM_TO_TOP_LEFT_TO_RIGHT; + break; + case POPPLER_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT: + order = PDFWriter::BOTTOM_TO_TOP_RIGHT_TO_LEFT; + break; + } + pdf_file->writer->setNumberUpLayout(order); +} + +/** + * poppler_pdf_file_add_paper_size: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @width: the paper width in 1/72 inch + * @height: the paper height in 1/72 inch + * @top_margin: the top margin in 1/72 inch + * @bottom_margin: the top margin in 1/72 inch + * @left_margin: the top margin in 1/72 inch + * @right_margin: the top margin in 1/72 inch + * + * Add a paper size with the specified margins. When number-up is disabled: + * - If no paper sizes are added, the output size of each page is the same as the original. + * - If one paper size is added, all pages will be printed to this paper size. + * - If more than one paper size is added, each output page will be printed to the closest + * matching page size. + * + * When number-up is enabled, only one paper size should be added. All output sheets will + * use this page size. + * + **/ +void +poppler_pdf_file_add_paper_size (PopplerPDFFile *pdf_file, + gdouble width, + gdouble height, + gdouble top_margin, + gdouble bottom_margin, + gdouble left_margin, + gdouble right_margin) +{ + pdf_file->writer->addPaperSize(width, height, + top_margin, bottom_margin, + left_margin, right_margin); +} + +/** + * poppler_pdf_file_set_scale: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @scale: the scale to print each each page (1.0 = original size) + * + * Set the scale to print each page. Note that setting any resize options + * other than POPPLER_RESIZE_NONE may alter this scale. + * + **/ +void +poppler_pdf_file_set_scale (PopplerPDFFile *pdf_file, + gdouble scale) +{ + pdf_file->writer->setScale(scale); +} + +/** + * poppler_pdf_file_set_orientation: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @orientation: the orientation of the printed pages + * + * When the autorotate option is disabled, this option sets the orientation + * of the printed pages. + * + **/ +void +poppler_pdf_file_set_orientation (PopplerPDFFile *pdf_file, + PopplerOrientation orientation) +{ + PDFWriter::Orientation orient; + switch (orientation) { + case POPPLER_ORIENTATION_PORTRAIT: + orient = PDFWriter::PORTRAIT; + break; + case POPPLER_ORIENTATION_LANDSCAPE: + orient = PDFWriter::LANDSCAPE; + break; + case POPPLER_ORIENTATION_UPSIDEDOWN: + orient = PDFWriter::REVERSE_PORTRAIT; + break; + case POPPLER_ORIENTATION_SEASCAPE: + orient = PDFWriter::REVERSE_LANDSCAPE; + break; + } + pdf_file->writer->setOrientation(orient); +} + +/** + * poppler_pdf_file_set_resize: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @resize: the resize option + * + * Specified what to do when the source page is larger or smaller than + * the output page. The POPPLER_RESIZE_NONE option will ensure the page is always + * printed at the original size. The POPPLER_RESIZE_SHRINK will shrink over size + * pages to fit the output page. Undersized pages are not altered. The + * POPPLER_RESIZE_FIT option will scale the source page up or down as required + * to fit the output page. + * + **/ +void +poppler_pdf_file_set_resize (PopplerPDFFile *pdf_file, + PopplerResize resize) +{ + PDFWriter::Resize size; + switch (resize) { + case POPPLER_RESIZE_NONE: + size = PDFWriter::NONE; + break; + case POPPLER_RESIZE_SHRINK: + size = PDFWriter::SHRINK; + break; + case POPPLER_RESIZE_FIT: + size = PDFWriter::FIT; + break; + } + pdf_file->writer->setResize(size); +} + +/** + * poppler_pdf_file_set_auto_rotate: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @auto_rotate: whether to change the orientation of the printed pages to match the + * orientation of the souce page + * + * When the auto rotate option is enabled, each printed pages will have the same + * orientation as the source page. + * + **/ +void +poppler_pdf_file_set_auto_rotate (PopplerPDFFile *pdf_file, + gboolean auto_rotate) +{ + pdf_file->writer->setAutoRotate(auto_rotate); +} + +/** + * poppler_pdf_file_set_center: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @center: whether to center each page on the printed pages + * + * When center is enabled, pages that are smaller than the output page size will + * be centered on the output page. + * + **/ +void +poppler_pdf_file_set_center (PopplerPDFFile *pdf_file, + gboolean center) +{ + pdf_file->writer->setCenter(center); +} + +/** + * poppler_pdf_file_add_page: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @page: the page number to print + * + * Included the specified page in the list of pages to be printed. + * This function should be called for each page number to be printed. + * + **/ +void +poppler_pdf_file_add_page (PopplerPDFFile *pdf_file, + gint page) +{ + pdf_file->writer->addPage(page); +} + +/** + * poppler_pdf_file_write: + * @pdf_file: a PopplerPDFFile which was not yet been written + * @filename: the path of the output filename + * + * Print the PDF document to the specified filename. + * of the printed pages. + * + * Return value: %TRUE, if the document was successfully written + **/ +gboolean +poppler_pdf_file_write (PopplerPDFFile *pdf_file, + const char *filename) +{ + if (filename != NULL) { + GooString s(filename); + return pdf_file->writer->writeFile(&s); + } + + return FALSE; +} diff --git a/glib/poppler-document.h b/glib/poppler-document.h index a34e88c..42e38b5 100644 --- a/glib/poppler-document.h +++ b/glib/poppler-document.h @@ -292,7 +292,48 @@ void poppler_ps_file_set_duplex (PopplerPSFile *ps_file, gboolean duplex); void poppler_ps_file_free (PopplerPSFile *ps_file); - +/* Export to pdf */ +#define POPPLER_TYPE_PDF_FILE (poppler_pdf_file_get_type ()) +#define POPPLER_PDF_FILE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), POPPLER_TYPE_PDF_FILE, PopplerPDFFile)) +#define POPPLER_IS_PDF_FILE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), POPPLER_TYPE_PDF_FILE)) +GType poppler_pdf_file_get_type (void) G_GNUC_CONST; +PopplerPDFFile *poppler_pdf_file_new (PopplerDocument *document); +void poppler_pdf_file_set_num_copies (PopplerPDFFile *pdf_file, + gint copies); +void poppler_pdf_file_set_collate (PopplerPDFFile *pdf_file, + gboolean collate); +void poppler_pdf_file_set_reverse (PopplerPDFFile *pdf_file, + gboolean reverse); +void poppler_pdf_file_set_duplex (PopplerPDFFile *pdf_file, + gboolean duplex); +void poppler_pdf_file_set_page_set (PopplerPDFFile *pdf_file, + PopplerPageSet page_set); +void poppler_pdf_file_set_number_up (PopplerPDFFile *pdf_file, + gint number_up); +void poppler_pdf_file_set_number_up_layout (PopplerPDFFile *pdf_file, + PopplerNumberUpLayout layout); +void poppler_pdf_file_add_paper_size (PopplerPDFFile *pdf_file, + gdouble width, + gdouble height, + gdouble top_margin, + gdouble bottom_margin, + gdouble left_margin, + gdouble right_margin); +void poppler_pdf_file_set_scale (PopplerPDFFile *pdf_file, + gdouble scale); +void poppler_pdf_file_set_orientation (PopplerPDFFile *pdf_file, + PopplerOrientation orientation); +void poppler_pdf_file_set_resize (PopplerPDFFile *pdf_file, + PopplerResize resize); +void poppler_pdf_file_set_auto_rotate (PopplerPDFFile *pdf_file, + gboolean auto_rotate); +void poppler_pdf_file_set_center (PopplerPDFFile *pdf_file, + gboolean center); +void poppler_pdf_file_add_page (PopplerPDFFile *pdf_file, + gint page); +gboolean poppler_pdf_file_write (PopplerPDFFile *pdf_file, + const char *uri); +void poppler_pdf_file_free (PopplerPDFFile *ps_file); G_END_DECLS diff --git a/glib/poppler-private.h b/glib/poppler-private.h index ab39b49..7a47c52 100644 --- a/glib/poppler-private.h +++ b/glib/poppler-private.h @@ -17,6 +17,7 @@ #include #include #include +#include #endif struct _PopplerDocument @@ -45,6 +46,15 @@ struct _PopplerPSFile gboolean duplex; }; +struct _PopplerPDFFile +{ + /*< private >*/ + GObject parent_instance; + + PopplerDocument *document; + PDFWriter *writer; +}; + struct _PopplerFontInfo { /*< private >*/ diff --git a/glib/poppler.h b/glib/poppler.h index 2d190f3..bff188c 100644 --- a/glib/poppler.h +++ b/glib/poppler.h @@ -171,6 +171,32 @@ typedef enum /*< flags >*/ POPPLER_FIND_WHOLE_WORDS_ONLY = 1 << 2 } PopplerFindFlags; +typedef enum +{ + POPPLER_PAGESET_ALL, + POPPLER_PAGESET_ODD, + POPPLER_PAGESET_EVEN +} PopplerPageSet; + +typedef enum +{ + POPPLER_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM, + POPPLER_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP, + POPPLER_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM, + POPPLER_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP, + POPPLER_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT, + POPPLER_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT, + POPPLER_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT, + POPPLER_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT +} PopplerNumberUpLayout; + +typedef enum +{ + POPPLER_RESIZE_NONE, + POPPLER_RESIZE_SHRINK, + POPPLER_RESIZE_FIT +} PopplerResize; + typedef struct _PopplerDocument PopplerDocument; typedef struct _PopplerIndexIter PopplerIndexIter; typedef struct _PopplerFontsIter PopplerFontsIter; @@ -187,6 +213,7 @@ typedef struct _PopplerPage PopplerPage; typedef struct _PopplerFontInfo PopplerFontInfo; typedef struct _PopplerLayer PopplerLayer; typedef struct _PopplerPSFile PopplerPSFile; +typedef struct _PopplerPDFFile PopplerPDFFile; typedef union _PopplerAction PopplerAction; typedef struct _PopplerDest PopplerDest; typedef struct _PopplerActionLayer PopplerActionLayer; -- 1.8.3.2