Hi,
This Patch does the following:
1) gets and set the pdf IDs in the trailer dictionary.
2) get all page labels & get page labels by index.
Kindly let me know if You commit this patch.
Thanks
A Srinivas
Index: poppler-0.14.0/glib/poppler-document.cc
===================================================================
--- poppler-0.14.0/glib/poppler-document.cc (revision 194)
+++ poppler-0.14.0/glib/poppler-document.cc (working copy)
@@ -1948,3 +1948,167 @@
return retval;
}
+
+
+/* following three functions has added to get and set pdf ID stored in Trailer Dictionary*/
+static void
+get_id(char* id,char *buff) {
+
+ int i;
+ for( i = 0; i < 16; i++ ) {
+
+ char *buf = (char*)malloc(3*sizeof(char));
+ sprintf(buf,"%02x",id[i] & 0xff);
+
+ if(i == 0) strcpy(buff,buf);
+ else strcat(buff,buf);
+
+ free (buf);
+ }
+}
+
+gboolean
+poppler_document_get_trailer_id(char *uri,char *password,char *id[2]) {
+
+ PopplerDocument *popplerdoc = poppler_document_new_from_file(uri,password,NULL);
+
+ XRef *xref = popplerdoc->doc->getXRef();
+ Object *trailer = xref->getTrailerDict();
+
+ Object obj;
+ trailer->dictLookup("ID",&obj);
+
+ if(obj.isNull())
+ return FALSE;
+
+ Object val1,val2;
+ obj.arrayGet(0,&val1);
+ obj.arrayGet(1,&val2);
+
+ char *Id1 = val1.getString()->getCString();
+ char *Id2 = val2.getString()->getCString();
+
+ for(int i=0; i<2; i++) {
+ id[i] = (char*)malloc(32*sizeof(char));
+ }
+ get_id(Id1,id[0]);
+ get_id(Id2,id[1]);
+
+ g_free(Id1);
+ g_free(Id2);
+
+ return TRUE;
+}
+
+void *
+poppler_document_generate_and_set_id(char *uri,char *password) {
+
+ PopplerDocument *popplerdoc = poppler_document_new_from_file(uri,password,NULL);
+
+ char *filename;
+ filename = g_filename_from_uri(uri,NULL,NULL);
+
+ GooString message;
+ char buffer[256];
+ sprintf(buffer, "%i", (int)time(NULL));
+ message.append(buffer);
+ message.append(filename);
+
+ // file size
+ unsigned int fileSize = 0;
+
+ FILE *f;
+ f = fopen(filename,"r");
+ char ch;
+ while((ch=fgetc(f)) != EOF) {
+ fileSize++;
+ }
+
+ sprintf(buffer, "%i", fileSize);
+ message.append(buffer);
+
+ XRef *xref = popplerdoc->doc->getXRef();
+ Object obj1;
+
+ //info dict -- only use text string
+ if (xref->getDocInfo(&obj1)->isDict()) {
+
+ for(int i=0; i<obj1.getDict()->getLength(); i++) {
+ Object obj2;
+ obj1.getDict()->getVal(i, &obj2);
+ if (obj2.isString()) {
+ message.append(obj2.getString());
+ }
+ obj2.free();
+ }
+ }
+ obj1.free();
+
+ Guchar digest[16];
+ Decrypt::md5((Guchar*)message.getCString(), message.getLength(), digest);
+
+ Object obj2,obj3;
+
+ obj2.initString(new GooString((const char*)digest,16));
+ obj3.initString(new GooString((const char*)digest,16));
+
+ Object *trailerDict = xref->getTrailerDict();
+ //create ID array
+ obj1.initArray(xref);
+
+ obj1.arrayAdd(&obj2);
+ obj1.arrayAdd(&obj3);
+
+ trailerDict->dictSet("ID", &obj1);
+
+ OutStream *outStr;
+
+ if (!(f = fopen(filename,"r+"))) {
+ error(-1, "Couldn't open file '%s'",filename);
+ }
+ outStr = new FileOutStream(f,0);
+
+ fflush(stdout);
+ popplerdoc->doc->saveAs(outStr,writeForceIncremental);
+
+ fclose(f);
+ delete outStr;
+ g_free(filename);
+ obj1.free();
+ obj2.free();
+ obj3.free();
+}
+
+char*
+poppler_document_get_page_label_by_index(PopplerDocument *document,
+ int index)
+{
+ GooString *string = new GooString();
+ Catalog *catalog = document->doc->getCatalog ();
+
+ catalog->indexToLabel(index,string);
+
+ char *label = string->getCString();
+ return label;
+}
+
+char**
+poppler_document_get_page_labels(PopplerDocument *document)
+{
+
+ char **labels;
+ Catalog *catalog= document->doc->getCatalog ();
+ int npages = poppler_document_get_n_pages(document);
+
+ labels = (char**)malloc(sizeof(char*)*npages);
+
+ int i;
+ for(i = 0; i < npages; i++) {
+
+ GooString *label = new GooString();
+ catalog->indexToLabel(i,label);
+ labels[i] = label->getCString();
+ }
+ return labels;
+}
+
Index: poppler-0.14.0/glib/poppler-document.h
===================================================================
--- poppler-0.14.0/glib/poppler-document.h (revision 194)
+++ poppler-0.14.0/glib/poppler-document.h (working copy)
@@ -122,6 +122,20 @@
PopplerFormField *poppler_document_get_form_field (PopplerDocument *document,
gint id);
+/* Labels */
+char*
+poppler_document_get_page_label_by_index(PopplerDocument *document,
+ int index);
+char**
+poppler_document_get_page_labels(PopplerDocument *document);
+
+
+/*Get the PDF ID from the trailer dictionary. Pdf Id contains two strings of length 16 bytes each, represented in Hex*/
+gboolean poppler_document_get_trailer_id(char *uri,char *password,char *id[2]);
+
+/*Generate the Pdf Id and set it in the Pdf file*/
+void *poppler_document_generate_and_set_id(char *uri,char *password);
+
/* Interface for getting the Index of a poppler_document */
#define POPPLER_TYPE_INDEX_ITER (poppler_index_iter_get_type ())
GType poppler_index_iter_get_type (void) G_GNUC_CONST;
_______________________________________________
poppler mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/poppler