From 3ce14bcbf198fc93365d664b0db5745680967764 Mon Sep 17 00:00:00 2001 From: Rodrigo Moya Date: Thu, 28 Apr 2011 10:24:05 +0200 Subject: [PATCH] glib: Add _set functions for some PopplerDocument properties --- glib/poppler-document.cc | 154 ++++++++++++++++++++++++++++++++++++++++++++++ glib/poppler-document.h | 6 ++ 2 files changed, 160 insertions(+), 0 deletions(-) diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 710a5b3..360aa42 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -607,6 +607,46 @@ char *_poppler_goo_string_to_utf8(GooString *s) return result; } +GooString *_poppler_goo_string_from_utf8(const gchar *source) +{ + char *s, *utf8; + gsize bytes_written; + GooString *result; + + utf8 = g_locale_to_utf8 (source, strlen (source), NULL, &bytes_written, NULL); + s = g_convert (utf8, bytes_written, + "UTF-16BE", "UTF-8", NULL, &bytes_written, NULL); + + result = new GooString (s, bytes_written); + if (!result->hasUnicodeMarker()) { + result->insert(0, 0xff); + result->insert(0, 0xfe); + } + + g_free (s); + g_free (utf8); + + return result; +} + +static gboolean +mark_as_modified (PopplerDocument *document, Object *obj) +{ + gboolean result = FALSE; + XRef *xref; + Object info_dict; + + xref = document->doc->getXRef (); + if (xref != NULL) { + xref->getDocInfoNF (&info_dict); + xref->setModifiedObject (obj, info_dict.getRef ()); + + result = TRUE; + } + + return result; +} + static gchar * info_dict_get_string (Dict *info_dict, const gchar *key) { @@ -763,6 +803,44 @@ poppler_document_get_title (PopplerDocument *document) } /** + * poppler_document_set_title: + * @document: A #PopplerDocument + * + * Sets the document's title + * + * Return value: whether the operation succeedded or not + * + * Since: 0.17 + **/ +gboolean +poppler_document_set_title (PopplerDocument *document, const gchar *title) +{ + Object obj; + gboolean result = FALSE; + + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), FALSE); + g_return_val_if_fail (title != NULL, FALSE); + + document->doc->getDocInfo (&obj); + if (obj.isDict ()) { + GooString *g_title; + Object obj_s; + + + g_title = _poppler_goo_string_from_utf8 (title); + obj_s.initString (g_title); + obj.dictSet ("Title", &obj_s); + + /* Now mark the document as modified */ + result = mark_as_modified (document, &obj); + } + + obj.free (); + + return result; +} + +/** * poppler_document_get_author: * @document: A #PopplerDocument * @@ -790,6 +868,44 @@ poppler_document_get_author (PopplerDocument *document) } /** + * poppler_document_set_author: + * @document: A #PopplerDocument + * + * Sets the document's author + * + Return value: whether the operation succeedded or not + * + * Since: 0.17 + **/ +gboolean +poppler_document_set_author (PopplerDocument *document, const gchar *author) +{ + Object obj; + gboolean result = FALSE; + + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), FALSE); + g_return_val_if_fail (author != NULL, FALSE); + + document->doc->getDocInfo (&obj); + if (obj.isDict ()) { + GooString *g_title; + Object obj_s; + + + g_title = _poppler_goo_string_from_utf8 (author); + obj_s.initString (g_title); + obj.dictSet ("Author", &obj_s); + + /* Now mark the document as modified */ + result = mark_as_modified (document, &obj); + } + + obj.free (); + + return result; +} + +/** * poppler_document_get_subject: * @document: A #PopplerDocument * @@ -817,6 +933,44 @@ poppler_document_get_subject (PopplerDocument *document) } /** + * poppler_document_set_subject: + * @document: A #PopplerDocument + * + * Sets the document's subject + * + Return value: whether the operation succeedded or not + * + * Since: 0.17 + **/ +gboolean +poppler_document_set_subject (PopplerDocument *document, const gchar *subject) +{ + Object obj; + gboolean result = FALSE; + + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), FALSE); + g_return_val_if_fail (subject != NULL, FALSE); + + document->doc->getDocInfo (&obj); + if (obj.isDict ()) { + GooString *g_title; + Object obj_s; + + + g_title = _poppler_goo_string_from_utf8 (subject); + obj_s.initString (g_title); + obj.dictSet ("Subject", &obj_s); + + /* Now mark the document as modified */ + result = mark_as_modified (document, &obj); + } + + obj.free (); + + return result; +} + +/** * poppler_document_get_keywords: * @document: A #PopplerDocument * diff --git a/glib/poppler-document.h b/glib/poppler-document.h index 5cec9ae..ec0d4d3 100644 --- a/glib/poppler-document.h +++ b/glib/poppler-document.h @@ -183,8 +183,14 @@ void poppler_document_get_pdf_version (PopplerDocument *doc guint *major_version, guint *minor_version); gchar *poppler_document_get_title (PopplerDocument *document); +gboolean poppler_document_set_title (PopplerDocument *document, + const gchar *title); gchar *poppler_document_get_author (PopplerDocument *document); +gboolean poppler_document_set_author (PopplerDocument *document, + const gchar *author); gchar *poppler_document_get_subject (PopplerDocument *document); +gboolean poppler_document_set_subject (PopplerDocument *document, + const gchar *subject); gchar *poppler_document_get_keywords (PopplerDocument *document); gchar *poppler_document_get_creator (PopplerDocument *document); gchar *poppler_document_get_producer (PopplerDocument *document); -- 1.7.2.3