Hi,
This patch gets the pdf ID in the trailer dictionary. If the document
does not contain pdf ID, then it generates and adds it to the trailer
dictionary.
Kindly let me know if You commit this patch.
Thanks
A Srinivas
Index: poppler-0.12.1/glib/poppler-document.cc
===================================================================
--- poppler-0.12.1/glib/poppler-document.cc (revision 189)
+++ poppler-0.12.1/glib/poppler-document.cc (working copy)
@@ -31,7 +31,9 @@
#include <FontInfo.h>
#include <PDFDocEncoding.h>
#include <OptionalContent.h>
+#include <Decrypt.h>
+
#include "poppler.h"
#include "poppler-private.h"
#include "poppler-enums.h"
@@ -340,6 +342,151 @@
delete document->doc;
}
+/* following three functions has added to get and set pdf ID stored in Trailer Dictionary*/
+void get_id(char* id,char *buff) {
+
+ for(int 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);
+ }
+}
+
+char *
+poppler_document_get_trailer_id(char *file,char *ownerpassword,char *userpassword,void *data) {
+
+ char *filename;
+ PDFDoc *newdoc;
+ GooString *opasswd_g;
+ GooString *upasswd_g;
+
+ opasswd_g = upasswd_g = NULL;
+
+ /*remove file:// from the path given*/
+ filename = g_filename_from_uri(file,NULL,NULL);
+
+ if(ownerpassword != NULL) {
+ opasswd_g = new GooString(ownerpassword);
+ }
+ if(userpassword != NULL) {
+ upasswd_g = new GooString(userpassword);
+ }
+
+ GooString *filename_g = new GooString(filename);
+ newdoc = new PDFDoc(filename_g,opasswd_g,upasswd_g,data);
+
+ XRef *xref = newdoc->getXRef();
+ Object *trailer = xref->getTrailerDict();
+
+ Object obj;
+ trailer->dictLookup("ID",&obj);
+
+ if(obj.isNull())
+ return NULL;
+
+ Object val1;
+ obj.arrayGet(0,&val1);
+
+ GooString *id1 = val1.getString();
+ char *Id1 = id1->getCString();
+ char *buff1 = (char*)malloc(32*sizeof(char));
+
+ //buff1 contains the encoded data, to get the actual hex string we are calling this function
+ get_id(Id1,buff1);
+ return buff1;
+}
+
+void *poppler_document_generate_and_set_id(char *file,char *ownerpassword,char *userpassword,void *data) {
+
+ PDFDoc *newdoc;
+ char *filename;
+ GooString *opasswd_g,*upasswd_g;
+ opasswd_g = upasswd_g = NULL;
+
+ filename = g_filename_from_uri(file,NULL,NULL);
+
+ if(ownerpassword != NULL) {
+ opasswd_g = new GooString(ownerpassword);
+ }
+ if(userpassword != NULL) {
+ upasswd_g = new GooString(userpassword);
+ }
+
+ GooString *filename_g = new GooString(filename);
+ newdoc = new PDFDoc(filename_g,opasswd_g,upasswd_g,data);
+
+ 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 = newdoc->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(&obj3);
+ obj1.arrayAdd(&obj2);
+
+ 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);
+ newdoc->saveAs(outStr,writeForceIncremental);
+
+ fclose(f);
+ delete outStr;
+}
+
/**
* poppler_document_get_n_pages:
* @document: A #PopplerDocument
Index: poppler-0.12.1/glib/poppler-document.h
===================================================================
--- poppler-0.12.1/glib/poppler-document.h (revision 189)
+++ poppler-0.12.1/glib/poppler-document.h (working copy)
@@ -121,7 +121,12 @@
/* Form */
PopplerFormField *poppler_document_get_form_field (PopplerDocument *document,
gint id);
+/*Get the PDF ID from the trailer dictionary*/
+char *poppler_document_get_trailer_id(char *file,char *ownerpasswd,char *userpasswd,void *data);
+/*Generate id and set it*/
+void *poppler_document_generate_and_set_id(char *file,char *ownerpasswd,char *userpasswd,void *data);
+
/* 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